Understanding Python Literals: A Complete Guide

In Python, literals are fixed values written directly in your code. They can be assigned to variables, used in expressions or passed to functions. Whether it’s a number, a piece of text, or a collection of values, a literal is often the starting point for working with data.

This guide explains what Python literals are, the different types available, and how each one is used through simple, beginner-friendly examples.

Introduction: What Are Python Literals?

A literal in Python is a value written directly in code. You don’t calculate it or derive it from anything — it is already fixed in the code.

Literals can represent different kinds of data such as numbers, text, true/false values, or even collections like lists and dictionaries. You’ll often use them when assigning values to variables or passing data into functions.

For example, in the following code, 25 and "Alice" are literals because they are written directly in the program:

age = 25
name = "Alice"

In Python, literals are generally grouped into a few categories based on their type.

Types of Python Literals

Python provides several types of literals to represent different kinds of data. Some represent individual values, while others store collections of values.

1. Numeric Literals
2. String Literals
3. Boolean Literals
4. None Literal
5. Collection Literals

The following sections explain each Python literal type with simple explanations and examples.

1. Numeric Literals

Numeric literals represent numbers written directly in Python code. Depending on the type of value you need, Python supports whole numbers, decimal numbers, and complex numbers.

i) Integer Literals

Integer literals represent whole numbers without a decimal point. They may be positive, negative, or zero. Python also lets you write integers in decimal, binary, octal, and hexadecimal formats.

Example: Decimal Integer Literal

# Decimal integer literal
num = 42
print(num)  # Output: 42
Explanation: Here, 42 is written directly as a whole number. Because there isn’t a decimal point, Python automatically recognizes it as an integer literal.

Example: Binary, Octal, Hexadecimal

# Binary literal
bin_num = 0b1010
print(bin_num)  # Output: 10


# Octal literal
oct_num = 0o12
print(oct_num)  # Output: 10


# Hexadecimal literal
hex_num = 0xA
print(hex_num)  # Output: 10
Explanation: Although these numbers look different, they all represent the decimal value 10. The prefixes 0b, 0o, and 0x simply tell Python which number system is being used. Curious about the different ways Python represents integer numbers? Explore our complete guide on Python Integer Literals.

⬆ Move to Top

ii) Floating-Point Literals

Floating-point literals represent numbers that contain a decimal point. They are commonly used when working with measurements, scientific calculations, or financial values.

Example: Float Literal

pi = 3.14159
print(pi)  


# Output: 3.14159

Explanation: Notice the decimal point in 3.14159. That’s what makes it a floating-point literal. This type of value is useful whenever a whole number isn’t enough.

Want to see how Python handles decimal values with precision? Explore our complete guide on Python Float Literals.

⬆ Move to Top

iii) Complex Literals

Complex literals represent complex numbers that contain both a real part and an imaginary part. In Python, the imaginary part is written using the suffix j.

Example: Complex Literal

complex_num = 2 + 3j
print(complex_num)  


# Output: (2+3j)

Explanation: The value 2 + 3j is a complex literal. Here, 2 is the real part, and 3j is the imaginary part. Python stores both parts together as a single complex number.

Ready to work with numbers beyond the real number system? Discover how Python handles them in our guide on Python Complex Literals.

⬆ Move to Top

2. String Literals

String literals represent text written inside quotes. Python supports single quotes, double quotes, and triple quotes for creating multi-line strings.

Example: Single and Double Quotes

greeting = 'Hello, Python!'
print(greeting)

message = "Learning literals is fun!"
print(message)


# Output
Hello, Python!
Learning literals is fun!

Explanation: Both examples create string literals. Python treats single and double quotes the same, so you can choose whichever fits your code more comfortably.

Example: Multi-line String

multiline_str = '''This is a string
that spans across
multiple lines.'''

print(multiline_str)


# Output
This is a string
that spans across
multiple lines.

Explanation: Triple quotes let a string continue across multiple lines without needing special characters. They’re useful whenever the text is longer than a single line.

There’s much more to strings than just quotes. Explore our complete guide on Python String Literals.

⬆ Move to Top

3. Boolean Literals

Boolean literals have only two possible values: True and False. They are mainly used when evaluating conditions and controlling the flow of a program.

Example: Boolean Literals

is_active = True
is_completed = False

print(is_active)
print(is_completed)


# Output
True
False

Explanation: True and False simply represent yes-or-no outcomes. Python relies on these values whenever it needs to decide whether a condition has been met.

Boolean values may look simple, but they’re at the heart of decision-making in Python. Learn more in our guide on Python Boolean Literals.

⬆ Move to Top

4. None Literal

The None literal represents the absence of a value. It is commonly used when a variable has not been assigned meaningful data or when a function has no value to return.

Example: None Literal

result = None
print(result)


# Output
None

Explanation: In this example, result hasn’t been given an actual value yet, so it stores None. This is different from 0, False, or an empty string because it represents the absence of a value.

The None literal is used more often than most beginners expect. See how it works in our complete guide on Python None Literal.

⬆ Move to Top

5. Collection Literals

Collection literals allow multiple values to be stored together. Python provides four commonly used collection literals: lists, tuples, sets, and dictionaries.

i) List Literals

A list literal stores an ordered collection of items inside square brackets ([]). Lists are mutable, which means their contents can be changed after creation.

Example: List Literals

my_list = [1, 2, 3, 4, 5]
print(my_list)


# Output
[1, 2, 3, 4, 5]

Explanation: The square brackets tell Python that these values belong to a list. Because lists are mutable, you can change their contents whenever needed.

Lists are one of the most frequently used data structures in Python. Explore them in detail in our guide on Python List Literals.

⬆ Move to Top

ii) Tuple Literals

A tuple literal stores an ordered collection of items inside parentheses (()). Unlike lists, tuples are immutable, which means their contents cannot be changed after they are created.

Example: Tuple Literal

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple)


# Output
(1, 2, 3, 4, 5)

Explanation: This example creates a tuple by placing the values inside parentheses. Once a tuple is created, its contents stay the same because tuples are immutable.

Need a collection that shouldn’t change? See how tuple literals work in our complete guide on Python Tuple Literals.

⬆ Move to Top

iii) Set Literals

A set literal stores a collection of unique values inside curly braces ({}). Duplicate values are automatically removed.

Example: Set Literals

my_set = {1, 2, 3, 4, 5}
print(my_set)


# Output
{1, 2, 3, 4, 5}

Explanation: Python creates a set from the values inside the curly braces. If the same value appears more than once, only one copy is kept.

Want to store only unique values? Explore how set literals make that easy in our guide on Python Set Literals.

⬆ Move to Top

iv) Dictionary Literals

A dictionary literal stores data as key-value pairs inside curly braces ({}). Each key is associated with a corresponding value, making dictionaries useful for organizing related information.

Example: Dictionary Literals

my_dict = {"name": "John", "age": 30, "city": "New York"}
print(my_dict)


# Output
{'name': 'John', 'age': 30, 'city': 'New York'}

Explanation: Each piece of information is stored as a key paired with its value. Instead of remembering positions, you can retrieve the data by referring to its key.

Working with key-value data? Learn how dictionary literals help organize information in our complete guide on Python Dictionary Literals.

⬆ Move to Top

Key Examples at a Glance: Python Literals

Literal Category Literal Type Example
Numeric Literals
Integer 42
Float 3.14
Complex 2 + 3j
String Literals
Single Quote ‘Hello’
Double Quote “Hello”
Multi-line ”’Hello”’
Boolean Literals
Boolean Values True, False
Special Literal
None None
Collection Literals
List [1, 2, 3]
Tuple (1, 2, 3)
Set {1, 2, 3}
Dictionary {“key”: “value”}

Key Takeaways: Python Literals

Here are a few points worth remembering:

  • A literal is any value written directly in Python code.
  • Numbers, text, True, False, None, and collections all have their own literal forms.
  • Each literal follows its own syntax, so the way you write a string is different from the way you write a list or dictionary.
  • You’ll see literals in almost every Python program, whether it’s a simple script or a larger application.
  • Getting comfortable with literals early makes many later Python topics easier to understand.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top