Literals are fixed values written directly in Python code. They represent constant data such as numbers, strings, Boolean values and special values and can be assigned to variables or used directly in expressions.
- 10, 3.14, and 5 + 2j are numeric literals.
- 'Hello' and "Python" are string literals.
- True and False are Boolean literals.
- None is a special literal representing the absence of a value.
Numeric Literals
Numeric literals represent numeric values written directly in the code. Python supports three main types of numeric literals:
- Integer Literals: Whole numbers without a decimal point, such as 10, -25 and 0.
- Floating-Point Literals: Numbers containing a decimal point, such as 3.14, -0.01 and 2.0.
- Complex Literals: Numbers with real and imaginary parts, written using j such as 4 + 7j and -3j.
a = 100
b = -50
c = 3.14
d = -0.005
e = 4 + 7j
f = -3j
print(a, b, c, d, e, f)
Output
100 -50 3.14 -0.005 (4+7j) (-0-3j)
Explanation:
- a and b are integer literals.
- c and d are floating-point literals.
- e and f are complex number literals containing imaginary parts represented by j.
String Literals
String literals represent text and are created by enclosing characters within quotes. Python supports several ways to define strings depending on the use case.
- Single-Quoted Strings: Enclosed in single quotes (' '), such as 'Hello'.
- Double-Quoted Strings: Enclosed in double quotes (" "), such as "Python".
- Triple-Quoted Strings: Enclosed in triple quotes (''' ''' or """ """) and commonly used for multi-line text.
- Raw Strings: Prefixed with r to treat backslashes as ordinary characters and ignore escape sequences.
a = 'Hello'
b = "Python"
c = '''This is
a multi-line string'''
d = r"C:\Users\Python"
print(a)
print(b)
print(c)
print(d)
Output
Hello Python This is a multi-line string C:\Users\Python
Explanation:
- a is a single-quoted string.
- b is a double-quoted string.
- c is a triple-quoted string used for multiple lines.
- d is a raw string where backslashes are treated as regular characters.
Boolean Literals
Boolean literals represent logical truth values in Python. They are commonly used in conditions, comparisons, and decision-making statements.
- True: Represents a true condition.
- False: Represents a false condition.
a = True
b = False
print(a, b)
print(1 == True)
print(0 == False)
print(True + 5)
print(False + 7)
Output
True False True True 6 7
Explanation:
- a and b store the Boolean values True and False.
- 1 == True and 0 == False evaluate to True.
- In arithmetic operations, True behaves like 1 and False behaves like 0.
- Therefore, True + 5 gives 6 and False + 7 gives 7.
Collection Literals
Collection literals are used to create multiple values in a single object. Python provides four main collection literals:
- List Literals: Ordered and mutable collections, such as [1, 2, 3].
- Tuple Literals: Ordered and immutable collections, such as (1, 2, 3).
- Dictionary Literals: Collections of key-value pairs, such as {"key": "value"}.
- Set Literals: Unordered collections of unique elements, such as {1, 2, 3}.
ranks = ["First", "Second", "Third"]
colors = ("Red", "Blue", "Green")
students = {"Jai": 10, "Anaya": 12}
nums = {1, 2, 3}
print(ranks)
print(colors)
print(students)
print(nums)
Output
['First', 'Second', 'Third']
('Red', 'Blue', 'Green')
{'Jai': 10, 'Anaya': 12}
{1, 2, 3}
Explanation:
- ranks is a list containing multiple values in a specific order.
- colors is a tuple whose elements cannot be modified after creation.
- students is a dictionary that stores data as key-value pairs.
- nums is a set that stores unique elements only.
Special Literal
Python provides a special literal called None, which represents the absence of a value or a null value. It is commonly used to indicate that a variable currently has no meaningful value assigned to it.
res = None
print(res)
Output
None
Explanation: None is a special value that represents the absence of a value.
Comparison of Python Literals
| Literal Type | Description | Example | Mutable / Immutable |
|---|---|---|---|
| Integer Literal | Represents whole numbers without a decimal point. | a = 77 | Immutable |
| Float Literal | Represents numbers containing a decimal point. | b = 3.144 | Immutable |
| Complex Literal | Represents numbers with real and imaginary parts. | c = 7 + 5j | Immutable |
| String Literal | Represents text enclosed in single, double, or triple quotes. | greeting = "Bonjour" | Immutable |
| Boolean Literal | Represents logical values True and False. | flag = True | Immutable |
| List Literal | Stores multiple ordered values that can be modified. | ranks = ["First", "Second", "Third"] | Mutable |
| Tuple Literal | Stores multiple ordered values that cannot be modified. | colors = ("Red", "Blue", "Green") | Immutable |
| Dictionary Literal | Stores data as key-value pairs. | students = {"Jai": 10, "Anaya": 12} | Mutable |
| Set Literal | Stores unique unordered values. | nums = {1, 2, 3} | Mutable |
| Special Literal (None) | Represents the absence of a value. | result = None | Immutable |