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"
#Assigning a new value to the variable 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.


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

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.

PI = 3.14

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

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

When you execute 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.