Keywords in Python
Every programming language has its own set of keywords that are reserved for special purposes. These keywords cannot be used for any other purpose such as variable name or class name.
Python has several keywords and a few rules for writing identifiers. In this tutorial, we will discuss Python keywords.
Types of Python Keywords
The Python keywords can be divided into several categories based on their usage.
Control flow keywords – Used to control the flow of execution
Iteration keywords – Used for creating and working with loops
Value keywords – Used as values
Returning keywords – Used for returning data from methods or functions
Operator keywords – Used as operators
Import keywords – Used for importing modules
Structuring keywords – Used in structuring the program
Exception-handling keywords – Used in exception handling
Variable handling keywords – Used for working with variables
Asynchronous keywords – Used to handle asynchronous data
Note: Except for value keywords (True, False, None) all other keywords are case sensitive.
Remember, the keywords should not be used as variable names, class names, or function names. Observe the following code.
If keywords are are used else where not intended to use python interpreter throws a Syntax Error: Invalid syntax
Example of Syntax error
def global(): print("This is a function") global()
The code will give a syntax error because “global” is one of the Python keywords and it cannot be used as a function name. Let’s make some changes to the above code.
def Global(): print("This is a function") Global()
The first letter of the function name is capitalized, meaning, it not a keyword anymore (because Python keywords are case-sensitive).