Python keywords are reserved words that have special meaning in the language. These keywords define the syntax and structure of Python and cannot be used as identifiers (names for variables, functions, classes, etc.).
In this article, we’ll explore all Python keywords (as of Python 3.11+), explain what each does, and provide examples for every keyword.
Getting All Keywords in Python
To view all keywords programmatically:
import keyword
print(keyword.kwlist)
Python Keywords (with Examples)
Below is the complete list of Python keywords and a short example for each.
✅ 1. False
Boolean false value.
x = False
if not x:
print("It is false")
✅ 2. True
Boolean true value.
x = True
if x:
print("It is true")
✅ 3. None
Represents the absence of a value.
x = None
if x is None:
print("No value")
✅ 4. and
Logical AND.
a = True
b = False
print(a and b) # False
✅ 5. or
Logical OR.
a = True
b = False
print(a or b) # True
✅ 6. not
Logical NOT.
a = False
print(not a) # True
✅ 7. if
Used for conditional execution.
x = 10
if x > 5:
print("Greater than 5")
✅ 8. elif
Else-if condition in branching.
x = 5
if x > 5:
print("Greater")
elif x == 5:
print("Equal")
✅ 9. else
Final fallback block.
x = 3
if x > 5:
print("Greater")
else:
print("Smaller or equal")
✅ 10. while
Used for loops with a condition.
x = 0
while x < 3:
print(x)
x += 1
✅ 11. for
Looping over sequences.
for i in range(3):
print(i)
✅ 12. break
Breaks out of a loop.
for i in range(5):
if i == 3:
break
print(i)
✅ 13. continue
Skips current iteration.
for i in range(5):
if i == 2:
continue
print(i)
✅ 14. pass
Does nothing (used as a placeholder).
def my_func():
pass
✅ 15. def
Defines a function.
def greet():
print("Hello")
greet()
✅ 16. return
Returns a value from a function.
def add(x, y):
return x + y
print(add(2, 3))
✅ 17. yield
Used with generators.
def count_up_to(n):
for i in range(n):
yield i
print(list(count_up_to(3)))
✅ 18. import
Imports a module.
import math
print(math.sqrt(16))
✅ 19. from
Used with import
.
from math import pi
print(pi)
✅ 20. as
Gives an alias to modules or context managers.
import math as m
print(m.pi)
✅ 21. class
Defines a class.
class Dog:
def bark(self):
print("Woof")
✅ 22. global
Declares a global variable.
x = 0
def set_global():
global x
x = 5
set_global()
print(x)
✅ 23. nonlocal
Declares a non-local variable in nested functions.
def outer():
x = 5
def inner():
nonlocal x
x = 10
inner()
print(x)
outer()
✅ 24. lambda
Creates an anonymous function.
square = lambda x: x * x
print(square(4))
✅ 25. try
Start of a try-except block.
try:
print(1 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
✅ 26. except
Handles exceptions.
(See above)
✅ 27. finally
Always executes after try/except.
try:
x = 1 / 1
finally:
print("Cleaning up...")
✅ 28. raise
Raises an exception.
raise ValueError("Invalid value")
✅ 29. assert
Debugging aid to check conditions.
assert 2 + 2 == 4
✅ 30. del
Deletes an object.
x = [1, 2, 3]
del x[1]
print(x)
✅ 31. with
Context manager (automatically handles resources).
with open("example.txt", "w") as f:
f.write("Hello")
✅ 32. async
Defines an asynchronous function.
async def greet():
return "Hi"
✅ 33. await
Waits for an asynchronous result.
import asyncio
async def say_hello():
return "Hello"
async def main():
greeting = await say_hello()
print(greeting)
asyncio.run(main())
✅ 34. in
Checks membership.
print(2 in [1, 2, 3]) # True
✅ 35. is
Checks object identity.
x = None
print(x is None) # True
✅ 36. match
Structural pattern matching (Python 3.10+).
def http_status(code):
match code:
case 200:
return "OK"
case 404:
return "Not Found"
print(http_status(200))
✅ 37. case
Used inside match
.
(See above)
Summary Table
Keyword | Description |
---|---|
Control | if , else , elif , while , for , break , continue , pass |
Functions | def , return , yield , lambda |
Imports | import , from , as |
Exceptions | try , except , finally , raise , assert |
Classes | class |
Variables | global , nonlocal , del |
Boolean | True , False , None |
Operators | and , or , not , is , in |
Async | async , await |
Pattern Matching | match , case |
Context | with |
Final Thoughts
Python keywords are the backbone of the language’s syntax. Mastering them helps you read and write Python code more effectively and allows you to understand how Python logic is constructed.