Identifiers in Python

What is an Identifier?

The naming in Python is an Identifier. That is assigning a value. It can be a variable name, a function name, a class name, etc,

# Defining/declaring a variable
a = 10

# A function in Python
def func:
    x = 10
    return x

# A Class in Python
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

In the above code snippet, a , func and Person are identifiers. Don't bother about the code if you don't understand. We will cover them in detail in the upcoming blog articles. For now, let's focus on naming conventions in Python.

Rules that apply to create an Identifier

It cannot be a Python Keyword

A keyword in Python is a reserved word for specific purposes. For example if , if-else , def , etc, are some of the keywords.

Brief on Python Reserved words or Keywords

Python contains only 33 reserved words. These words represent some meaning or functionality and are called reserved words.

Nothing can have the same name as a reserved word.

The keywords in Python:

  • True, False, None

  • and, or, not, is

  • if, elif, else

  • while, for, break, continue, return, in, yield

  • try, except, finally, raise, assert

  • import, from, as, class, def, pass

  • global, nonlocal, lambda, del, with

These Keywords contains

  1. Only alphabets

  2. Only True, False, None are uppercase, others all are smallcase.

  3. Switch state/concept is not available in Python

  4. Do While is not available in Python

Identifier can contain

  1. a - z

  2. A - Z

  3. 0 - 9

  4. _ (an underscore)

    Following are the correct forms of naming

     city = 'Bangalore'
     City = 'Bangalore'
     CITY = 'Bangalore'
     city1 = 'Bangalore'
     city_1 = 'Bangalore'
     _city = 'Bangalore'
     _1city = 'Bangalore'
    

Cannot contain

  1. White space

  2. Special characters (other than underscore)

    For example:

# cannot contain white space
person name = 'Siddharth'

# Cannot contain special characters
last#name = 'Ballure'

# underscore can be used as
last_name = 'Ballure'

It cannot start with

  1. Digits

  2. Special Characters (other than underscore)

    For example:

# Digits
123list = []
1_name = 'Milind'

# Special Characters
!name = 'Siddharth'

Python is Case Sensitive programming language

name = 'Siddharth'
Name = 'Siddharth'
NAME = 'Siddharth'

All are different identifiers pointing to the same variable 'Siddharth'. If I declare different variables then it might be clear.

name = 'Siddharth'
Name = 'Milind'
NAME = 'Anand'

In the above snippet, it looks like the naming is the same but that's not the case. As Python is a case-sensitive programming language all three are different identifiers.

There is no limit on identifier length

# Usual naming convention
x = 10

# naming with no limit on lenth of identifier
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 10

You can name it as shown in the above snippet. It is very much possible. But that will not make any logical sense. So, it is important to choose a meaningful name.

All in all, the following are the ways in which an identifier can be declared.

language = 'Python'
Language = 'Python'
LANGUAGE = 'Python'
language1 = 'Python'
favorite_language = 'Python'
_language = 'Python'
_123language = 'Python'

Following are some of the advanced concepts and we will see them in the upcoming blogs but I just want you to know that these too fall under the naming convention and has different purpose and plays different function. As soon as you go from the intermediate level to advanced you will be required to use these. For details, we will deal with them later.

For comparison have a look at below code block.

advanced     # a NORMAL declaration of variable
_advanced    # PROTECTED variable, one underscore at the beginning
__advanced   # PRIVATE variable, two underscores at the beginning
__advanced__ # MAGIC variable, two underscores at beginning and two at the endinng

Click: Read more here about the advanced naming convention

I hope you enjoyed this article. I know it is somewhat confusing or maybe seems like too many rules. I can understand. As you start your small steps into learning Python it will become natural to you. With practice, it becomes second nature. Let's keep learning one step at a time.

Thank you! See you in the next post.