Python Keywords and Identifiers
This sections explains keywords (reserved words in Python) and identifiers (names given to variables, functions, classes, etc.).
There are 35 keywords in Python 3.8.2. The number of keywords varies based on the Python version.
- Keywords are the reserved words in Python.
- Keyword cannot be used as a variable name, function name or any other name identifiers.
- Keywords are fundamental part of Python language, its syntax and structure.
- Keywords, similar to variable or function names, are case-sensitive.
Only three keywords have the first letter as CAPITAL (
None), the rest is written in lower case (remember, keywords are case-sensitive)
All the keywords except
None are in lowercase and they must be written as it is. The list of all the keywords is given below.
Keywords in Python version 3.8.2
You can check the desired name against the list above easily to determine if it is a reserved keyword. This module allows a Python program to determine if a string or identifier is a keyword.
import keyword keyword.iskeyword(variable_name)
Trueif variable_name is reserved keyword
Falseif variable_name is NOT reserved keyword
#if keyword True #if not keyword False
An identifier is a name that the user gives to any user-created variables, functions, classes, decorators, generators etc.
Rule of thumb for identifiers nomenclature :
- There is NO limit on the length of an identifier name. It can be a character or 1000.
- Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore
my_awesome_coding_skills, all are valid examples.
- Identifiers can start with
_, for example;
__variableis viable but it cannot start with a number, for example; 22variable or 22_variable is not viable identifiers and Python throws a syntax error
- Python reserved keywords cannot be used as identifiers.
#code class = 2020 #Output File \"<interactive input>\", line 1 class = 2020 ^ SyntaxError: invalid syntax
- We cannot use special symbols like !, @, #, $, % etc. in our identifier.
#Code group@ = 3 #Output File \"<interactive input>\", line 1 group@ = 3 ^ SyntaxError: invalid syntax