A variable is just an identifier/name used to refer a particular value stored in a memory. In Python, a value stored in the memory is nothing but an object, hence
a variable could also be known as a reference, pointing to a specific object value in the memory.
Although a variable can only point to one specific object value at one time, a variable as the name says, can vary and refer to different objects from
time to time.
Python has specified some rules which should be followed in order to give a variable name in a Python program.
Although, we have already discussed these rules in our article identifiers and keywords,
for those who haven't read it, here's a recap.
Rules to name a variable/identifier in Python
An identifier must start with an alphabet.
An identifier cannot start with a digit.
An identifier must not contain a space or a comma(,) or even a dot(.)
A multi-word identifier should be separated by an underscore(_)
An identifier could be a mix of lowercase(a-z), uppercase(A-Z) alphabets, digits(0-9) or an underscore(_).
An identifier could be of any length.
An identifier cannot contain special characters like #,@,&,%.
Python is a case-sensitive language, hence identifier such as name is different from Name.
Python keywords i.e. reserved words, should not be used as identifiers.
Let's see an example of how we can declare a variable with a value in a program and how we could make it refer to a new value object.
# Python - Valid identifiers following naming conventions
# Declaring identifier/variable size with value 1000
size = 1000;
print('Value referenced by variable size is : ', size)
# Modifiying identifier/variable size to refer to value 9.9
size = 9.9;
print('Value referenced by variable size is : ', size)
# Modifiying identifier/variable size to refer to value Extra Large
size = 'Extra Large'
print('Value referenced by variable size is : ', size)
# Modifiying identifier/variable to refer to value L
size = 'L'
print('Value referenced by variable size is : ', size)
Output
Value referenced by variable size is : 1000
Value referenced by variable size is : 9.9
Value referenced by variable size is : Extra Large
Value referenced by variable size is : L
Constants
Constant is a value that remains the same and does not change or it cannot be
modified. Python does not allow us to create constants as easy as we do in C, C++ or Java.
In Python, we can define constants in a module, where a module is a file which could be imported to access the defined constants and methods.
Note: A module constant should be in uppercase alphabets, where multi-word constant names are separated by an underscore(_).
Let's look at an example to define a constant in a module. For this, we are going to create a module named misc i.e.
a file named misc.py, and in this module we are going to define some constants.
Creating a module - misc
# Python - Defining constants in a module/file
APPLE = 'RED'
GRAVITY = 9.8
PLANETS = 8
Let's import the module misc, in order to use its constant values.
# Python - Defining constants in a module/file
import misc
print('Value of constant APPLE : ',misc.APPLE)
print('Value of constant GRAVITY : ',misc.GRAVITY)
print('Value of constant PLANETS : ',misc.PLANETS)
Output
Value of constant APPLE : RED
Value of constant GRAVITY : 9.8
Value of constant PLANETS : 8
Declaring constants like that can be modified. Let us see how.
Advertisement
Trying to modify a constant value
Let's try to modify constant values defined in our module misc i.e. a file named misc.py and see what happens.
Cons.c
# Python - Trying to modify the value of constants in a module/file
import misc
misc.APPLE = 'Pink'
misc.GRAVITY = 20
misc.PLANETS = 10
print('Modified value of constant APPLE : ',misc.APPLE)
print('Modified value of constant GRAVITY : ',misc.GRAVITY)
print('Modified value of constant PLANETS : ',misc.PLANETS)
Output
Modified value of constant APPLE : Pink
Modified value of constant GRAVITY : 20
Modified value of constant PLANETS : 10
As you can see in the output of the last program, the constants defined this way in a module can be changed.
Now, let's show you another way to define constants
which cannot be changed, by defining constants within the methods of a module. Let us see how it is done.
Creating constants in a module that cannot be modified
In order to define constants that cannot be modified just by importing the module they are contained in, we will have to
define each constant within a separate method of its module. Let's see how it is done.
Creating a new module - misc2
# Python - Defining constants in a module/file
def get_constant_apple()
APPLE = 'Red'
return APPLE
def get_constant_gravity()
GRAVITY = 9.8
return GRAVITY
def get_constant_planets()
PLANETS = 8
return PLANETS
Let's import the module misc, in order to use its constant values.
# Python - Defining constants in a module/file
import misc2
ob = misc2()
print('Value of constant APPLE : ',ob.get_constant_apple())
print('Value of constant GRAVITY : ',ob.get_constant_gravity())
print('Value of constant PLANETS : ',ob.get_constant_planets())
Output
Value of constant APPLE : RED
Value of constant GRAVITY : 9.8
Value of constant PLANETS : 8
Defining each constant within a separate method of its module restricts their direct access and thereby allows us to define constants which cannot be modified.