π Python Keywords β The Core Building Blocks of the Language
Introduction π
Python keywords are special reserved words that have predefined meanings in the Python language. They cannot be used as variable names or identifiers because they control the structure, logic, and flow of a program.
Note
π‘ Think of keywords as the grammar rules of Python β they define how code should behave.
1. What Are Python Keywords? π€
Keywords are case-sensitive and always written in lowercase. Python 3 has 35 keywords (this number may change in future versions).
You can list all keywords using:
list_keywords.py
import keyword
print(keyword.kwlist)2. Complete List of Python Keywords π
| Keyword | Purpose |
|---|---|
| False | Boolean value false |
| None | Represents null value |
| True | Boolean value true |
| and | Logical AND |
| as | Alias for modules |
| assert | Debugging tool |
| async | Asynchronous functions |
| await | Used inside async functions |
| break | Exit a loop |
| class | Define a class |
| continue | Skip current loop cycle |
| def | Define a function |
| del | Delete an object |
| elif | Else-if condition |
| else | Else condition |
| except | Handle exceptions |
| finally | Run code after try/except |
| for | Looping statement |
| from | Import specific module parts |
| global | Declare global variable |
| if | Conditional statement |
| import | Import a module |
| in | Check membership |
| is | Identity operator |
| lambda | Create small anonymous functions |
| nonlocal | Use non-local variable |
| not | Logical negation |
| or | Logical OR |
| pass | Do nothing placeholder |
| raise | Raise an exception |
| return | Return from a function |
| try | Attempt risky code block |
| while | While loop |
| with | Context manager |
| yield | Pause a generator function |
Note
π§ These keywords are reserved β you cannot create variables like if = 10 orclass = "hello".
3. Using Some Important Keywords π§ͺ
if / elif / else β Conditional Logic
if_else.py
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")for β Looping
for_loop.py
for i in range(3):
print("Hello")def β Creating a Function
function.py
def greet():
print("Hello from Python!")
greet()class β Creating a Class
class_example.py
class Person:
def __init__(self, name):
self.name = name
p = Person("Sathish")
print(p.name)try / except β Error Handling
try_except.py
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero!")4. Checking If a Word Is a Keyword π
You can check whether a word is a Python keyword using keyword.iskeyword().
check_keyword.py
import keyword
print(keyword.iskeyword("if")) # True
print(keyword.iskeyword("hello")) # False5. Tips for Working With Keywords π‘
- Always use lowercase β keywords are case-sensitive.
- Never use keywords as variable, class, or function names.
- Use descriptive variable names to avoid conflicts.
- Keep this list handy as you learn Python syntax.
Conclusion π
>>βKeywords form the grammar of your code β master them, and your programs gain clarity and power.β π₯
You're now familiar with every Python keyword and how to use them. Want a tutorial on operators, variables, data types, loops, functions, or something else? Just ask! π