Variables can be viewed as containers in memory where we store data. Each such container has a name and an object that is stored there. The name of a variable is also called an identifier.

ExampleEdit & Run
student = "Mike"
Output:
[Finished in 0.01082673599012196s]

In the above example, student is the variable's identifier/name,  while "Mike" is the data stored in the variable.

There are rules that we should follow when creating valid identifiers. The rules are outlined below:

  • An identifier cannot be a python keyword.
  • Identifier are case-sensitive
  • An identifier can only be made up of alphanumeric characters and underscores, i.e A-Z , a-z, 0-9, _
  • The first letter of an identifier can be a letter or an underscore(A-Z, a-z, _) but not a digit.
  • Whitespaces and other special symbols(except underscores) are not allowed in identifiers.

 An identifier cannot be a Python keyword

Keywords are reserved words that are used to perform some essential operations in Python.Some of the keywords are shown below;

  • if
  • else
  • for
  • while
  • def
  • break
  • continue
  • and others

Using a keyword as an identifier will immediately lead to a SyntaxError exception being raised.

ExampleEdit & Run

keyword as an identifier leads to a syntax error

break = False

There are over 60 keywords in python, you can view all of them here.

Identifier are case sensitive

Identifiers being case sensitive means that student, Student and  STUDENT are all distinct and separate identifiers. 

ExampleEdit & Run
student = 'Mike'

print(student) #works

print(Student)#fails
Output:
MikeTraceback (most recent call last):  File "<string>", line 5, in <module>NameError: name 'Student' is not defined. Did you mean: 'student'?[Finished in 0.010055249091237783s]

In the above example, a NameError exception is raised in the second print statement because Python interprets Student as a separate identifier from student. It fails to locate any variable with Student as its identifier and it therefore raises the NameError exception.

An identifier can only be made up of alphanumeric characters and underscores

Alphanumeric characters refers to all letters both lowercase and uppercase as well as digits from 0 to 9. These, together with an underscore(_) are the only allowed characters in an identifier.

ExampleEdit & Run

Example of valid identifiers 

student1 = 'Mike'
STUDENT2 = 'John'
student_3 = "Mary"

print(student1)
print(STUDENT2)
print(student_3)
Output:
MikeJohnMary[Finished in 0.009759127162396908s]

Presence of any other character out of the ones said above will automatically lead to a SyntaxError exception, this is including whitespaces.

ExampleEdit & Run
student-1 = 'John'
Output:
  File "<string>", line 1    student-1 = 'John'    ^^^^^^^^^SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?[Finished in 0.010786562226712704s]

In the above case we used a hyphen which is not an allowed character thus leading to the SyntaxError exception being raised.

The first letter of an identifier can be a letter or an underscore

While digits(0-9) are allowed in identifiers, they cannot appear as the first character of an identifier. Thus only a letter or an underscore can be used as the first character.

ExampleEdit & Run

valid identifiers

student = "Mike" #starts with a lowercase letter
Age  = 19 #starts with an uppercase letter
_school = 'Reeds' # starts with an underscore 

print(student)
print(Age)
print(_school)
Output:
Mike19Reeds[Finished in 0.00948834395967424s]
ExampleEdit & Run

invalid identifiers

1student = 'John'
%student = 'Mike'
Output:
  File "<string>", line 1    1student = 'John'    ^SyntaxError: invalid decimal literal[Finished in 0.010028131073340774s]

Good practices when naming variables

You can name a variable whatever you want as long as the above rules are followed. However, there are some conventions which when followed will help in ensuring the readability and maintainability of your code and the entire program. Some of the good practices are listed below:

  • Give your variables a descriptive name that is relevant to the data stored in it.
  • Generally avoid single character variables except in special cases where using longer identifiers may lead to redundancy, such as in loop counters.
  • Declare global constants with uppercase letters to easily identify them. Constants are variables that are not intended to be changed in the entire program execution
  • When using multiple-word variable names you can use two approaches:
    1. camelCase -  In the camelCase approach the first word starts with a lowercase letter and all the subsequent words are capitalized for example, firstName, middleName, etc.
    2. pothole_case - In the pothole approach the words are lowercased and are separated with an underscore, for example, first_name, middle_name, etc.