List of Keywords in Python
Keyword |
Description |
Keyword |
Description |
Keyword |
Description |
---|---|---|---|---|---|
and |
It is a Logical Operator |
False |
Represents an expression that will result in not being true. |
nonlocal |
It is a non-local variable |
as |
It is used to create an alias name |
finally |
It is used with exceptions |
not |
It is a Logical Operator |
assert |
It is used for debugging |
for |
It is used to create Loop |
or |
It is a Logical Operator |
break |
Break out a Loop |
from |
To import specific parts of a module |
pass |
pass is used when the user doesn’t want any code to execute |
class |
It is used to define a class |
global |
It is used to declare a global variable |
raise |
raise is used to raise exceptions or errors. |
continue |
Skip the next iteration of a loop |
if |
To create a Conditional Statement |
return |
return is used to end the execution |
def |
It is used to define the Function |
import |
It is used to import a module |
True |
Represents an expression that will result in true. |
del |
It is used to delete an object |
is |
It is used to test if two variables are equal |
try |
Try is used to handle errors |
elif |
Conditional statements, same as else-if |
in |
To check if a value is present in a Tuple, List, etc. |
while |
While Loop is used to execute a block of statements |
else |
It is used in a conditional statement |
lambda |
Used to create an anonymous function |
with |
with statement is used in exception handling |
except |
try-except is used to handle these errors |
None |
It represents a null value |
yield |
yield keyword is used to create a generator function |
Getting the List of all Python keywords
We can also get all the keyword names using the below code.
Python3
import keyword # printing all keywords at once using "kwlist()" print ( "The list of keywords is : " ) print (keyword.kwlist) |
Output:
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']
Let’s discuss each keyword in detail with the help of good examples.
Python Keywords
Every language contains words and a set of rules that would make a sentence meaningful. Similarly, in Python programming language, there are a set of predefined words, called Keywords which along with Identifiers will form meaningful sentences when used together. Python keywords cannot be used as the names of variables, functions, and classes.
In this article, we will learn about Python keywords and how to use them to perform some tasks.