Python - Things to know before proceed - Identifiers Tutorial
An identifier is used to identify a class, function, variables, modules, or another object.
Rules for writing identifiers:
-
Identifiers are a combination of uppercase (AB…), lowercase(ab…), digits(123…), and underscore (_).
It does not include any type of special symbol like ($,%,&,*,…)
Valid:- class_1, var1, MainClass
Invalid:- _class1,1var, class@, var$
-
Identifier cannot start with digit.
Invalid:- 1_class,1var
-
A keyword cannot be used as an identifier.
Invalid:- pass = 1;
-
Python is case-sensitive. Hence var_1 and Var_1 are different from each other.
Valid and Invalid identifiers:
Valid Identifier |
Invalid Identifier |
Comment |
false |
False |
“False” is Boolean value or Keyword,1st letter of Boolean value should be capital. While “false” is not keyword and can be used as a valid identifier |
Else |
else |
Else is not a keyword as it is capitalize. While “else” is keyword and cannot be used as a identifier. |
_sam1 , sam1_ |
sam+s , Sam. , vai@ |
Only underscore (special character ) is a valid identifier. |
sam_3 |
1sam_ , 33 |
Valid identifier cannot start with digit and can’t be only digit |
|
for , finally , is |
Reserved Keyword cannot be valid identifier |