codescracker
python

Python Identifiers



As name suggests, identifier is a name that identifies an object in a Python program. Object may be a variable, class, function etc. For example:

var = "codescracker.com"
a = 100
b = 200

def myfun():
    print(var)

class myclass:
    def sum(self, num_one, num_two):
        return num_one+num_two

myfun()
obj = myclass()
print("Sum = ", obj.sum(a, b))

In above program, the list of identifiers are:

where myfun and sum are the two identifiers used to name functions, myclass is another identifier used to name class, and all others are used to name variables. Basically, identifiers are building-block of a Python program. The output produced by above program should exactly looks like:

python identifiers example

Don't worry about the code, just look at the identifiers only. You'll learn all the things like functions, classes etc. in upcoming chapters one by one.

Important - Identifiers should not be a keyword.

Note - Python is a case-sensitive programming language. Case-sensitive means, num, Num, NUM, nUm, nuM are all different variables or identifiers.

How to Name an Identifier in Python ?

To name an identifier in Python, we can use the combination of letters (a-z, A-Z), numbers (0-9) and underscore (_). For example, codescracker, codes_cracker, codes12cracker_, codes123 etc.

Rules to Name Identifiers in Python

Here are the list of rules that must be followed while naming or creating a variable, class, function etc. in Python:

Python Identifiers Example

Let's create a program in Python proves that Python is a case-sensitive language. That is, two identifiers say num and Num gets treated as two different identifiers by the compiler:

num = 11
Num = 12
NUM = 13
nUm = 14
nuM = 15

print(num)
print(Num)
print(NUM)
print(nUm)
print(nuM)

The snapshot given below shows the exact output produced by above Python program:

python identifiers

You see! all the identifier prints different value, but the name is same if their case gets ignored. If Python is not a case-sensitive language, then the output should be 15, five times. That is, if you replace the above program with the program given below:

num = 11
num = 12
num = 13
num = 14
num = 15

print(num)
print(num)
print(num)
print(num)
print(num)

Then the output must be equal to the snapshot given below:

identifiers in Python

Python Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker