While studying about programming in python, you must have encountered certain phrases like keywords, variables, constants and literals. In this article, we will learn about variables and constants and we will study the underlying concepts for their definition and usage in python.
What are variables in Python?
A variable in python is a name which is used to refer to an object in the memory. Variables are also termed as references because they refer to a specific object in the memory.
For example, the variable myNum
given in the following code snippet will refer to an object of type integer which contains 1117 as its value.
myNum = 1117
We can assign a value to a variable. Likewise, we can also assign a variable to a variable as follows.
myNum=1117
anotherNum=myNum
When we assign a variable to a variable, both the variables start pointing to the same object in the memory. This can be verified using the id() method which gives an unique identifier for every object.
myNum = 1117
anotherNum = myNum
print("id of myNum is:", id(myNum))
print("id of anotherNum is:", id(anotherNum))
Output:
id of myNum is: 140209154920336
id of anotherNum is: 140209154920336
In the above example, we can see that id of both the variables is same which confirms that both the variables refer to the same object.
We can also modify values in the variables. While modifying values for mutable data types, the new value is assigned to the same object. For example, If we modify the value for a key in the python dictionary given in the following example, the dictionary will remain the same. This can be verified from the id of the dictionary before and after the modification.
myDict = {1: 2}
print("Id of myDict before modification:", id(myDict))
myDict[1] = 4
print("Id of myDict after modification:", id(myDict))
Output:
Id of myDict before modification: 140457374471040
Id of myDict after modification: 140457374471040
When we modify values assigned to immutable data types like integers, a new object is created and assigned to the variable. This can be seen in the following example.
myNum=1117
print("Id of myNum before modification:", id(myNum))
myNum = 1118
print("Id of myNum after modification:", id(myNum))
Output:
Id of myNum before modification: 140485377224528
Id of myNum after modification: 140485378289104
In the above example, we can see that the id of the myNum variable has changed. This confirms that a new object has been created during assignment of new value to myNum.
Conventions for defining Variables in Python
A variable name in python always starts with an alphabet. We can start a variable with capital or small case letters but it should never start with a number or any special character.
name="PythonForBeginners" #correct variable name
Name="PythonForBeginners" #correct variable name
2name="PythonForBeginners" #incorrect variable name
#name="PythonForBeginners" #incorrect variable name
A variable can have underscore _ character but it should never have any other special character like #,@,$,%,&,!.
my_name="PythonForBeginners" #correct variable name
my&name="PythonForBeginners" #incorrect variable name
When a variable name consists of multiple words, we can use camelCase naming convention. In camelCase convention, we start a variable with a small letter and each word in the variable is started using capital letters as follows.
myName="PythonForBeginners" #correct variable name
We can also use underscore to separate different words in a variable name as follows.
my_name="PythonForBeginners" #correct variable name
Python is a case sensitive language. It means that variable names having the same spelling but different cases will refer to different objects.
myNum=1117
MyNum = 1118
print("myNum is:",myNum)
print("MyNum is:",MyNum)
Output:
myNum is: 1117
MyNum is: 1118
Keywords should not be used as variable names in python, otherwise error occurs during execution of the program.
in =1117 #incorrect variable name
What are constants in Python?
Constants are literals which contain a value which is not supposed to be changed during the execution of the program.
In python, variables and constants are not differentiated from each other by the interpreter. In a program, we differentiate a variable from a constant by using the naming conventions.
A constant in python is defined using only uppercase letters and underscores. Generally, constants are defined in a module and then they are imported into a program whenever we need to use them. For example, we can use the constant PI from the cmath module after importing it as follows.
import cmath
print("Value of PI is:",cmath.pi)
Output:
Value of PI is: 3.141592653589793
We can also modify a constant after importing it as follows.
import cmath
print("Value of PI is:", cmath.pi)
cmath.pi = 1117
print("modified value of PI is:", cmath.pi)
Output:
Value of PI is: 3.141592653589793
modified value of PI is: 1117
Being able to modify the value in a constant entirely violates the idea behind constants. To avoid this, there should be getter functions defined in the modules to access the constants. This will restrict the user to modify the values.
Conclusion
In this article, we have studied variables and constants in python. We have also seen the conventions for naming the variables and constants in python.We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way . Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.