In this article, we will discuss identifiers & reserved keywords in Python in detail
- name used to define variable
- for example, x=10
- name used to define method or function
- for example, def f1():
- name used to define class
- for example, class Test(Exception):
Rules to define identifiers in Python:
- Alphabets in uppercase (A-Z) & lowercase (a-z) and Digits (0 to 9) and Underscore (_) are allowed but no other special characters are allowed like $, #, etc.
- Identifiers should start with alphabets and not with digits
- Identifiers are case-sensitive i.e.; total & TOTAL are 2 different identifiers
- Reserved or pre-defined keywords are not allowed to define identifiers
- There is no upper limit on the length of characters for defining identifier in Python but it is not recommended to use long identifiers names as a programming practice
Identifiers starting with underscore:
- If identifiers starts with single underscore (_) then it is considered as private
- If identifiers starts with 2 continuous underscore (__) then it is considered as strongly private
- If identifiers starts with 2 continuous underscore (__) and ends with 2 continuous underscore (__) then it is considered as language specific identifiers and it is special variable defined by Python itself
There are 33 reserved keywords in Python,
- Boolean values :- True, False, None
- Logical operator :- and, or, not, is
- Conditional statement :- if, else, elif
- Flow controller :- while, for, break, continue, return, in, yield
- Exception handling :- try, except, finally, raise, assert
- Miscellaneous :- import, from, as, class, def, pass, global, nonlocal, lambda, del, with
Note :- all reserved keywords contains only lower-case alphabets except for Boolean values where starting letter is upper-case alphabet
How to list all reserved keywords in Python terminal ?
- First we need to import keyword
- Then invoke keyword.kwlist in the terminal
- It prints all reserved keyword in square bracket
Happy Learning !!
Happy Coding !!