Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python Tuples: Immutable Sequences and Unpacking

Data structures · Tutorial 16

Python Tuples: Immutable Sequences and Unpacking

Use tuples for fixed groups of values, unpack them cleanly, and know when a tuple is a better choice than a list.

Open the compilerPractice exercises
beginner7 min readUpdated November 19, 20264 learning goals
On this page
  1. Access and iteration
  2. Immutability
  3. Unpacking
  4. Returning multiple values
  5. Tuple methods
  6. Tuple versus list
  7. Practice

What you will learn

  • Create tuples, including one-item tuples
  • Index, slice, and iterate over tuples
  • Unpack tuples into variables
  • Decide between tuples and lists
Before this lesson: Python Lists: Creation, Indexing, and Iteration

A tuple is an ordered sequence that cannot be changed after creation. Use parentheses and commas.

Python
point = (3, 5)
colours = ("red", "green", "blue")
single = (42,)
empty = ()

The comma creates the tuple. (42) is just the number 42 in parentheses; (42,) is a one-item tuple.

Access and iteration#

Python
point = (3, 5)

print(point[0])    # 3
print(point[-1])   # 5
print(len(point))  # 2

for coordinate in point:
    print(coordinate)

Slicing works exactly as it does for lists:

Python
numbers = (1, 2, 3, 4, 5)
print(numbers[1:4])   # (2, 3, 4)
print(numbers[::-1])  # (5, 4, 3, 2, 1)

Immutability#

Python
point = (3, 5)
point[0] = 7  # TypeError: 'tuple' object does not support item assignment

Immutability makes tuples predictable. They are hashable when all their items are hashable, so they can be dictionary keys or set members:

Python
locations = {
    (40.7128, -74.0060): "New York",
    (51.5074, -0.1278): "London",
}

print(locations[(51.5074, -0.1278)])

A list cannot be used as a dictionary key because it can change.

Unpacking#

Python
point = (3, 5)
x, y = point

print(x, y)

The number of names must match the number of items, unless you use a starred target:

Python
first, *middle, last = (1, 2, 3, 4, 5)

print(first)   # 1
print(middle)  # [2, 3, 4]
print(last)    # 5

Unpacking also swaps values without a temporary variable:

Python
a, b = 1, 2
a, b = b, a
print(a, b)

Returning multiple values#

A function that returns several values actually returns one tuple:

Python
def min_max(numbers):
    return min(numbers), max(numbers)

lowest, highest = min_max([4, 9, 1, 7])
print(lowest, highest)

This is idiomatic and readable for small fixed groups. If the result grows beyond a few values, a dictionary or a small class may communicate intent better.

Tuple methods#

Tuples have only two methods:

Python
values = (1, 2, 2, 3)

print(values.count(2))  # 2
print(values.index(2))  # 1

Tuple versus list#

Use a tuple when:

  • The group has a fixed shape, such as (latitude, longitude).
  • The values should not change.
  • You need a hashable key for a dictionary or set.

Use a list when:

  • You will add or remove items.
  • The number of items varies.
  • You need methods such as sort() or append().

Parentheses are optional in many places

Multiple values separated by commas form a tuple: point = 3, 5. Parentheses are often added for clarity. Empty parentheses create an empty tuple.

Practice#

Create a tuple for a book's title, author, and year. Unpack it into three variables and print a formatted sentence. Then build a dictionary where each key is a (row, column) tuple and print one entry.

Previous tutorialPython List Methods: append, insert, remove, sort, and MoreNext tutorialPython Dictionaries: Keys, Values, and Practical Patterns

Related tutorials

beginner

Python Dictionaries: Keys, Values, and Practical Patterns

Store and retrieve key-value data with dictionaries, iterate over items, and use get, update, and comprehensions.

9 min read
beginner

Python Functions: Define, Call, and Return Values

Write reusable code with def, pass arguments, return results, and use docstrings to document behaviour.

9 min read

On this page

  1. Access and iteration
  2. Immutability
  3. Unpacking
  4. Returning multiple values
  5. Tuple methods
  6. Tuple versus list
  7. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.