In this Python Keywords and Identifiers tutorial, we will discuss keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.).
Python Keywords
- Keywords are the reserved words in Python.
- Keywords are case-sensitive.
- We cannot use a keyword as a variable name, function name, or any other identifier.
- There are 35 keywords in Python 3.8.8. It may vary depending on your version in python
The list of all the keywords is given below.
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 |
#Get All Keywords list in Python
import keyword as kw
print(kw.kwlist)
print('\nTotal numbers of Keywords in Python : ', len(kw.kwlist))
Please read related topic
Python Introduction
Python Get Started
Python Basic Syntax
Python Identifiers
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.
Rules for writing identifiers:
- 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 _. Names like testClass, xyz_1 are valid examples.
- An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.
- Keywords cannot be used as identifiers.
Please look into the below examples
xyz_1 = 12
The above xyz_1 = 12 line is valid one.
global = 1
File "<ipython-input-4-89339e2d91ea>", line 1
global=1
^
SyntaxError: invalid syntax
The above piece of code will return error from python because we are assign value into reserved keywords
1variable = 1
File "<ipython-input-5-e5103f12584c>", line 1
1variable = 1
^
SyntaxError: invalid syntax
The above piece of code will return error from python because we are assign value into 1variable. It start with number.
Difference between Keywords and Identifiers:
KEYWORD | IDENTIFIER |
Keywords are predefined word that gets reserved for working progs that have special meaning and cannot get used anywhere else. | Identifiers are the values used to define different programming items such as variables, integers, structures, unions and others and mostly have an alphabetic character. |
Specify the type/kind of entity. | Identify the name of a particular entity. |
It always starts with a lowercase letter. | First character can be a uppercase, lowercase letter or underscore. |
A keyword should be in lower case. | An identifier can be in upper case or lower case. |
A keyword contains only alphabetical characters. | An identifier can consist of alphabetical characters, digits and underscores. |
They help to identify a specific property that exists within a computer language. | They help to locate the name of the entity that gets defined along with a keyword. |
No special symbol, punctuation is used. | No punctuation or special symbol exce |