Python Literals
Introduction
In Python, literals are the fixed values that we directly write in a program. These values are assigned to variables or used in expressions.
For example:
a = 10
name = "Python"
price = 99.5
Here, 10, "Python", and 99.5 are literals.
In this article, we will learn about different types of Python literals with simple examples.
1. What Are Literals in Python?
A literal is a constant value written directly in Python source code.
Example:
x = 100
y = 10.5
z = "Hello"
In this example:
100is an integer literal.10.5is a floating-point literal."Hello"is a string literal.
2. Numeric Literals
Numeric literals represent numerical values. Python supports integer, floating-point, and complex literals.
A. Integer Literals
Integer literals are whole numbers without decimal points.
Examples:
a = 10
b = -20
c = 0
print(a)
print(b)
print(c)
Output:
10
-20
0
Python supports different number systems for representing integer literals.
1. Binary Literals
Binary numbers use only 0 and 1. In Python, binary literals start with 0b or 0B.
a = 0b1010
print(a)
Output:
10
2. Decimal Literals
Decimal numbers use digits from 0 to 9. This is the default number system.
a = 100
print(a)
Output:
100
3. Octal Literals
Octal numbers use digits from 0 to 7. In Python, they start with 0o or 0O.
a = 0o12
print(a)
Output:
10
4. Hexadecimal Literals
Hexadecimal numbers use digits from 0 to 9 and letters from A to F. In Python, they start with 0x or 0X.
a = 0xA
print(a)
Output:
10
B. Floating-Point Literals
Floating-point literals represent numbers containing decimal points.
Examples:
a = 10.5
b = 3.14
c = -7.8
print(a)
print(b)
print(c)
Output:
10.5
3.14
-7.8
Python also supports scientific notation for representing very large or very small numbers.
Example:
a = 1.5e3
b = 2.5e-2
print(a)
print(b)
Output:
1500.0
0.025
Here:
1.5e3means (1.5 \times 10^3).2.5e-2means (2.5 \times 10^{-2}).
C. Complex Literals
Complex literals represent complex numbers. A complex number has a real part and an imaginary part.
In Python, the imaginary part uses the letter j.
Example:
a = 5 + 6j
print(a)
print(a.real)
print(a.imag)
Output:
(5+6j)
5.0
6.0
Here:
5.0is the real part.6.0is the imaginary part.
3. String Literals
String literals represent text or a sequence of characters. They can be written using single quotes, double quotes, or triple quotes.
A. Single-Quoted String
name = 'Python'
print(name)
Output:
Python
B. Double-Quoted String
name = "Programming"
print(name)
Output:
Programming
C. Triple-Quoted String
Triple quotes are commonly used for multiline strings.
message = """Welcome to
Python Programming"""
print(message)
Output:
Welcome to
Python Programming
D. Unicode String Characters
Python strings support Unicode characters, allowing text from different languages and symbol sets.
Example:
print("\u0041")
Output:
A
E. Raw String Literals
A raw string uses the r or R prefix. It treats backslashes as ordinary characters instead of interpreting them as escape sequences.
Example:
path = r"C:\new\test"
print(path)
Output:
C:\new\test
4. Boolean Literals
Boolean literals represent truth values. Python has two Boolean literals:
TrueFalse
Example:
a = True
b = False
print(a)
print(b)
print(type(a))
Output:
True
False
<class 'bool'>
Boolean values can also be used in arithmetic operations because bool is a subclass of int in Python.
Example:
print(True + 4)
print(False + 10)
Output:
5
10
Here, True behaves like 1 and False behaves like 0 in arithmetic.
5. None Literal
None is a special literal in Python that represents the absence of a value.
Example:
result = None
print(result)
print(type(result))
Output:
None
<class 'NoneType'>
It is commonly used to represent an empty result or an unassigned value.
6. Summary of Python Literals
| Literal Type | Example |
|---|---|
| Integer | 100 |
| Binary | 0b1010 |
| Decimal | 100 |
| Octal | 0o12 |
| Hexadecimal | 0xA |
| Float | 10.5 |
| Scientific notation | 1.5e3 |
| Complex | 5+6j |
| String | "Python" |
| Boolean | True, False |
| None | None |
7. Practice Questions (MCQs)
Q1. What is a literal in Python?
A. A function
B. A fixed value written directly in code
C. A loop
D. A variable name
Correct Answer: B. A fixed value written directly in code
Q2. Which prefix is used for binary literals in Python?
A. 0x
B. 0o
C. 0b
D. 0d
Correct Answer: C. 0b
Q3. What is the output of print(0b1010)?
A. 2
B. 8
C. 10
D. 12
Correct Answer: C. 10
Q4. Which character represents the imaginary part of a complex number in Python?
A. i
B. j
C. c
D. x
Correct Answer: B. j
Q5. Which of the following is a Boolean literal?
A. "True"
B. 1
C. True
D. None
Correct Answer: C. True
Q6. What is the output of print(True + 4)?
A. 4
B. 5
C. True
D. TypeError
Correct Answer: B. 5
Q7. What does None represent in Python?
A. Zero
B. False only
C. Absence of a value
D. Empty string
Correct Answer: C. Absence of a value