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.
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.
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.
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.
Presence of any other character out of the ones said above will automatically lead to a SyntaxError
exception, this is including whitespaces.
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.
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:
- 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. - pothole_case - In the pothole approach the words are lowercased and are separated with an underscore, for example,
first_name
,middle_name
, etc.
- camelCase - In the camelCase approach the first word starts with a lowercase letter and all the subsequent words are capitalized for example,