Python keywords are reserved words that are used for a specific purpose. These keywords cannot be used in any identifiers such as function names, variables, classes etc. The keywords convey a specific meaning to the compiler/interpreter to perform special functions. Keywords are case sensitive and must be written as it is. Following are the list of keywords that are available in the language as of version 3.3.
To view the list of available keywords, the keywords command from python help() mode can be used. Follow the below steps to open the help section.
1. Open command prompt/terminal.
2. Enter the command python. The interactive shell will be initiated.
3. Enter the command help() to open the help section.
4. Now, enter the command keywords. This will display the list of keywords available in the installed version of python as shown below.
To exit from the help section, press Ctrl+C and then type exit() to exit the interactive mode.
Python Keywords example
def sample(a, b): a = a; b = b; if a > b: print("a is greater than b") else: print("b is greater than a") sample(3, 5)
In the above example, we have used three keywords def, if, else. Python functions are defined with the keyword def. The sample program contains a simple function with the name sample which gets two parameters with variable names a and b as input and checks the greater number of the two inputs and displays the message based on the condition.
In the final line of the program, we have called the function and passed two parameters 3 and 5. Since we have passed the second number as a bigger one, the output will be displayed as “b is greater than a”. You can save this code in a file named sample.py and execute it in the command prompt by calling the filename as shown below.