python identifier

m umar
Artificial Intelligences Technology
3 min readFeb 6, 2021

A python identifier is a name for variable, function, class or module. when we create something while coding we have to name it to differentiate from other code entities, so python provides us a set of rules for naming these coding entities, These rules also known as identifiers rules.

python identifier rules:

  • identifier are the combination of Small(a to z) or Capital(A to Z) letter or underscore or digits (0 to 9) like Robot,machine_007,Neuron etc
  • identifier cannot start with digit like 0DataScience is invalid identifier because it is starting with digit
  • key word or reserved word cannot use as identifier like if, else, while, for etc
  • Special character or symbols(!,@,# etc) cannot use as identifier like @machine or rob!ot etc. are invalid identifiers.
  • can be of variable length(Number of character in identifier) i.e. there is no restriction of length.
  • Python is a case sensitive language, so variable and VARIABLE are different identifiers.

Python Naming Conventions

  1. Class name start with Upper case letter (A to Z) and follow CamelCase Convention.
  2. Python’s build in classes are typically lowercase words
  3. Exception Classes should end with “Error”
  4. Variable name start with lower case (a to z)
  5. If a variable is private then start with a single leading underscore(_) like _Machine
  6. Starting a variable with two leading underscore indicates strongly private variable
  7. If a variable ends with two trailing underscores then it is a language defines spacial name.
  8. Methods name are lower case words
  9. Constants should be all capitalize
  10. Words in a constant name should be separated by an underscore

python Identifier examples:

  • Valid Variable identifier
var=307
My_var=120
My_var344=120
STRING="UMAR"
fLoat=3.14
  • functions identifier
def my_function():
print("This is valid function name 1")
def My_function():
print("This is valid function name 2")
def Myfunction344():
print("This is valid function name 3")
def FUNCTION():
print("This is valid function name 4")
#Calling
my_function()
My_function()
Myfunction344()
FUNCTION()
  • class identifier
class MyClass:
x = 11
class My_Class:
x = 5
class My_Class344:
x = 52
class ClassName344:
x = 5
class CLASSNAME:
x = 5

check whether a string is valid identifier or not:

we can check whether a string is valid identifier or not with built in method isidentifier(). If we are not sure about a string whether it is valid identifier or not then we must use isidentifier() method to verify, if this method returns true then string is valid identifier otherwise given string cannot be used as identifier. Look at following example.

print("var".isidentifier())        #True
print("My_var".isidentifier()) #True
print("My_var344".isidentifier()) #True
print("STRING".isidentifier()) #True
# some invalid identifiers example
print("5var".isidentifier()) #False (cannot start with a number)
print("@var".isidentifier()) #False (cannot start with a special character)
print("55".isidentifier()) #False (cannot start with a number)

Exception in isidentifier() method:

There is exceptional case with isidentifier() method, which is if we try this method with keyword or reserved words then it will return true which is wrong output as

we cannot use keywords as identifiers in python.

print("for".isidentifier())        #True
#for is a keyword

So wat’s the solution of above exception. we can check whether a string is a keyword or not before checking whether it is an identifier or not. python provide us a built in method keyword.iskeyword(“string”) to check for key word validation. Look at following example

import keyword
print(keyword.iskeyword("for")) #True

Now we can use both method for identifiers verification.

import keyword
def is_Valid_Identifier(s):
if (s.isidentifier() and not keyword.iskeyword(s)):
print(s,"is Valid Identifier")
else:
print(s,"is not Valid Identifier")
is_Valid_Identifier("for")
is_Valid_Identifier("var")

Python Basic Syntax:

if else in python:

functions in python:

--

--

m umar
Artificial Intelligences Technology
0 Followers

i am python expert and machine learning engineer do follow me if you want to learn python, machine learning and deep learning.