Literals in Python are fixed values written directly in the code that represent constant data. They provide a way to store numbers, text, or other essential information that does not change during program execution. Python supports different types of literals, such as numeric literals, string literals, Boolean literals, and special values like None. For example:
- 10, 3.14, and 5 + 2j are numeric literals.
- ‘Hello’ and “Python” are string literals.
- True and False are Boolean literals.
.png)
Literals in Python
In this article, we’ll explore various types of literals in Python with examples to demonstrate their usage:
Numeric Literals
Numeric literals represent numbers and are classified into three types:
- Integer Literals – Whole numbers (positive, negative, or zero) without a decimal point. Example: 10, -25, 0
- Floating-point (Decimal) Literals – Numbers with a decimal point, representing real numbers. Example: 3.14, -0.01, 2.0
- Complex Number Literals – Numbers in the form a + bj, where a is the real part and b is the imaginary part. Example: 5 + 2j, 7 – 3j
Python
# Integer literals
a = 100
b = -50
# Floating-point literals
c = 3.14
d = -0.005
# Complex number literals
e = 4 + 7j
f = -3j
print(a, b, c, d, e, f)
Output100 -50 3.14 -0.005 (4+7j) (-0-3j)
String Literals
String literals are sequences of characters enclosed in quotes. They are used to represent text in Python.
Types of String Literals:
Python
# Different string literals
a = 'Hello' # Single-quoted
b = "Python" # Double-quoted
c = '''This is
a multi-line string''' # Triple-quoted
d = r"C:\Users\Python" # Raw string
print(a)
print(b)
print(c)
print(d)
OutputHello
Python
This is
a multi-line string
C:\Users\Python
Boolean Literals
Boolean literals represent truth values in Python. They help in decision-making and logical operations. Boolean literals are useful for controlling program flow in conditional statements like if, while, and for loops.
Types of Boolean Literals:
- True – Represents a positive condition (equivalent to 1).
- False – Represents a negative condition (equivalent to 0).
Python
# Boolean literals
a = True
b = False
print(a, b) # Output: True False
print(1 == True) # Output: True
print(0 == False) # Output: True
print(True + 5) # Output: 6 (1 + 5)
print(False + 7) # Output: 7 (0 + 7)
Explanation:
- True is treated as 1, and False is treated as 0 in arithmetic operations.
- Comparing 1 == True and 0 == False returns True because Python considers True as 1 and False as 0.
Collection Literals
Python provides four different types of literal collections:
Python
Rank = ["First", "Second", "Third"] # List
colors = ("Red", "Blue", "Green") # Tuple
Class = { "Jai": 10, "Anaya": 12 } # Dictionary
unique_num = {1, 2, 3} # Set
print(Rank, colors, Class, unique_num)
Output['First', 'Second', 'Third'] ('Red', 'Blue', 'Green') {'Jai': 10, 'Anaya': 12} {1, 2, 3}
Explanation:
- [“First”, “Second”, “Third”] is a list (rank of students).
- (“Red”, “Blue”, “Green”) is a tuple (like a set of crayons you can’t change).
- {“Jai”: 10, “Anaya”: 12} is a dictionary (storing names and ages).
- {1, 2, 3} is a set (a bag where every item is unique).
Special Literal
Python contains one special literal (None). ‘None’ is used to define a null variable. If ‘None’ is compared with anything else other than a ‘None’, it will return false.
Python
Explanation: None represents “nothing” or “empty value.”
Difference between Literals
Type of Literals
| Description
| Example
| Mutable/Immutable
|
---|
Integer literals
| Whole numbers (without decimals).
| a = 77
| Immutable
|
---|
Float literals
| Numbers with a decimal point.
| b=3.144
| Immutable
|
---|
Complex literals
| Numbers with real and imaginary part.
| c = 7 + 5j
| Immutable
|
---|
String literals
| Stores text. Can use single, double, or triple quotes.
| greeting = “Bonjour” story = “””Once upon a time…”””
| Immutable
|
---|
Boolean literals
| Represents True (1) or False (0).
| a = (1 == True) → True b = (1 == False) → False
| Immutable
|
---|
Boolean as Number
| Boolean values can act as numbers (1 or 0).
| c = True + 3 → 4 d = False + 7 → 7
| Immutable
|
---|
List literal
| Stores multiple values; can be changed.
| Rank = [“First”, “Second”, “Third”]
| Mutable
|
---|
Tuple literal
| Like a list but cannot be changed.
| colors = (“Red”, “Blue”, “Green”)
| Immutable
|
---|
Dictionary literal
| Stores key-value pairs.
| Class = { “Jai”: 10, “Anaya”: 12 }
| Mutable
|
---|
Set literal
| Stores unique values, unordered.
| unique_num = {1, 2, 3}
| Mutable
|
---|
Special literal
| Represents “nothing” or “empty.”
| water_remain = None
| Immutable
|
---|
Similar Reads
Print new line in Python
In this article, we will explore various methods to print new lines in the code. Python provides us with a set of characters that performs a specific operation in the code. One such character is the new line character "\n" which inserts a new line. [GFGTABS] Python a = "Geeks\nfor\nGeeks"
2 min read
Lists Of Strings In Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples. Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings. [GFGTABS
2 min read
Python - Integers String to Integer List
In this article, we will check How we can Convert an Integer String to an Integer List Using split() and map()Using split() and map() will allow us to split a string into individual elements and then apply a function to each element. [GFGTABS] Python s = "1 2 3 4 5" # Convert the string to
2 min read
Insert a number in string - Python
We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
2 min read
Print a List of Tuples in Python
The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ]. Using print()print() function is the
2 min read
Python Glossary
Python is a beginner-friendly programming language, widely used for web development, data analysis, automation and more. Whether you're new to coding or need a quick reference, this glossary provides clear, easy-to-understand definitions of essential Python termsâlisted alphabetically for quick acce
5 min read
Subtract Two Numbers in Python
Subtracting two numbers in Python can be done in multiple ways. The most common methods include using the minus operator (-), defining a function, using a lambda function, or applying bitwise operations. Each method has its use cases depending on simplicity, readability and performance needs. Using
2 min read
List As Input in Python in Single Line
Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com
3 min read
Boolean list initialization - Python
We are given a task to initialize a list of boolean values in Python. A boolean list contains elements that are either True or False. Let's explore several ways to initialize a boolean list in Python. Using List MultiplicationThe most efficient way to initialize a boolean list with identical values
3 min read
Python - Words Lengths in String
We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6]. Using Lis
2 min read