🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Tuples

AI & DATA SCIENCE // tuples

A tuple is Python's built-in, immutable, ordered sequence type, commonly used for fixed collections of values.

Syntax

point = (3, 4)
single = (5,)
x, y = point

Deep Dive Course

Tuples look and behave like lists for reading — indexing, slicing, iteration, containment checks — but they can never be modified after creation: no append, no item assignment. That immutability makes them hashable, when their contents are also hashable, so they can be used as dictionary keys or set elements, and it also signals to readers that the collection represents a fixed, related group of values, like a coordinate pair or a database row.

1Understanding Tuples

Tuples look and behave like lists for reading — indexing, slicing, iteration, containment checks — but they can never be modified after creation: no append, no item assignment. That immutability makes them hashable, when their contents are also hashable, so they can be used as dictionary keys or set elements, and it also signals to readers that the collection represents a fixed, related group of values, like a coordinate pair or a database row.

💡

Tuple unpacking (x, y = point) is one of Python's most useful idioms — it works for function return values, swapping variables, and iterating over pairs from zip() or dict.items().

editor.html
point = (3, 4)
x, y = point
print(x, y)
localhost:3000

2Practical Example

Here is a real-world application of Tuples showing how it is used in production Python code.

editor.html
def min_max(numbers):
    return min(numbers), max(numbers)

lo, hi = min_max([4, 1, 9, 2])
print(lo, hi)
localhost:3000

3Best Practices

Follow these guidelines when working with Tuples:

1. Use a tuple to return multiple values from a function instead of a custom container class

2. Prefer tuple unpacking over indexing when consuming a fixed-size tuple, for readability

3. Use collections.namedtuple when a tuple's fields need descriptive names instead of positional meaning

⚠️

Tip: Tuple unpacking (x, y = point) is one of Python's most useful idioms — it works for function return values, swapping variables, and iterating over pairs from zip() or dict.items().

editor.html
point = (3, 4)
x, y = point
print(x, y)
localhost:3000

Examples

Example 01Basic Usage
point = (3, 4)
x, y = point
print(x, y)
Example 02Advanced Example
def min_max(numbers):
    return min(numbers), max(numbers)

lo, hi = min_max([4, 1, 9, 2])
print(lo, hi)

Best Practices

  • Use a tuple to return multiple values from a function instead of a custom container class
  • Prefer tuple unpacking over indexing when consuming a fixed-size tuple, for readability
  • Use collections.namedtuple when a tuple's fields need descriptive names instead of positional meaning

Interview Question

How does swapping two variables with `a, b = b, a` work without a temporary variable, under the hood?

Hint: Think about the order tuple packing and unpacking happen in.

Python first evaluates the entire right-hand side, packing the current values of b and a into a temporary tuple — that packing happens before any assignment occurs. It then unpacks that tuple into a and b on the left, in order. Because the right side is fully evaluated first, no explicit temporary variable is needed in the source code, even though one effectively exists in the temporary tuple.

Exercises

MediumPractice using Tuples in a real scenario.
View Solution
point = (3, 4)
x, y = point
print(x, y)

Frequently Asked Questions

How does swapping two variables with `a, b = b, a` work without a temporary variable, under the hood?

Python first evaluates the entire right-hand side, packing the current values of b and a into a temporary tuple — that packing happens before any assignment occurs. It then unpacks that tuple into a and b on the left, in order. Because the right side is fully evaluated first, no explicit temporary variable is needed in the source code, even though one effectively exists in the temporary tuple.

Related Functions

listsnamedtupledictionaries