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 |
---|
Similar Reads
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
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
Interesting Facts About Python Python is a high-level, general-purpose programming language that is widely used for web development, data analysis, artificial intelligence and more. It was created by Guido van Rossum and first released in 1991. Python's primary focus is on simplicity and readability, making it one of the most acc
7 min read
How to Initialize a String in Python In Python, initializing a string variable is straightforward and can be done in several ways. Strings in Python are immutable sequences of characters enclosed in either single quotes, double quotes or triple quotes. Letâs explore how to efficiently initialize string variables.Using Single or Double
2 min read
Python String Input Output In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.Input operations in PythonPythonâs input() function allows us to get data from the u
3 min read
Output of Python programs | Set 7 Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
3 min read