Python Keywords
Keywords are reserved words in Python that have a predefined meaning. They are part of the Python language syntax and cannot be used as variable, function or class names.
Python provides keywords for decision making, loops, functions, classes, exception handling, Boolean operations and other programming tasks.
List of Python Keywords
The following are the Python keywords used in modern Python 3:
| Keyword | Purpose |
|---|---|
| and | Logical AND operation |
| as | Creates an alias |
| assert | Checks whether a condition is true |
| async | Defines an asynchronous function |
| await | Waits for an asynchronous operation |
| break | Terminates a loop |
| case | Defines a pattern in match statements |
| class | Defines a class |
| continue | Skips the current loop iteration |
| def | Defines a function |
| del | Deletes an object or item |
| elif | Tests another condition |
| else | Executes when a condition is false |
| except | Handles an exception |
| False | Boolean false value |
| finally | Executes after try/except |
| for | Creates a for loop |
| from | Imports selected objects from a module |
| global | Refers to a global variable |
| if | Tests a condition |
| import | Imports a module |
| in | Tests membership or iterates over values |
| is | Tests object identity |
| lambda | Creates an anonymous function |
| match | Starts pattern matching |
| None | Represents no value |
| nonlocal | Refers to a variable in an enclosing function |
| not | Logical NOT operation |
| or | Logical OR operation |
| pass | Creates an empty statement |
| raise | Raises an exception |
| return | Returns a value from a function |
| True | Boolean true value |
| try | Starts exception handling |
| while | Creates a while loop |
| with | Manages resources using a context manager |
| yield | Returns a value from a generator |
Python Keywords with Examples
1. and, or, not
These keywords are used for logical operations.
age = 20
print(age >= 18 and age <= 60)
print(age < 18 or age > 60)
print(not age < 18)
2. if, elif and else
These keywords are used for decision making.
marks = 75
if marks >= 80:
print("Grade A")
elif marks >= 40:
print("Pass")
else:
print("Fail")
Pass
3. for, while, break and continue
These keywords are used to control loops.
for i in range(1, 6):
if i == 3:
continue
if i == 5:
break
print(i)
1 2 4
4. def and return
def defines a function and return sends a value
back to the calling statement.
def add(a, b):
return a + b
print(add(10, 20))
30
5. class
The class keyword is used to create a class.
class Student:
name = "Rahul"
s = Student()
print(s.name)
Rahul
6. import and from
These keywords are used to work with modules.
import math
print(math.sqrt(25))
The from keyword can be used to import a particular object.
from math import sqrt
print(sqrt(36))
7. in and is
in checks membership, while is checks whether
two references point to the same object.
numbers = [10, 20, 30]
print(20 in numbers)
a = None
print(a is None)
True True
8. True, False and None
True and False represent Boolean values.
None represents the absence of a value.
x = True
y = False
z = None
print(x)
print(y)
print(z)
9. pass
The pass keyword is used when a statement is required
syntactically but no action is needed.
def display():
pass
10. try, except, else and finally
These keywords are used for exception handling.
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Program completed")
Cannot divide by zero Program completed
11. raise
The raise keyword is used to generate an exception manually.
age = 15
if age < 18:
raise ValueError("Age must be 18 or above")
12. with and as
The with keyword is commonly used for file handling.
The as keyword gives a name to the object being used.
with open("data.txt", "r") as file:
data = file.read()
print(data)
13. lambda
The lambda keyword creates a small anonymous function.
square = lambda x: x * x
print(square(5))
25
14. del
The del keyword removes an object, variable or item.
numbers = [10, 20, 30]
del numbers[1]
print(numbers)
[10, 30]
15. global
The global keyword allows a function to modify a global variable.
count = 0
def update():
global count
count = 10
update()
print(count)
10
16. nonlocal
The nonlocal keyword is used to modify a variable in an
enclosing function.
def outer():
x = 10
def inner():
nonlocal x
x = 20
inner()
print(x)
outer()
20
17. assert
The assert keyword checks a condition and raises an
AssertionError if the condition is false.
age = 20
assert age >= 18
print("Valid age")
18. yield
The yield keyword is used in a generator function to
produce values one at a time.
def numbers():
yield 1
yield 2
yield 3
for n in numbers():
print(n)
1 2 3
19. async and await
async defines an asynchronous function and
await is used to wait for an asynchronous operation.
import asyncio
async def message():
print("Hello Python")
asyncio.run(message())
20. match and case
match and case are used for structural
pattern matching.
day = 2
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case _:
print("Other day")
Tuesday
keyword.kwlist.
View Python Keywords Using Python
import keyword
print(keyword.kwlist)