Keywords in Python
Keywords are special reserved words that are part of the language itself. They define the rules and structure of Python programs which means you cannot use them as names for your variables, functions, classes or any other identifier.
Getting List of all Python keywords
import keyword
print("The list of keywords are : ")
print(keyword.kwlist)
The list of keywords are:
['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']
Keyword-Description
and --> A logical operator
as --> To create an alias
assert --> For debugging
async --> Define an asynchronous function
await --> Wait for and get a result from an awaitable
break --> To break out of a loop
case --> Pattern in a match statement
class --> To define a class
continue --> To continue to the next iteration of a loop
def --> To define a function
del --> To delete an object
elif --> Used in conditional statements, same as else if
else --> Used in conditional statements
except -->Used with exceptions, what to do when an exception occurs
False --> Boolean value, result of comparison operations
finally --> Used with exceptions, a block of code that will be executed no matter if there is an exception or not
for --> To create a for loop
from --> To import specific parts of a module
global--> To declare a global variable
if -->To make a conditional statement
import --> To import a module
in --> To check if a value is present in a list, tuple, etc.
is --> To test if two variables are equal
lambda -->To create an anonymous function
match --> Start a match statement (compare a value against cases)
None --> Represents a null value
nonlocal--> To declare a non-local variable
not --> A logical operator
or --> A logical operator
pass --> A null statement, a statement that will do nothing
raise --> To raise an exception
return --> To exit a function and return a value
True --> Boolean value, result of comparison operations
try --> To make a try...except statement
while --> To create a while loop
with --> Used to simplify exception handling
yield -->To return a list of values from a generator
Identify Python Keywords
Ways to identify Keywords are:
- With Syntax Highlighting: Most of IDEs provide syntax-highlight feature. You can see Keywords appearing in different color or style.
- Look for SyntaxError: This error will encounter if you have used any keyword incorrectly. Keywords can not be used as identifiers like variable or a function name.
Keywords as Variable Names
If we attempt to use a keyword as a variable, Python will raise a SyntaxError.
Let's look at an example:
for = 10
print(for)
Output :
Let's categorize all keywords based on context for a more clear understanding
** Keywords vs Identifiers**
Variables vs Keywords
Variables vs Identifiers in Python
They are related, but not the same.
In Python programming, data such as numbers, strings, and other values are stored in the computerโs main memory. To store this data, Python allocates sufficient memory using appropriate data types.
- To process and access the data stored in memory, we must assign distinct names to these memory locations. These distinct names help us identify the stored values and are known as Identifiers.
- During program execution, the values stored in these identifiers can change or vary. Because of this changing nature, identifiers are also called Variables.
๐ Hence, all types of literals must be stored in the form of variables.
In Python, every variable is treated as an Object, because Python follows an object-oriented programming model.
Definition of a Variable
A Variable is an identifier whose value can be changed during the execution of a program.
In any programming language, data processing is not possible without using variables (called objects in Python). Variables act as containers that store data and allow programs to manipulate it.
Reference :




Top comments (0)