What Are Keywords in Python?
Keywords are special reserved words in Python that have predefined meanings. They are used to define the structure and behavior of a Python program.
Python keywords have specific purposes, such as creating conditional statements, defining loops, handling functions, and managing exceptions.
Since keywords are reserved by Python, they cannot be used as variable names, function names, or other identifiers.
For example:
if age >= 18:
print("You are eligible")Here, if is a keyword used to create a conditional statement.
List of Python Keywords
Python provides several keywords for different programming operations. The exact list can vary between Python versions.
Some commonly used Python keywords include:
| Keyword | Purpose |
|---|---|
if | Checks a condition |
else | Executes an alternative block |
elif | Checks another condition |
for | Creates a loop |
while | Creates a loop |
break | Terminates a loop |
continue | Skips the current iteration |
def | Defines a function |
return | Returns a value |
class | Defines a class |
True | Represents a Boolean true value |
False | Represents a Boolean false value |
None | Represents the absence of a value |
import | Imports a module |
from | Imports specific objects |
try | Starts exception handling |
except | Handles an exception |
finally | Executes cleanup code |
pass | Represents an empty statement |
and | Logical AND operator |
or | Logical OR operator |
not | Logical NOT operator |
in | Checks membership |
is | Checks object identity |
How to Get the List of Keywords in Python
Python provides a built-in module called keyword that allows you to view all reserved keywords.
Example:
import keyword
print(keyword.kwlist)This program displays the list of keywords supported by your installed Python version.
You can also check whether a particular word is a keyword using iskeyword().
Example:
import keyword print(keyword.iskeyword("if")) print(keyword.iskeyword("name"))
Output:
True
False
Here, if is a reserved keyword, whereas name is not.
Categories of Python Keywords
Let's understand some important categories of keywords.
1. Conditional Keywords
Conditional keywords are used to make decisions in a program.
Examples: if, elif, and else.
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")2. Looping Keywords
Looping keywords are used to control repetitive operations.
Examples: for, while, break, and continue.
for i in range(5): print(i)
3. Function Keywords
The def keyword is used to define a function, while return is used to send a result back from it.
def add(a, b): return a + b print(add(10, 20))
Output:
30
4. Logical Keywords
Logical keywords are used to combine or modify conditions.
Examples: and, or, and not.
age = 25
if age >= 18 and age <= 60:
print("Eligible")5. Exception Handling Keywords
These keywords help handle errors during program execution.
Examples: try, except, finally, and raise.
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")Difference Between Keywords and Identifiers
Keywords and identifiers are different concepts in Python. Let's understand their differences:
| Keywords | Identifiers |
|---|---|
| Reserved words in Python | Names created by programmers |
| Have predefined meanings | Used to name variables, functions, and classes |
| Cannot be used as identifiers | Must follow naming rules |
Example: if, for, while |
Example: name, age, total |
Conclusion
Python keywords are reserved words that help define how a program works. They are used in conditions, loops, functions, classes, and exception handling.
Understanding keywords is important for writing correct Python programs. You can use the keyword module to explore the reserved words available in your Python version.
