Skip to content
Home » Programming » Variables, Constants and Literals in Python

Variables, Constants and Literals in Python

variables, constants and literals in python

In this post, we will explore about the Variables, Constants and Literals in Python and the way of using them in a program.

Python Variables

A variable is a named, reserved memory locations to store values. This means that when we create a variable we reserve/allocate some space in memory. Simply, the variables can be taken as the container that holds data and can be modified later in the program. For example,

number = 7

Firstly, we created a variable as number. Secondly, we assigned the value to the variable as 10.

We can also consider of variables as a school bag to store books in it and that book can be replaced at any time. It can be illustrated as below.

number = 7
number = 8
number = 9

Initially, the value of the variable number was 7. Later, we modified it to 8 and again, we changed it to 9.

Assigning values to Variables in Python

As we can see from the above example, we can simply use the assignment operator “=” to assign a value to a variable.

For example,

–> Declaring and assigning value to a variable

number = 7

In the program above, we assigned a value 7 to the variable number. Then, we end the program printing out the value assigned to the variable i.e 7.

The beneficial point in Python programming language is that it is a type-inferred language, so we don’t have to explicitly define the variable (data) type. It automatically recognizes that 7 is an integer value and declares the variable as integer.

–> Assigning multiple values to multiple variables

first, second, third = 6, 11.11, "DroxElement"

#If we need to assign same value to multiple variables at once we can do this as below.

print("\n Same value ------")
fourth = fifth = sixth = "DroxElement"

 Same value ------

Python Constants

A constant is a variable type whose value cannot be changed. It can be taken as the containers that holds data and that data cannot be changed later in the program execution.

Assigning value to constant in Python

In Python, constants are usually declared and assigned in a different module from the main module. Here, the different module is a new file containing variables, functions, etc which is imported to the main module/file. For example,

–> Declaring and assigning value to a constant

Create a module named

pi = 3.14
gravity = 9.8

Create a main file

import constant


In the above program, firstly we created a module file. After that, we assigned the constant value 3.14 and 9.8 to pi and gravity respectively. Then, we created a file and imported the constant module.

import constant

And then, finally we ended the program printing the constant value. In reality, we use Constants in less amount as compared to the variables in Python.

Also Read : Calculate Simple Interest and Compound Interest in Python

Rules for naming Variables and Constants in Python

  1. Constant and variable names should have a combination of letters (a to z) and digits (0 to 9) and joined together with underscore ( _ ) if separated. For example,

2. Don’t start a variable or constant name with a digit.

3. Never use special symbols like !, @, #, $, %, etc.

4. If possible, use capital letters to declare a constant (that will differentiate them from variables).

5. Create a name that make sense.


Literals are the raw data that are given in a variable or constant. In Python, there are numerous types of literals. Some of them are as follows:

Numeric Literals

Numeric literals are not changeable (immutable). For instance, they belong to 3 different numerical types:

  • Integer : Numbers can be both positive and negative with no fractional part. For example, 100
  • Float : Real numbers with both integer and fractional part. For example, -200.5
  • Complex : In the form of a + bj where a forms the real part and b forms the imaginary part of the complex number. For example, 5 + 3.14j

–> How to use Numeric literals in Python?

d = 0b1010 #Binary Literal
r = 700    #Decimal Literal
o = 0o310  #Octal Literal
x = 0x12c  #Hexadecimal Literal

#Float Literal
E = 9.8
L = 6.62e9

#Complex Literal
ement = 5 + 3.14j

print("\n Integer Literals")
print(d, r, o, x)
print("\n Float Literals")
print(E, L)
print("\n Complex Literals")
print(ement, ement.imag, ement.real)
Integer Literals
10 700 200 300

 Float Literals
9.8 6620000000.0

 Complex Literals
(5+3.14j) 3.14 5.0

String Literals

A string literal is a collection of characters enclosed with inverted commas (quotes). And character literal is a single character enclosed with quotes.

–> How to use String literals in Python?

text = "DroxElement"  #String Literal
char = "D"                   #Character Literal
mul = """Welcome
DroxElement"""           #Multi-line String1
mul2 = 'Hello\
DroxElement'               #Multi-line string2





Boolean Literals

A boolean literal can only have any of these two values: True or False

–> How to use Boolean Literals in Python?

d = True  
e =  False

print("\n d is ",d)
print("\n e is ",e)
d is  True

 e is  False

In the above program, we used boolean literals where True represents the value as 1 and False as 0.

Special Literals

Python contains one special literal i.e None; which is used to specify that the field has not been created.

–> How to use Special Literals in Python?

var = 10
var2 = None


Literal Collections

Moreover, Python provides four different types of literal collection such as list, tuple, dict, and set literals.

  • List : It contains items of different data types. They are mutable (changeable). The values stored in list are separated by comma (,) and enclosed within square brackets ([ ]).
  • Dictionary : It stores the data in the key-value pair. It is enclosed by curly-braces ({ }) and each pair is separated by the commas (,).
  • Tuple : It is a collection of different data type. It is immutable and is enclosed by the parenthesis () and each element is separated by comma (,).
  • Set : It is the collection of the unordered data set. It is enclosed by the curly-braces ({ }) and each element is separated by comma (,).

–> How to use Literals collection in Python?

fruits = ["apple", "mango", "orange"]      #list
numbers = (1, 2, 3)                                  #tuple
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'}                     #set

['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}

Leave a Reply

Your email address will not be published. Required fields are marked *