
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Reserved Keywords in Python?
Reserved keywords are the special words that are predefined by the language. These keywords are used to define the structure and syntax of the Python programs. They cannot be used as variable names, function names, or identifiers.
Python has a fixed set of keywords that are recognized by the interpreter, and these keywords cannot be redefined. The Python keywords are case sensitive.
To view all the keywords in the current Python version, we can use the built-in keyword module along with the 'keyword.kwlist' attribute to return the list of all the reserved keywords in Python as a list of strings.
import keyword print(keyword.kwlist)
This will generate the list of all the reserved keywords in the current Python version -
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Examples of Reserved Keywords in Python
Below are the examples of reserved keywords -
Example 1
In this scenario, we are going to use the if which is a conditional keyword used to check a boolean expression. If the condition is true, it executes the indented block.
a = 5 if a > 2: print("a is greater than 2")
The output of the above program is as follows -
a is greater than 2
Example 2
Here we are going to use the for keyword, which is a looping keyword used to iterate over a sequence.
for x in range(2): print("Iteration", x)
The output of the above program is as follows -
Iteration 0 Iteration 1
Example 3
In this approach, we are going to use the def, which is used to define the function.
def demo(x): return "Welcome To " + x print(demo("TP."))
The output of the above program is as follows -
Welcome To TP.