Skip to main content

Python Tutorial 4.2 - Tuples in Python

Welcome to the “Tuples in Python” tutorial!


Overview

Tuples are ordered, immutable sequences used to store heterogeneous data.

Creating Tuples

colors = ("red", "green", "blue")
single = (42,)  # single‑element tuple requires a comma
empty = ()

Tuple Operations

  • Indexing & Slicing:
    print(colors[1])     # "green"
    print(colors[:2])    # ("red", "green")
    
  • Immutability:
    Elements cannot be changed once assigned.
    # colors[0] = "yellow"  # Error!
    
  • Packing & Unpacking:
    point = (10, 20)
    x, y = point
    

Key Takeaways

  • Tuples provide data integrity with immutability.
  • Use for fixed collections or as keys in dictionaries.
  • Packing and unpacking enable clear multiple assignments.

Happy coding!

Comments