- A name in python program is called identifier.
- Identifiers can be class name or function name or module name or variable name.
- Example a = 10, where a is identifier.
Identifiers naming
- The only allowed characters in python are;
- Alphabet symbols (either lower case or upper case)
- Digits (0 to 9)
- Underscore symbol (_)
By mistake if we are using any other symbol like $ then we will get syntax error.
Example
Python
cash = 10
print(cash)
Output
PowerShell
10
Example
Python
ca$h = 20
print(ca$h)
Output
PowerShell
SyntaxError: invalid syntax
- Identifier should not starts with digit.
Example
Python
123total = 10
print(123total)
Output
PowerShell
SyntaxError: invalid decimal literal
Example
Python
total123 = 10
print(total123)
Output
PowerShell
10
- Identifiers are case sensitive. Of course python language is case sensitive language.
Python
total=10
TOTAL=999
print(total)
print(TOTAL)
Output:
PowerShell
10
999
- We cannot use reserved words as identifiers.
Python
def = 10
Output
PowerShell
SyntaxError: invalid syntax
- There is no length limit for python identifiers. But not recommended to use too lengthy identifiers.
Python
ABCDEFGHIJKLMNOPQRSTUVWXYS = 10
print(ABCDEFGHIJKLMNOPQRSTUVWXYS)
Output
PowerShell
10
Question
Which of the following are valid python identifiers?
Python
123total = 10
total123 = 10
java2share = 10
ca$h = 10
abc_abc = 10
def = 10
if = 10
Answer
PowerShell
123total = 10 ❌
total123 = 10 ✔
java2share = 10 ✔
ca$h = 10 ❌
abc_abc = 10 ✔
def = 10 ❌
if = 10 ❌
Remark
- If identifier starts with _ symbol then it indicates that it is private.
- If identifier starts with __(two under score symbols) indicating that strongly private identifier.
- If the identifier starts and ends with two underscore symbols then the identifier is language defined special name, which is also known as magic example __add__.
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!