What are Literals in Python
A literal in Python is a way to represent a fixed value or raw data in the source code. It directly appears in the program and does not change during the execution of a program.
Literals can be either numbers, text, boolean, or any other form of data. In Python program, we use literals to create fixed values that are assigned to variables, constants, used in expressions, or passed to functions.
For example, suppose we want to setting a variable with a specific value in a Python program. We will write the code for it as:
x = 10
This statement creates an integer object containing a literal value 10. Literal means simply a value. Some other examples of literal in Python are 100, x, 2.25, “Hello”, ‘World’, True, False, and so on.
Types of Literals in Python Language
Literals can be any of the primitive data types in Python language. The way of using literals depends on its type. The various types of literals used in the Python program are as follows:
- String literals
- Numeric literals
- Boolean literals
- Literal Collections
- Special Literals
Let’s look at the following diagrammatic representation of the classification of Python literals.

String Literals
A string literal in Python is a consecutive sequence of characters used to store and represent messages, heading, and other text-based information.
It is enclosed within a pair of double quotation marks and single quotation marks. Both are applicable to define the string literals.
“Hello”, ‘World’, “123”, ‘The area of rectangle is: ‘, etc are some examples of string literals. We use quotation marks to start and end the string.
Strings in Python are immutable, meaning that when we perform an operation on strings, the Python interpreter always creates a new string object in the memory, rather than mutating an existing string object.
String object provides several methods in Python that we will gain knowledge in the further tutorial.
Types of Strings
Python language supports two types of string objects.
1) Single-line String: A string created in a single line is called single-line string. An example of a single-line string object is as below:
single_line_string = ‘Scientech Easy’ # Here, literal is Scientech Easy.
2) Multi-line String: A piece of statement written in multiple lines is called multiple lines string. In Python, there are two ways to create multi-line string objects.
a) Adding a black slash (\) at the end of each line. An example of it is:
multi_line_string = 'Welcome \
to \
Scientech Easy'
print(multi_line_string)
Output:
Welcome to Scientech Easy
b) We can also create a multi-line string literal using triple quotation marks. An example of it is as below:
multi_line_string = '''Welcome \
to \
Scientech Easy, \
Dhanbad'''
print(multi_line_string)
Output:
Welcome to Scientech Easy, Dhanbad
Numeric literals
Numeric literals in Python consists of numbers or digits from 0 to 9. They may be preceded by a positive (+) or negative (-) sign.
All numeric literals in Python are immutable (unchangeable) objects, meaning that when we perform an operation on a number object, the Python interpreter always creates a new number object.
Operation performed on numbers are called arithmetic operations. We can classify numeric literals in Python into three types that are as follows:
- Integer literals
- Floating point literals
- Complex literals
Integer literals in Python
We know an integer is a whole number without any decimal point. It comprises a consecutive sequence of digits, from (0 to 9). An integer can be positive or negative. An integer literal comes into a different number system:
- Decimal literal (base 10)
- Octal literal (base 8)
- Hexadecimal literal (base 16)
a) Decimal literal: A decimal literal is a consecutive sequence of digits, from (0 to 9) in which the first digit is nonzero. It is the most commonly used number system in any programming language.
If a decimal literal consists of two or more digits, the first digit must be some other digit than zero because the Python interpreter will consider it as an octal number. Some examples of decimal literals are 10, 30, 33, 676, 20,000, etc.
b) Octal literal: An octal literal consists of 0, followed by the sequence of any of the digits from 0 to 7. The first digit must be 0 in order to identify as an octal literal. For example: 01, 025, 02799, etc.
c) Hexadecimal literal: This literal comprises any of the digits from 0 to 9 and letters from A to F, representing decimal values from 10 to 15. A hexadecimal integer literal starts with either 0x or 0X. For example: 0x124, 0X12345, 0x10,000.
Rules for creating integer literals
The following are the rules for creating integer literals:
a) Python does not allow to use commas, or blank spaces in an integer literal. Some examples of valid integer literals are 20, 40, 140, 1990, etc. Invalid integer literals are 20,5; 30 50, etc.
b) Integer literals may be positive or negative. If no sign precedes an integer literal, Python considers it as positive. Some example of valid integer literals are +20; -40; 130; -1999. Invalid integer literals are 20-5; 0x-120.
c) Python does not allow to have a decimal point in integer literals. Some of invalid integer literals having decimal point are 30.5; 40.50; 0x4.25.
d) Integer literals must contain at least one digit, for example, 0.
e) The value of an integer literal should lie within the range of integers.
Note that the print() function always outputs integer using decimal representation, even if they are specified as octal or hexadecimal. The following example code shows the output 50 in three forms:
x = 50 # Decimal integer
y = 0o62 # Octal integer
z = 0x32 # Hexadecimal integer
print("Decimal literal representation: ", x)
print("Octal literal representation: ", y)
print("Hexadecimal representation: ", z)
Output:
Decimal literal representation: 50
Octal literal representation: 50
Hexadecimal representation: 50
Floating-point literals in Python
A floating-point literal is a real number that contains a decimal point (.) or exponent sign or both, differentiating them from integer literals. We can express them in either standard or scientific notation, which is explained in the below:
a) Standard notation: In this notation, the floating point numbers contain an integer part or fractional part with a decimal point in between two parts.
For example: 15.5, -10.50, 333.999, 9.0, -1234.567, and so on. The following are the general rules for creating floating point literals in standard notation:
1. A decimal point must be present. Valid examples are 20.30, -150, 0.9999, etc.
2. We cannot use commas, or blanks with real literals.
3. A floating-point literal can be positive or negative. If any sign does not precede a literal, then the interpreter will consider it as a positive number. For example: +2734.365, 234.55, -1234.55, and so on.
b) Scientific notation: In scientific notation, a number contains two parts such as mantissa and exponent. The mantissa part represents the floating point number and the exponent part specifies a power of 10 by which the number is to be multiplied.
The letter E or e separates the mantissa and exponential portion of a number. For instance, we can represent a number 24.55 in the exponential form as 2.455e2 (i.e. 2.455*10^2).
Here, the number 2.455 specifies the mantissa part and the part after the letter e specifies the exponent part which has a base value 10.
In general, we use exponential form to represent the large numbers in order to writing large number of 0s in a number. For instance, 5,000,000 can be written as 5.0e6.
Rules for creating floating point literals in scientific notation
There are the following rules for creating floating point literals in scientific notation. They are:
1. The mantissa part can be in integer or decimal form. A positive or negative sign can precede it. The default sign is positive. For example, we can write 25.5 as 2.55e1 or 255e-1, or 0.255e2.
2. The exponent part must contain at least one digit, which should be a positive or negative integer. The default sign is +ve. Valid examples are 2.32e+1; 234e-1. Invalid examples are 2.0e; -121e; +1260e.
3. Spaces are not allowed in the mantissa part and in the exponent part. For instance, 5 5.44e-5, 525, 6e-6 are invalid.
4. We can write the letter e in both uppercase or lowercase. For example, 5.2e7, 1.3E7.
5. We can leave out the decimal point if we include the exponent part. For example, we can represent 0.95 as 95e-2.
Note that the print() function always outputs integer floating-point numbers. Even if they are specified in the exponential form. Look at the following example code of Python floating point literals.
x = 75e-2
y = 0.75
print(x)
print(y)
Output:
0.75
0.75
Complex literal
A complex number comprises two floating-point values, one each for the real and imaginary. A complex literal is represented by (a + bj).
Here, a is a real part and the b with j is the imaginary or complex part. The letter j represents the square root of -1. In mathematics, we use the letter i. An example code of it is below.
# complex literal
a = 5 + 10j
b = 2j
print(a)
print(b)
Output:
(5+10j)
2j
Boolean literals
A boolean literal is a logical value that can have any of the two values: True or False. True represents the value 1 whereas False represents the value 0.
We use boolean literal in certain operations in which need a boolean value. We use boolean value to test whether a condition is true or false. Let’s take an example based on it.
# boolean literals
a = (9 == 9)
b = (5 == False)
c = True + 5
d = False + 5
print("a is ", a)
print("b is ", b)
print("c is ", c)
print("d is ", d)
Output:
a is True
b is False
c is 6
d is 5
You can observe in the above example, we have used boolean literals for numerical comparison. Based on the conditions, we received the outputs True and False, respectively.
Special literals in Python
Python language contains only one special literal, i.e., None. It is a special constant in Python that specifies the null value or the absence of a value. We use None to specify a field that is not constructed. We also use None at the end of lists in Python. Let’s take an example on it.
# Special literals
a = None
print(a)
Output:
None
You must make a note that None does not imply False, 0, or an empty list, dictionary, string, and so on.
Collection literals in Python
A collection literal is a syntactic representation that is used to work with more than one value. Python language provides the four types of collection literal tokens, such as List, Tuple, Dictionary, and Set. Let’s understand each one by one.
List literals
A list is a collection of mutable data or values of different types. To create a list in Python, place data or values in within square brackets ([ ]) and separating them by commas (,).
You can consider the Python lists similar to arrays in C, C++, or Java. To declare a list in Python, the syntax is as:
<name of list> = [ <value1>, <value2>, <value3>, . . . . ]
Let’s take an example based on it.
# List literals
address = ['Scientech Easy', 'Joraphatak Road', 'Dhanbad']
print(address)
Output:
['Scientech Easy', 'Joraphatak Road', 'Dhanbad']
Let’s take another example in which we will assign the first 10 natural numbers and print the numbers in a list called num.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(num)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Dictionary Literals
Python dictionary is a collection of data that stores value in a key-value pairs. They are enclosed in curly braces ({ }) and separated by the commas (,).
Dictionary literals in Python are mutable (changeable) and can also contain different types of data or values. Let’s take an example based on it.
# Dictionary literals
fruits_dict = {'a': 'apple',
'o': 'orange',
'b': 'banana'}
print(fruits_dict)
Output:
{'a': 'apple', 'o': 'orange', 'b': 'banana'}
Tuple Literals
Tuple in Python is a collection of different data-type, similar to a list. It is immutable, which means we cannot change it after creation. It performs the same operation as a list does.
The parentheses ( ( ) ) enclose it and the comma separates each element of tuples. Below is an example code of tuple literals.
# tuple literals
even_numbers = (2, 4, 6, 8, 10, 12)
vowels = ('a','e','i','o','u')
direction = ('North', 'South', 'East', 'West')
print(even_numbers)
print(vowels)
print(direction)
Output:
(2, 4, 6, 8, 10, 12)
('a', 'e', 'i', 'o', 'u')
('North', 'South', 'East', 'West')
Set Literals
Set literals in Python are a well-defined collection of unordered data that cannot be changed. The elements of a set are enclosed within curly brackets separated by commas (,). Let’s see an example code for it.
# Set literals
fruits = {'Apple', 'Mango', 'Banana', 'Orange', 'Grape'}
print(fruits)
Output:
{'Orange', 'Apple', 'Grape', 'Banana', 'Mango'}
In this tutorial, you have known in depth about literals in Python with various examples. I hope that you will have understood the basic points of Python literals and practiced all programs.
Thanks for reading!!!



