Literals in Python represent raw information assigned to fixed or variable values when programming. Literal is essentially a quick and simple way to define a value. They may be in the form of text, Boolean data, integers, or any other type of data. The value of Python literals doesn’t change as the program runs.
In this article, we’ll take a closer look at the six major types of literals and provide some useful examples that will help understand the concept better.
Photo by Artturi Jalli on Unsplash
A string literal is created when text is encased in quotations. To specify a string literal, you can use single or double quotes. There are several ways to specify them, which essentially create different types of string literals. As you are teaching yourself how to code, consider the following string literals:
A single line literal is created when strings are only supplied on a single line. Both double quotation marks and single quotation marks can be used to wrap single-line string literals. To get a grasp of how they operate, consider the examples below:
Code:
single_quotes_string='Good Morning!'
double_quotes_string="How are you?"
Output:
Good Morning!
How are you?
A multi-line string is a group of characters or a string that spans several lines. These strings are again implemented in two different ways:
Code:
str="Good evening!\ How are you?\
Output:
Good evening! How are you?
We may easily generate a multi-line string in Python by using triple quotes at the beginning and end of the string.
Code:
t = """Good evening!
How are you? """
print (t)
Output:
Good evening!
How are you?
Based on the nature and magnitude of the numbers, there are various forms of numeric literals: integer, large integer, complex numbers, and float (decimal numbers). Let's examine each of them individually.
Integers make up all natural numbers, whether they are negative, zero, or positive. The following kinds can also be used to describe them:
Code:
a = 36
b = 52
print (a)
print (b)
Output:
36
52
Float literals are essentially real numbers made up of both integer and fractional components. Consider the following example to get a better idea of what are float literals:
Code:
c = 44.42
print (c)
Output:
44.42
A complex — or imaginary — number, is essentially the negative number's root. Complex numbers have unique applications and significance and they can be specified in Python. Below, you can see a simple example of complex number literals in Python:
Code:
a = 6 + 9j
b = 4j
print (a)
print (b)
Output:
(6+9j)
4j
Boolean literals are pretty straightforward in nature as they only represent a True or False value. Here’s a simple example of the Boolean literals code and output:
Code:
a = 4 > 2
print (a)
b = 48 < 49
print (b)
Output:
True
False
If you want to interact with several values in Python literal, you can use collections. Python has four different forms of literal collections:
A list is a collection of objects of various data types. Lists are mutable, so they can be changed as necessary. To form a list, you should specify the values of various data types and enclose them in square brackets. By using the [:] slice operator, any value or group of values contained in a list can be retrieved. Lists are accompanied by two operators: concatenation (the + operator) and repetition (the * operator). Let's examine the examples provided below to learn how list literals function in Python:
Code:
l = [465, Anne Simons, 1+4.5j, 33.44]
print (l)
type (l)
Output:
[465, Anne Simons, 1+4.5j, 33.44]
Tuples are literals that can hold any data type and are declared by using round brackets. The components of Tuple literals are separated by commas. Unlike list literals, these are immutable.
Code:
even_numbers = (2, 4, 6, 8)
vowels = ('a','e','i','o','u')
print (even_numbers)
print (vowels)
Output:
(2, 4, 6, 8)
('a', 'e', 'i', 'o', 'u')
A dictionary is a set of data that uses a key-value format to store value. These are set out by commas and contained in curly braces. These literals can hold a variety of data types and are mutable. See the below example to understand the implementation of dictionary literals:
Code:
my_dict = {'a': 'apple', 'b': 'bat', 'c': 'car'}
print (my_dict)
Output:
{'a': 'apple', 'b': 'bat', 'c': 'car'}
Set literals are a non-modifiable collection of unordered data. The components are separated by commas and enclosed in curly brackets. The below example outlines the use of set literals in Python:
Code:
vowels = {'a', 'e', 'i', 'o', 'u'}
print (vowels)
Output:
{'o', 'e', 'a', 'u', 'i'}
None is a unique literal in the Python language. It denotes that a specific field hasn’t been created yet. When we print a variable in Python without a value given to it, Python will output None. Python also uses "none" at the end of lists. Check this example:
Code:
my_list = ['Dog', 'Cat', None]
print (my_list[2])
Output:
output13
Literal is one of the many features that Python programming offers to help you apply an idea in the simplest way possible. Literals enable you to determine the input value as necessary for data of any type. They can be quite helpful in programs that use variables with various data types. Hopefully, this post and the real-life examples helped you better understand literals in Python and how to use the various types. Whether you're just starting out or you're an experienced Python programmer, we have a tutor that can help you master your Python programming coursework. If you're stuck on a question or need help with Python homework our tutors will help you to solve it!