Understanding Python Tuple Literals
Python programs often need to store multiple related values together, such as names, numbers, prices, or coordinates. These values can be written directly in code using a tuple literal or created during program execution.
In this guide, you’ll learn what Python tuple literals are, their syntax, how they work, and how to use them with simple examples.
🚀 Getting Started: To see how tuple literals compare with other literal types, explore our guide on Python literal types and examples.
💡 Tip: To understand how Python stores and works with tuples internally, you can explore our guide on Python tuple() function.
Introduction: What is a Tuple Literal?
Definition: A Python tuple literal is a collection of one or more items written directly in Python code and enclosed within parentheses (()). Individual items are separated by commas, and each item can be any valid Python object, including numbers, strings, Boolean values, or even other tuples.
Example: In the statement numbers = (1, 2, 3), the value (1, 2, 3) is a tuple literal because it is written directly in the code and enclosed within parentheses.
Before exploring the different forms of Python tuple literals, let’s first understand their syntax.
How to Write Python Tuple Literals
Python allows tuple literals to be written directly in the source code without using any special function or constructor. A tuple literal is created by enclosing one or more items within parentheses and separating them with commas.
The syntax below shows the general structure of a Python tuple literal.
Syntax
tuple_name = (item1, item2, item3, ...)
Explanation: Here, tuple_name is the variable used to store the tuple, while item1, item2, and item3 represent the values stored in the tuple. Each item is separated by a comma, and the entire collection is enclosed within parentheses.
| Component | Description |
|---|---|
tuple_name |
The name of the variable used to store the tuple. |
= |
The assignment operator used to assign the tuple literal to a variable. |
( ) |
Parentheses that define the tuple literal. |
item1, item2, ... |
The values stored in the tuple. Each item is separated by a comma and may be of the same or different data type. |
Forms of Python Tuple Literals
Python tuple literals can be written in different forms depending on the type of data they contain:
Let’s explore each form of Python tuple literal with explanations and examples.
1. Tuple of Integers
Tuple literals can store integer values.
Example: Integer Tuple Literal
# Tuple of integers
numbers = (1, 2, 3, 4, 5)
print(numbers)
# Output
(1, 2, 3, 4, 5)
Explanation: The tuple contains only integer values. Each value is stored in the order in which it appears. Since tuples are immutable, these values cannot be modified after the tuple is created.
2. Tuple of Float Values
Tuple literals can also store floating-point numbers.
Example: Float Tuple Literal
# Tuple of float values
prices = (19.99, 45.50, 12.75, 3.90)
print(prices)
# Output
(19.99, 45.5, 12.75, 3.9)
Explanation: Every element in the tuple is a floating-point value representing a decimal number. Once created, the tuple cannot be modified.
3. Mixed Data Type Tuple
A tuple can store different types of values together.
Example: Mixed Data Type Tuple
# Mixed data type tuple
mixed_tuple = (10, "Hello", 3.14, True)
print(mixed_tuple)
# Output
(10, 'Hello', 3.14, True)
Explanation: This tuple contains four different data types: an integer, a string, a float, and a Boolean value. Python tuples can store different types of data in the same collection.
4. Nested Tuple
A tuple can also contain one or more tuples as its elements.
Example: Nested Tuple
# Nested tuple
nested_tuple = (1, 2, (3, 4, 5), 6)
print(nested_tuple)
# Output
(1, 2, (3, 4, 5), 6)
Explanation: The third element is another tuple. Nested tuples are commonly used to represent tables, matrices, coordinate systems, and other hierarchical data structures.
Key Examples at a Glance: Tuple Literals
The following table summarizes the different forms of Python tuple literals:
| Type | Format | Example | Meaning |
|---|---|---|---|
| Integer Tuple | (a, b, c) | (1, 2, 3, 4, 5) | Stores whole numbers |
| Float Tuple | (a, b, c) | (19.99, 45.50, 12.75, 3.90) | Stores decimal values |
| Mixed Data Type Tuple | (a, "text", 1.5, True) | (10, "Hello", 3.14, True) | Stores multiple data types |
| Nested Tuple | (a, (b, c), d) | (1, 2, (3, 4, 5), 6) | Contains another tuple as an element |
Key Takeaways: Tuple Literals
Here are the key points to remember about Python tuple literals:
- A Python tuple literal is created using parentheses (
()). - Tuple items are separated by commas.
- Tuples preserve the order of their elements.
- Tuples are immutable, so their values cannot be changed after creation.
- A tuple can store values of the same or different data types.
- Tuples can contain other tuples, making it easy to represent nested or hierarchical data.