Last Updated : 12 Jul, 2025
Keywords in Python are reserved words that have special meanings and serve specific purposes in the language syntax. Python keywords cannot be used as the names of variables, functions, and classes or any other identifier.
Getting List of all Python keywordsWe can also get all the keyword names using the below code.
Python
import keyword
# printing all keywords at once using "kwlist()"
print("The list of keywords is : ")
print(keyword.kwlist)
Output:
How to Identify Python Keywords ?The list of keywords are:
['False', 'None', 'True',"__peg_parser__ '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']
In Python, keywords are reserved words that have special meanings and cannot be used as variable names. If you attempt to use a keyword as a variable, Python will raise a SyntaxError. Let's look at an example:
Python
Output
Hangup (SIGHUP)
File "/home/guest/sandbox/Solution.py", line 1
for = 10
^
SyntaxError: invalid syntax
Let's categorize all keywords based on context for a more clear understanding.
Category KeywordsValue Keywords
Operator Keywords and, or, not, is, inControl Flow Keywords
if, else, elif, for, while, break, continue, pass, try, except, finally, raise, assert
Function and Class def, return, lambda, yield, class Context Management with, as Import and Module import, from Scope and Namespace global, nonlocal Async Programming async, await Difference Between Keywords and Identifiers Keywords Identifiers Reserved words in Python that have a specific meaning. Names given to variables, functions, classes, etc. Cannot be used as variable names. Can be used as variable names (if not a keyword). Examples: if, else, for, while Examples: x, number, sum, result Part of the Python syntax. User-defined, meaningful names in the code. They cannot be redefined or changed. Can be defined and redefined by the programmer. Difference Between Variables and Keywords Variables Keywords Used to store data. Reserved words with predefined meanings in Python. Can be created, modified, and deleted by the programmer. Cannot be modified or used as variable names. Examples: x, age, name Examples: if, while, for Hold values that are manipulated in the program. Used to define the structure of Python code. Variable names must follow naming rules but are otherwise flexible. Fixed by Python language and cannot be altered.Suggested Quiz
10 Questions
Which of the following keywords is used to define a function in Python?
Explanation:
def is the keyword used to define a function in Python. After def, you specify the function name, followed by parentheses () and a colon : to start the function's body.
What keyword is used to handle exceptions in Python?
except
catch
handle
error
Explanation:
try is used to define a block of code that might raise an exception. except follows the try block and defines the code that runs if an exception occurs.
Which keyword is used to create a new class in Python?
Explanation:
class is used to define a new class in Python.
In Python, which keyword is used to indicate that a variable is global?
global
public
shared
external
Explanation:
In Python, the keyword global is used to indicate that a variable is global. This keyword is used inside a function to refer to a variable that is defined in the global scope, allowing the function to modify the global variable directly.
What does the elif keyword do in Python?
It is used to define an exception.
It is used to start a function.
It is used in conditional statements to check additional conditions.
It is used to define a loop.
Explanation:
elif is short for "else if" and is used after an if block to check for additional conditions when the original if condition fails.
What keyword is used to return a value from a function in Python?
Explanation:
The return keyword is used inside a function to send back a result or value to the caller, effectively ending the function.
Which of the following keywords is used to define an object constructor in Python?
init
constructor
__init__
define
Explanation:
__init__ is a special method in Python used to initialize an object's state when a new instance of a class is created.
Which of the following keywords is used to declare an alias for a module in Python?
import as
using
alias
import
Explanation:
The import as syntax allows you to import a module and give it a shorthand alias, which is useful for convenience.
What keyword is used to define an empty function or class in Python?
Explanation:
pass is a placeholder keyword that does nothing. It is used in scenarios where we need to define a function or class but haven’t implemented it yet.
Which keyword is used to define an anonymous function in Python?
lambda
def
function
anonymous
Explanation:
The lambda keyword is used to create anonymous (unnamed) functions in Python, typically for short, simple operations.
Quiz Completed Successfully
Your Score : 2/10
Accuracy : 0%
Login to View Explanation
1/10 1/10 < Previous Next >RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4