Variables and Constants In Python


A variable in Python is used to store a value in the memory. The value of a variable can be changed later in the program. In Python programming, a variable is created when you first assign a value to it.

For example –
num = 10

place = "New Delhi"

Here num and place are two different variables one store a number which is 10 and the other one stores a string called New Delhi.

How a variable’s value is changed?

The value stored in a variable is not fixed it can be different for different stages of a program.

For example –

place="New Delhi"
print(place)
#Assigning a new value to the variable place
place="Mumbai"
print(place)

Now when you save and execute this program on your system this will display the output as given in the image below –

As you can see here earlier the place variable contains the value New Delhi and later it was replaced by Mumbai.

Rules and naming convention for variables in Python

There are certain rules in Python to use variable names –

  • A variable name must start with a letter or underscore, numbers or digits are not allowed at the starting
  • It should only contain the alphanumeric characters and underscore i.e. A-Z, a-z,0-9 and
  • The variable name is case sensitive that means NAME and name are two different variables in Python
  • It is recommended to use a descriptive name of a variable for example age make more sense than a
  • Using Python keywords as the variable name is not allowed

Examples of variables in Python

Following are the examples of some valid variables in Python.

_emp_id
name_of_employee
age
SALARY
Contact

The example of some illegal variable names that are not allowed in Python are –

1var
var-name
Var name

You can declare a multi-word variable name in the following ways, this will enhance the code readability.

You can separate each word with an underscore called Snake case.

name_of_emp = "Sumit"

Start the first letter of each word in capitals except the first one this is known as the Camel case

nameOfEmp = "Sumit"

Start each word in capitals this is known as the Pascal case

NameOfEmp = "Sumit"

Constant in Python

A constant is a variable in Python whose value is fixed during the program execution that means it can not be changed later in the program.

Example of declaring and assigning a value to constant in Python

Following is the example of declaring a constant in Python and assigning a value to it.

constant.py

PI = 3.14
GRAVITY=9.8

You can declare them in a separate python file let’s say constant.py and then import and use them in your main.py module

main.py 

import constant
print (constant.PI)
print (constant.GRAVITY)

When you execute main.py this will display the output as given in the image below.

Always declare a constant name in capital letters as we do in the above example.

Please note that in reality there is no way to declare a constant in Python I mean there is no keyword to declare constants like we do use const in C or C++. So it doesn’t actually prevent reassignment.

Now try these examples on your own system. If you find any problem then leave it in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.