Reserved Words
Python reserved words (keywords) are special words that have predefined meanings and cannot be used as variable names, function names, or any other identifiers. This reference provides a complete list of all Python keywords with their purposes and usage examples.
Python Keywords Reference
Keyword | Purpose | Category | Usage Example | Description |
---|---|---|---|---|
and | Logical operator | Operator | if x > 0 and y > 0: | Logical AND operation |
as | Alias creation | Statement | import numpy as np | Create alias for imports or with statements |
assert | Debugging aid | Statement | assert x > 0, "x must be positive" | Test conditions during development |
break | Loop control | Control Flow | break | Exit current loop immediately |
class | Class definition | Definition | class MyClass: | Define a new class |
continue | Loop control | Control Flow | continue | Skip to next iteration of loop |
def | Function definition | Definition | def my_function(): | Define a new function |
del | Object deletion | Statement | del my_variable | Delete objects or variables |
elif | Conditional | Control Flow | elif condition: | Additional condition in if statement |
else | Conditional/Loop | Control Flow | else: | Alternative branch in if or loop |
except | Exception handling | Exception | except ValueError: | Handle specific exceptions |
False | Boolean value | Literal | is_valid = False | Boolean false value |
finally | Exception handling | Exception | finally: | Code that always executes |
for | Loop construct | Control Flow | for item in list: | Iterate over sequences |
from | Import statement | Import | from math import sqrt | Import specific items from module |
global | Scope declaration | Scope | global my_var | Declare global variable in function |
if | Conditional | Control Flow | if condition: | Basic conditional statement |
import | Module import | Import | import os | Import modules or packages |
in | Membership test | Operator | if item in list: | Test membership in sequence |
is | Identity test | Operator | if x is None: | Test object identity |
lambda | Anonymous function | Function | lambda x: x * 2 | Create small anonymous functions |
None | Null value | Literal | result = None | Represents absence of value |
nonlocal | Scope declaration | Scope | nonlocal my_var | Access enclosing scope variable |
not | Logical operator | Operator | if not condition: | Logical NOT operation |
or | Logical operator | Operator | if x or y: | Logical OR operation |
pass | Placeholder | Statement | pass | Empty placeholder statement |
raise | Exception raising | Exception | raise ValueError("message") | Raise exceptions manually |
return | Function return | Statement | return result | Return value from function |
True | Boolean value | Literal | is_valid = True | Boolean true value |
try | Exception handling | Exception | try: | Begin exception handling block |
while | Loop construct | Control Flow | while condition: | Create while loops |
with | Context manager | Statement | with open(file) as f: | Resource management |
yield | Generator | Function | yield value | Create generator functions |
Keyword Categories
Control Flow Keywords
Keywords that control the flow of program execution:
- if, elif, else: Conditional execution
- for, while: Loop constructs
- break, continue: Loop control
- pass: Empty placeholder
Function and Class Definition
Keywords for defining reusable code blocks:
- def: Function definition
- class: Class definition
- lambda: Anonymous functions
- return: Return values
- yield: Generator functions
Exception Handling
Keywords for managing errors and exceptions:
- try, except, finally: Exception handling blocks
- raise: Manually raise exceptions
- assert: Debug-time assertions
Import and Module Keywords
Keywords for working with modules:
- import: Import modules
- from: Import specific items
- as: Create aliases
Scope and Variable Keywords
Keywords affecting variable scope:
- global: Global variable declaration
- nonlocal: Enclosing scope variable access
- del: Delete variables or objects
Logical and Comparison Operators
Keywords for logical operations:
- and, or, not: Logical operators
- in: Membership testing
- is: Identity testing
Literal Values
Keywords representing fixed values:
- True, False: Boolean literals
- None: Null value literal
Context Management
Keywords for resource management:
- with: Context manager protocol
Common Usage Patterns
Conditional Execution
# Basic if-elif-else structure
age = 25
if age >= 18:
print("Adult")
elif age >= 13:
print("Teenager")
else:
print("Child")
Loop Constructs
# For loop with break and continue
for i in range(10):
if i == 3:
continue # Skip 3
if i == 7:
break # Stop at 7
print(i)
Exception Handling
try:
# risky code
except SpecificError as e:
# handle specific error
except Exception as e:
# handle general errors
finally:
# cleanup code
Function Definition
# Function with global variable
counter = 0
def increment():
global counter
counter += 1
return counter
print(increment()) # 1
print(increment()) # 2
Class Definition
class ClassName:
def __init__(self):
pass
def method(self):
return value
Important Notes
- Case Sensitive: All keywords are lowercase and case-sensitive
- Reserved: Cannot be used as identifiers (variable names, function names, etc.)
- Version Specific: Some keywords may be added in newer Python versions
- Syntax Highlighting: Most editors highlight keywords in different colors
- Error Prevention: Using keywords as identifiers will raise SyntaxError
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.