Nearby lessons

13 of 159

Python - Reserved Words

📌 What You Will Learn
  • Know what reserved words are in Python
  • Understand why you cannot use them as identifiers
  • Learn all 35 Python reserved words

Introduction

In Python, some words have a predefined meaning.

These words are called Reserved Words or Keywords.

Python uses these keywords for specific programming tasks, so they cannot be used as identifiers.

You cannot use reserved words as:

  • Variable names
  • Function names
  • Class names
  • Module names
  • Other identifiers

Total Reserved Words

Python 3.14 contains 35 reserved words (keywords).

Python also provides four soft keywords:

  • match
  • case
  • _
  • type

Soft keywords behave like keywords only in specific situations and are not included in the reserved keyword list.

List of Python Reserved Words

Reserved Words
True, False, None
and, or, not, is
if, elif, else
while, for, break, continue, return
in, yield
try, except, finally, raise, assert
import, from, as
class, def, pass, lambda
global, nonlocal
del, with
async, await

1. Boolean Keywords

True and False represent Boolean values.

🐍Code Cell
1a = True
2b = False
3 
4print(a)
5print(b)
Output
True
False

2. Special Value

None represents the absence of a value.

🐍Code Cell
1x = None
2 
3print(x)
Output
None

3. Logical Keywords

The keywords and, or, not, and is are used in logical expressions.

🐍Code Cell
1a = True
2b = False
3 
4print(a and b)
5print(a or b)
6print(not a)
Output
False
True
False

4. Conditional Keywords

The keywords if, elif, and else are used for decision making.

🐍Code Cell
1age = 18
2 
3if age >= 18:
4 print("Eligible")
5else:
6 print("Not Eligible")
Output
Eligible

5. Looping Keywords

The keywords for, while, break, continue, return, in, and yield are used in loops and iteration.

🐍Code Cell
1for i in range(5):
2 print(i)
Output
0
1
2
3
4

6. Exception Handling Keywords

Python provides keywords to handle runtime errors.

🐍Code Cell
1try:
2 print(10 / 0)
3except ZeroDivisionError:
4 print("Cannot divide by zero")
Output
Cannot divide by zero

7. Import Keywords

The keywords import, from, and as are used to import modules.

🐍Code Cell
1import math
2 
3print(math.sqrt(16))
Output
4.0

8. Asynchronous Programming Keywords

The keywords async and await are used for asynchronous programming.

🐍Code Cell
1import asyncio
2 
3async def greet():
4 print("Hello")
5 
6asyncio.run(greet())
Output
Hello

9. OOP and Function Keywords

The keywords class, def, pass, and lambda are used in object-oriented programming and function definitions.

🐍Code Cell
1def display():
2 pass
Output
Function created successfully.

10. Variable Scope Keywords

The keywords global and nonlocal control variable scope.

🐍Code Cell
1x = 10
2 
3def test():
4 global x
5 x = 20
6 
7test()
8 
9print(x)
Output
20

11. Delete Keyword

The del keyword is used to delete variables or objects.

🐍Code Cell
1a = 10
2 
3del a
Output
Variable deleted.

12. Context Management Keyword

The with keyword is used for resource management, such as working with files.

🐍Code Cell
1with open("test.txt") as file:
2 print(file.read())
Output
Contents of the file are displayed.

Important Notes

  • All reserved words contain only alphabet characters.
  • True, False, and None begin with capital letters.
  • All other reserved words are written in lowercase.
  • async and await were introduced in Python 3.5.
  • match, case, _, and type are soft keywords.

Display All Reserved Words

Python provides the keyword module to display all reserved words.

🐍Code Cell
1import keyword
2 
3print(keyword.kwlist)
Output
Displays the complete list of Python reserved words.

Invalid Use of Reserved Words

Reserved words cannot be used as identifiers.

🐍Code Cell
1if = 100
Output
SyntaxError

Key Points

  • Python 3.14 contains 35 reserved words.
  • Reserved words have predefined meanings.
  • They cannot be used as variable names or other identifiers.
  • True, False, and None start with capital letters.
  • Most other keywords are written in lowercase.
  • The keyword module displays the list of reserved words.

Quick Summary

Category Examples
Boolean ValuesTrue, False
Special ValueNone
Logical Operatorsand, or, not, is
Conditional Statementsif, elif, else
Loopsfor, while, break, continue
Exception Handlingtry, except, finally, raise
Importimport, from, as
OOP & Functionsclass, def, lambda, pass
Variable Scopeglobal, nonlocal
Othersdel, with, async, await
📝 Key Takeaways
  • Reserved words have special meaning in Python
  • You cannot use them as variable names, function names, or class names
  • There are 35 reserved words in Python 3
  • Keywords like if, else, for, while, def, class are commonly used
  • Always check the official list when in doubt

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8