Python Literals

What are Literals?

Literals are the fixed values assigned to variables in Python. They're displayed on the screen exactly they typed in the text editor and can hold multiple lines of text. These have a constant value and data type. In the below example, 21, "hello" and 1.50 are different kinds of literal available in Python.

			

my_var = 21 #Integer Literal

my_var = 'hello'    #String Literal

my_var = 1.50   #Float Literal

				

Literals are of five types in Python. These are listed as follows:

Types of Literals in Python

Python String Literals

The String is defined as a collection of one or more characters inside quotation marks. Strings are usually used to store text data. Note that strings are immutable in Python.

A string literal can be used like this:

			

my_str = 'hello'    #String Literal using single quotes
#or
my_str = "hello"    #String Literal using double quotes

				

The key difference between declaring a string literal using single or double quotes is that with single quotes, the usage of the double quotes is allowed inside the single quotes. For example [With single quotes]:

			

my_message = 'John greeted me by saying "Hi! How are you"'
>>> print(my_message)

				

Output:

John greeted me by saying "Hi! How are you."
			

Example, with double quotes,
my_message = "John greeted me by saying "Hi! How are you.""
print(my_var)

				

Output:

SyntaxError: invalid syntax

The triple quotes can be used if there is a need for single as well as double quotes, like

			

my_message = '''
'John greeted me by saying'
"Hi! How are you"
'''
print(my_message)

				

Output:

'John greeted me by saying' "Hi! How are you"

The triple quote is used to write multiline string literals in the above example.

If there are multiline strings in the code but we need the output to be in a single line, "\" can be used at the end of each line. It is as follows:

			

my_message = '''\
'John greeted me by saying \
"Hi! How are you"'\
'''
print(my_message)

				

Output:

'John greeted me by saying "Hi! How are you"'

"\" also works with single as well as double-quotes.

Python Numeric Literals

The Numeric Literals represent numbers in the code. These literals are further divided into three types:

Integer Literals - It includes positive or negative whole numbers. Integer can be of any length as there is no limit on the length of an integer in the Python programming language.

			

x = 10	#Integer Literal
print(type(x))

				

Output:

<class 'int'>

Python Float Literals - It includes real numbers represented in a floating-point representation. It is accurate up to 15 decimal points.

			

z = 5.5	#Float Literal
print(type(z))

				

Output:

<class 'float'>

Python Complex Literals - It includes numbers with a real and imaginary part.

			

y = 3 + 5j	#Complex Literal
print(type(y))

				

Output:

<class 'complex'>

Python Boolean Literals

It represents only two values, either True or False, and these literals are used in Conditional statements

			

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)

				

Output:

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

Literal Collections

These literals are used for assigning values to Lists, Tuples, Dictionaries, and Sets. These Literals are further divided into four types:

Python List Literals - A list is a collection of elements. These elements are listed inside [ ] square brackets separated by commas. Lists are mutable(modifiable). Lists can store elements of similar or different data types.

			

my_list = [1, 2.2, 'list']

print(my_list)  #print my_list

my_list_2 = [5,10,15,20,25,30,35,40]

print(my_list_2) 

				

Output:

[1, 2.2, 'list'] [5, 10, 15, 20, 25, 30, 35, 40]

Python Tuple Literals - Tuples are also a collection of elements of different or similar types. The tuple items are separated with a comma (,) and enclosed in parentheses (). We can not modify Tuples after their creation as they are immutable in nature. The size of the Tuple cannot grow or shrink after its creation.

			

my_tuple  = ("hi", "Python", 2)   

print (type(my_tuple))   

print (my_tuple)

				

Output:

<class 'tuple'> ('hi', 'Python', 2)

Python Dictionary Literals - Dictionary is a collection of different elements, written in an unordered manner in the form of Key:Value pairs. The elements are separated by commas "," and enclosed inside curly braces { }. As dictionary is mutable in nature, so it's size can grow and shrink according to the requirement.

			

my_dictionary = {1:'value','key':2}
print(type(my_dictionary))
print("d[1] = ", d[1])
print("d['key'] = ", d['key'])

				

Output:

<class 'dict'> my_dictionary[1] = value my_dictionary['key'] = 2

Python Sets - Set is defined by values separated by commas inside braces { }. It is an unordered collection of unique elements. Elements in a Set are not ordered. An item only appears once inside a set, no matter if it has been written multiple times inside the Set. Being mutable in nature, Set can also grow and shrink as per requirement.

			

my_set = {5,10,11,2,1}

print("my_set = ", my_set)

print(type(my_set))

				

Output:

my_set = {1, 2, 5, 10, 11} <class 'set'>

Special Literals

Python programming language has one special literal, called None Literal.

None Literal

It is used as a placeholder and for initializing objects and variables.

			

apple = "Available"
orange = None

def menu(x):
   if x == apple:
       print(apple)
   else:
       print(orange)

menu(apple)
menu(orange)

				

Output:

Available None