Keywords:
These keywords serve various purposes in python as below:
– Control Flow: Keywords like if, else, elif, for, while, break, continue, and pass are used for control flow statements and loops.
– Function and Class Definitions: Keywords such as def and class are used to define functions and classes respectively.
– Logical Operators: Keywords like and, or, not are used as logical operators for Boolean expressions.
– Exception Handling: Keywords such as try, except, finally, raise are used for exception handling.
– Importing Modules: Keywords like import and from are used to import modules and packages into the Python script.
– Boolean Values: Keywords True and False represent the Boolean values in Python.
It is important to note that keywords are case-sensitive and cannot be used as variable names or identifiers in Python programs.
Attempting to use a keyword as an identifier will result in a syntax error as below:
#Assigning value to class keyword
class = 45
Output: File "<ipython-input-5-d49a1984b151>", line 1 class = 45 ^ SyntaxError: invalid syntax
- Let’s see all keywords of python
import keyword
print('All keywords of python : ',keyword.kwlist)
print('Total number of keyword :',len(keyword.kwlist))
Output: All keywords of python : ['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'] Total number of keyword : 35