literal
In Python, a literal represents a fixed value in your code. These are the raw data elements that don’t change during execution.
Python supports literals for the following data types:
- Stings
- Integer values
- Floating-point values
- Boolean values
- Lists and tuples
- Dictionaries and sets
Literals allow you to directly assign values to variables, making your code more readable and straightforward.
When you use literals, you hardcode values. This means that wherever the literal appears, it will always represent the same value. This is useful for initializing variables with known values or defining constants.
Examples
Here are some example demonstrating different types of literals in Python:
# String literal
greeting = "Hello, Pythonista!"
# Integer literal
age = 30
# Floating-point literal
pi = 3.14159
# Boolean literal
is_active = True
# List literal
fruits = ["apple", "orange", "mango"]
# Dictionary literal
numbers = {"one": 1, "two": 2, "three": 3}
In these examples, you assign literals to variables using the assignment operator.
Related Resources
Tutorial
Basic Data Types in Python: A Quick Exploration
In this tutorial, you'll learn about the basic data types that are built into Python, including numbers, strings, bytes, and Booleans.
For additional information on related topics, take a look at the following resources:
- Strings and Character Data in Python (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Dictionaries in Python (Tutorial)
- Sets in Python (Tutorial)
- Exploring Basic Data Types in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Using Dictionaries in Python (Course)
- Python Dictionaries (Quiz)
- Dictionaries in Python (Quiz)
- Using Sets in Python (Course)
- Python Sets (Quiz)
By Leodanis Pozo Ramos • Updated April 22, 2025