This tutorial has everything about python keywords and identifiers which is the most important part of every programming languages.
Let’s start with What is Python Keyword?
Every programming has some words which are reserved and you can not use those as variable, those words are known as Keywords. In Python’s version 3.X there is a total of 32 reserved keywords.
So, whenever you write a variable make sure that it’s not a reserved keyword or else the python interpreter will give an error.
All the Python Keywords are case-sensitive that means a word in python will act as a keyword if you write it as it supposed to.
>>> def function_1(): >>> pass
>>>DeF function_1(): #it will through an Error
SyntaxError: invalid syntax
def is a keyword used in python to declare a function, instead of def if you write DeF then it will generate an error, you need to write the keywords as they are supposed to write and as they were defined in Python.
The Keywords in the python are in lower case except for these three True, False and None
Python Reserved Keywords
In Python when you declare a class, or a function or a variable, you give it a name and that could be anything according to you. Those names are known as Identifiers by which you identify your class, functions or variables. Identifiers are the opposite of the Keywords. Identifiers are not reserved in python. You can give any name to the identifiers which you suit, but there are some set of rules to write an identifier.
Identifiers are case-sensitive too for e.g.
variable = 2 Variable =3 print(variable) print(Variable)
In the above example we have defined 2 variables first is variable and the 2nd is Variable and both are different.
As we have mentioned above that there are some sets of rules to write an Identifier.
Rules To write an Identifier
- An identifier can be a combination of alphabets that is lowercase (a to z) or uppercase (A to Z)) or digits(0 to 9) or an underscore(_).
- You can not use a digit or any special character except underscore (_) to start an identifier name.
- You cannot use any special symbol to write an Identifier.
- You cannot give blank space to write an identifier.
- You cannot use a reserved keyword to write an identifier.
Variable23 = 44 # Valid Identifier 23Variable = 44 #Invalid Identifier start with a digit _23Variable = 44 #Valid variable start with underscore Variable 23 = 44 # Invalid blank space between Variable and 23 Variable@ 23 = 44 # Invalid blank it contains a special symbol @ Variable_23 = 44 #Valid there is no space in between the identifier False = 44 #Invalid Identifier False is a Keyword
Point to Remember
- Python is case sensitive, so be careful while using cases.
- The Identifiers could be of any length.
- Always use underscore when you write a lengthy identifier.