Python Tutorial
Python Literals
Learn Python literals with clear tables and examples for numbers, strings, Booleans, collections, None, escape sequences, and mutability.
What are literals in Python?
A literal is a value written directly in Python source code. The number 42, the text "Python", and the Boolean value True are all literals. A variable is a name that refers to a value; the literal is the value itself.
count = 42
language = "Python"
ready = True
missing = NoneTypes of Python literals
| Literal category | Examples | Used for |
|---|---|---|
| Numeric | 10, 3.14, 2 + 3j | Whole numbers, decimal values, and complex numbers. |
| String | "Hello", 'Python' | Text and character data. |
| Boolean | True, False | Truth values used in conditions. |
| Collection | [1, 2], (1, 2) | Groups of values such as lists and tuples. |
| Special | None | Represents the absence of a value. |
Numeric literals
Numeric literals create int, float, or complex values. Python integers can grow beyond the size commonly supported by a fixed-width integer type, subject to available memory.
Integer literals
| Number system | Prefix | Example | Decimal value |
|---|---|---|---|
| Decimal | None | 25 | 25 |
| Binary | 0b or 0B | 0b11001 | 25 |
| Octal | 0o or 0O | 0o31 | 25 |
| Hexadecimal | 0x or 0X | 0x19 | 25 |
decimal_number = 25
binary_number = 0b11001
hex_number = 0x19
print(decimal_number, binary_number, hex_number)Floating-point and complex literals
A floating-point literal contains a decimal point or an exponent. A complex literal uses j or J for its imaginary part:
price = 19.95
large_value = 2.5e3 # 2500.0
complex_value = 2 + 3j
print(price, large_value, complex_value)Binary floating-point values are approximations, so use decimal.Decimal when exact decimal arithmetic is required, such as for some financial calculations.
String literals
String literals represent text and can use single quotes, double quotes, or triple quotes. Choose a quoting style that keeps the text readable:
single = 'Python'
double = "Programming"
multi_line = """This text
uses more than one line."""
print(single, double)
print(multi_line)Escape sequences
| Sequence | Meaning | Example |
|---|---|---|
\n | New line | "Hello\nPython" |
\t | Tab | "Name:\tAsha" |
\' | Single quote inside a single-quoted string | 'It\'s' |
\" | Double quote inside a double-quoted string | "Say \"Hi\"" |
\\ | Backslash | "C:\\Python" |
Formatted string literals
F-strings insert expressions into text using an f prefix:
name = "Asha"
score = 92
print(f"{name} scored {score}")Boolean literals
Python has two Boolean literals: True and False. They are commonly produced by comparisons and used by if statements:
is_adult = age >= 18
if is_adult:
print("Access allowed")Use the exact capitalized spellings. true and false are not Python Boolean literals.
Collection literals
| Collection | Literal syntax | Important behavior |
|---|---|---|
| List | [10, 20, 30] | Ordered and mutable; duplicates are allowed. |
| Tuple | (10, 20, 30) | Ordered and immutable; useful for fixed groups of values. |
| Dictionary | {"name": "Asha"} | Maps keys to values; keys must be hashable. |
| Set | {10, 20, 30} | Stores unique values without relying on position. |
numbers = [10, 20, 30]
coordinates = (12.9, 77.6)
student = {"name": "Asha", "level": 1}
unique_numbers = {10, 20, 20, 30}
print(numbers, coordinates, student, unique_numbers)An empty list is [], an empty dictionary is {}, and an empty set must be created with set() because {} means an empty dictionary.
The special literal None
None represents “no value” or “not available yet.” It is not the same as zero, an empty string, or False. Check it with identity comparison:
result = None
if result is None:
print("No result is available")Literals and mutability
Writing a literal creates a value, but whether that value can be changed depends on its type. Lists, sets, and dictionaries are mutable; numbers, strings, tuples, and Boolean values are immutable. Assigning a new value to a variable changes what the name refers to; it does not change an immutable value in place.
Common mistakes
- Using
true,false, orNULLinstead ofTrue,False, orNone. - Using
{}when an empty set was intended; useset(). - Forgetting that input from
input()is a string literal until it is converted. - Comparing with
iswhen value equality with==is required.
Further Reading
Continue learning with these related Python tutorials:
Continue learning
