What are Python Literals?
In Python, literals refer to the data that is specified in a variable or constant. So, literal basically corresponds to a short and easy way to specify a value. There are various types of literals in Python such as string literal, integer literal, long integer literal, float, etc. When a literal is evaluated, an object of the given type is yielded. The value can be approximated as needed. Literals in almost all cases correspond to data types that are immutable. As a result of this, an object’s identity becomes less important when compared to its value.
Types of Python Literals
In Python, there are various types of literals. We shall see each of them as discussed in the below section.
1. String Literal
When a text is enclosed in quotes, a string literal is formed. Based on the need, single or double quotes can be used to specify a string literal. There are multiple ways to specify string literal that basically forms various types. These are:
Single Line String Literal
When strings are specified in a single line only then that becomes a single line literal.
t = 'Good Morning!'
Multiline String Literal
When we intend to use multiple lines for specifying the string, that becomes a multiline string literal. In order to accomplish this backslash ‘\’ is used.
t = "Hi!\
How are you?"
As can be seen above, the backslash has been specified after “Hi”, and then we moved to the next line. This functionality is quite useful in situations that involve specifying big strings that we don’t prefer to be specified in a single line.
There’s another method to specify the string in multiple lines. This is through the use of triple quotes. For instance, we want to say “Hi! How are you? It’s long since we met last”. We would like to have this string specified using the triple quotes method. It will be done in python as shown below.
t = """Hi
How are you?
It's long since we met last."""
As can be seen in the above python code execution screenshot, the output gets properly indented. So this approach basically allows us to format the output as needed.
2. Numeric Literals
Numeric literals are of multiple types based on the number type and size. The types of numeric literals are integer, long integer, float (decimal numbers), and complex numbers. Let’s see each of them one-by-one.
They can be either positive or negative. In Python, integers can be assigned to a variable as illustrated below.
a = 23
b = 45
These are basically real numbers that consist of both integer as well as fractional parts. They are, most of the time, result of a division operation. The following example illustrates the use of Python Float Literals.
c = 34.62
As we can see in the above screenshot, C has been assigned a float value. While printing as output, C cannot be printed directly, but first needs to be converted to a string as C is a float variable. So, the str() function has been used in the print statement.
Complex Number Literals
A complex number is an imaginary number, which is basically the root of a negative number. Complex numbers have their own significance and applications. Python allows us to specify complex numbers like any other variable. The following example illustrates the specification and use of complex literals in Python.
i = 1.5J
a = 1 + i
b = a + 3j
In the above code, we specified a complex number. 1.5j is not taken by Python as a string but as a complex number. Then we performed additional operations. And finally, we printed the complex numbers as well as their types. Let’s see how the above code works in Python.
As can be seen above, any number of the form nj is considered by Python as a complex literal. This is proved by the output that the type() function gave in the above program code.
3. Boolean Literals
These are the literals that can have either True or False value. Let’s go through the following program code to see how Boolean literals work in Python.
a = 5 > 3
b = 55 < 33
As we can see above, the first condition is true and so variable a gets True value. In the second case, the condition is evaluated to False, and so b is assigned False.
4. List Literal
A list in Python is a set of items of different data types. Lists can be modified as needed and are even mutable. In order to create a list, specify the values of different data types separated by a comma and enclose them in square brackets. Any value or multiple values stored in a list can be retrieved by making use of a slice operator i.e. [:]. There are two operators associated with lists. These are firstly concatenation i.e. + operator and secondly repetition i.e. ‘*’ operator. Let’s see how to list literals work in Python by going through the examples as illustrated below.
l = [231, 'Mary Matthews', 1+3.5j, 43.56]
In the above code, we created a list containing an integer, string, complex number, and floating-point number. The list is then printed and finally, we check the type of the variable l. Let’s see how the code performs in Python. This is as shown in the following screenshot.
As we can see in the above screenshot, the type of variable l is the list. So, Python automatically recognizes that it a list based on the type of items present in the set.
Now, we create another list and add the two lists.
l1 = ['England', 122]
l2 = ['India', 123]
print(l1 + l2)
Amongst the various functionalities provided by Python programming to apply a concept in the easiest manner, literal is one. May the data of any type, literals allow the user to specify the input value as needed. Literals are quite useful in programs involving variables of different data types.
This is a guide to Python Literals. Here we discuss the Introduction and types of python literals which include string, numeric, boolean and list literal. You may also look at the following articles to learn more –