in ,

Python Variables, Constants, and Literals

Python Variables, Constants, and Literals

In this tutorial, you will learn about Python variables, constants, literals, and their use cases.

Python Variables

A variable is a named area used to store data in the memory. It is useful to think of variables in a compartment that holds data that can be changed later in the program. For instance,

number = 10

Here, we have created a variable named number. We have assigned the value 10 to the variable.

You can think of variables as a bag to store books in it and that book can be replaced at any time.

number = 10
number = 1.1

Initially, the value of the number was 10. Later, it was changed to 1.1.

Note: In Python, we don’t actually assign values to the variables. Rather, Python gives the reference of the object(value) to the variable.

Assigning values to Variables in Python

As you can see from the above example, you can use the assignment operator = to assign a value to a variable.

Example 1: Declaring and assigning value to a variable

website = ""


In the above program, we allocated a worth to the variable site. At that point, we printed out the worth doled out to site for example

Note: Python is a type-inferred language, so you don’t need to unequivocally characterize the variable kind. It naturally realizes that is a string and pronounces the size variable as a string.

Example 2: Changing the value of a variable

website = ""

# assigning a new variable to website
website = ""



In the above, the program, have assigned to the website variable initially. then, the value is changed to

Example 3: Assigning multiple values to multiple variables

a, b, c = 5, 3.2, "Hello"

print (a)
print (b)
print (c)

If we want to assign the same value of multiple variables at once, we can do this as:

x = y = z = "same"

print (x)
print (y)
print (z)

The second program assigns the same string to all the three variables x, y, and z.


A constants sort of factor whose value can’t be changed. It is useful to consider constants compartments that hold data that can’t be changed later.

You can think constants as a bog store a few books which can’t be supplanted once positioned inside the bog.

Assigning a value to constant in python

In Python, constants are generally proclaimed and doled out in a module. Here, the module is another document containing factors, capacities, and so on which are imported to the fundamental record. Inside the module, constants are written in every single capital letter and underscores isolating the words.

Example 3: Declaring and assigning value to a constant

Create a

PI = 3.14

Create a

import constant




In the above program, we make a module record. At that point, we dole out the steady an incentive to PI and GRAVITY. From that point forward, we make a document and import the steady module. At long last, we print the constant value.

Note: in reality, we don’t use constants in Python. Naming them in every capital letter is a show to isolate them from factors, be that as it may, it doesn’t actually prevent reassignment.

Rules and Naming Convention for Variables and constants

1. Consistent and variable names ought to have a blend of letters in lowercase (start to finish) or capitalized (beginning to end) or digits (0 to 9) or an underscore (_). For instance:


2. Make a name that makes sense. For instance, vowel makes more sense than v.

3. If you need to make a variable name having two words, use underscore to separate them. For instance:


4. Use capital letters possible to declare a constant. For example:


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

Don’t start a variable name with a digit.


Literal is a raw data given in a variable or constant. In Python, there are various types of literals they are as follows:

Numeric Literals

Numeric Literals are imputable  (unchangeable). Numeric literals can have a place with 3 distinctive numerical sorts: Integer, Float, and Complex.

Example 4: How to use Numeric literals in Python?

a = 0b1010 #Binary Literals
b = 100 #Decimal Literal 
c = 0o310 #Octal Literal
d = 0x12c #Hexadecimal Literal

#Float Literal
float_1 = 10.5 
float_2 = 1.5e2

#Complex Literal 
x = 3.14j

print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)


10 100 200 300
10.5 150.0
3.14j 3.14 0.0

In the above program,

We assigned integer literals into various factors. Here, a is binary literal, b is a decimal strict, c is an octal strict and d is hexadecimal literal.

When we print the variables, all the literals are changed over into decimal values.

10.5 and 1.5e2 are floating-point literals. 1.5e2 is expressed with exponential and is equivalent to 1.5 * 102.

We assigned a complex literal i.e 3.14j in factor x. At that point, we utilize fanciful strict (x.imag) and genuine exacting (x.real) to make nonexistent and genuine pieces of complex numbers.

To learn more about Numeric Literals, refer to Python Numbers.

String literals

A string literal is a sequence of characters surrounded by quotes. We can use both single, double, or triple quotes for a string. And, a character literal is a single character surrounded by single or double-quotes.

Example 7: How to use string literals in Python?

strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"



This is Python
This is a multiline string with more than one line code.
raw \n string

In the above program, This is Python is a string literal and C is a character literal.

The value in triple-quotes ” assigned to the multiline_str is a multi-line string literal.

The string u”\u00dcnic\u00f6de” is a Unicode literal which supports characters other than English. In this case, \u00dc represents Ü and \u00f6 represents ö.

r”raw \n string” is a raw string literal.

Boolean literals

A Boolean literal can have any of the two values: True or False.

Example 8: How to use boolean literals in Python?

x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)


x is True
y is False
a: 5
b: 10

In the above program, we use boolean literal True and False. In Python, True represents the value as 1 and False as 0. The value of x is True because 1 is equal to True. And, the value of y is False because 1 is not equal to False.

Similarly, we can use the True and False in numeric expressions as value. The value of a is 5 because we add True which has a value of 1 with 4. Similarly, b is 10 because we add the False having value of 0 with 10.

Special literals

Python contains one special literal i.e. None. We use it to specify that the field has not been created.

Example 9: How to use special literals in Python?

drink = "Available"
food = None

def menu(x):
    if x == drink:




In the above program, we define a menu function. Inside the menu, when we set the argument as a drink then, it displays Available. And, when the argument is food, it displays None.

Literal Collections

There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.

Example 10: How to use literals collections 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'}

In the above program, we created a list of fruits, a tuple of numbers, a dictionary dict having values with keys designated to each value, and a set of vowels.

To learn more about literal collections, refer to Python Data Types.

Please feel free to give your comment if you face any difficulty here.

For More Latest Articles Click on Below Link

salman khan

Written by worldofitech

Leave a Reply

Python Statement, Indentation and Comments

Python Statement, Indentation, and Comments

Python Data Types

Python Data Types