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.
Literals in PythonIn 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 |
---|
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice