Python Keywords
Keywords are the reserved words in the Python programming language. All keywords are designated with a special meaning to each. The meaning of all keywords is fixed and it cannot be modified or removed. All the keywords need to be used as they have defined (Lower case or Upper case). In Python, there are 33 keywords. All the keywords in Python are listed in the following table with their meaning.
S.No. | Keyword | Description |
---|---|---|
1 | and | Logical AND operator |
2 | as | Used to create an alias |
3 | assert | Used for debugging |
4 | break | Used to break out of a loop |
5 | class | Used to create a class |
6 | continue | Used to continue to the next iteration of a loop |
7 | def | Used to create a function |
8 | del | Used to delete an object |
9 | elif | Used in conditional statements, same as else if |
10 | else | Used in conditional statements - if |
11 | except | Used with exceptions, what to do when an exception occurs |
12 | finally | Used with exceptions, what to do when none of the specified exceptions solved |
13 | for | Used to create a for loop |
14 | from | Used to import specific parts of a module |
15 | global | Used to declare a global variable |
16 | if | Used to define a conditional statement |
17 | import | Used to import a module |
18 | in | Used to check if a value is present in a list, tuple, etc. |
19 | is | Used to test if two variables are equal |
20 | lambda | Used to create an anonymous function |
21 | None | Used to represent a NULL value |
22 | nonlocal | Used to declare a non-local variable |
23 | not | Logical NOT operator |
24 | or | Logical OR operator |
25 | pass | A null statement, a statement that will do nothing |
26 | raise | Used to raise an exception |
27 | return | Used to exit a function and return a value |
28 | try | Used to make a try...except statement in exception handling |
29 | while | Used to create a while loop |
30 | with | Used to simplify exception handling |
31 | yield | Used to end a function, returns a generator |
32 | FALSE | Boolean value False |
33 | TRUE | Boolean value True |
Following is the Python code used to display all the keywords in Python 3.7.3.
Python code to display all keywords in Python
import keyword
print(keyword.kwlist)
Sample program to display all the keywords in Python using Python IDLE
Sample program to display all the keywords in Python using PyCharm IDE
Points to be Remembered
- All the keywords must be used as they have defined.
- Keywords should not be used as identifiers like variable names, functions name, class name, etc.
- The meaning of each keyword is fixed, we can not modify or remove it.