Tuples in python: quick revision, 16 bites

All 16 revision bites from this kit, free and with no sign-up.

Tuple: Immutable Ordered Collection

Locked-in list, safer data.

A **tuple** is an ordered, immutable sequence of items in Python. Once created, you cannot change, add, or remove elements. This makes tuples ideal for fixed data like coordinates or configuration constants.

Takeaway. Tuples are immutable lists, perfect for data that should never change.

Tuple Syntax

Parentheses, not brackets.

Create a tuple using parentheses `()` or just commas. A single-element tuple needs a trailing comma to avoid ambiguity with parentheses for grouping.

Takeaway. Use parentheses and commas; remember the trailing comma for single-element tuples.

Tuple vs List

Mutable vs immutable choice.

Takeaway. Choose tuples for safety and speed when data shouldn't change.

Accessing Tuple Elements

Index and slice like lists.

Takeaway. Use square-bracket indexing and slicing just like with lists.

Tuple Unpacking

Assign multiple values at once.

**Tuple unpacking** lets you assign each element of a tuple to a separate variable in one line. This is a clean, Pythonic way to swap values or extract data.

Takeaway. Unpack tuples for elegant, readable assignments.

Tuples as Dictionary Keys

Immutable = hashable = key.

Because tuples are immutable, they are **hashable** and can be used as dictionary keys. Lists, being mutable, cannot. This is perfect for mapping composite keys to values.

Takeaway. Use tuples as dictionary keys when you need a compound identifier.

Forgetting Trailing Comma

Single-element tuple gotcha.

Takeaway. Always include a trailing comma for single-element tuples.

Returning Multiple Values

Functions return tuples implicitly.

Python functions that return multiple values actually return a tuple. This is a common pattern for returning related data without creating a custom class.

Takeaway. Use tuples to return multiple values from a function cleanly.

Remember: Tuple = Two + People

Couple stays together forever.

Think of **tuple** like 'two people' (a couple) who are **together permanently** — you can't change who's in the couple. The word 'tuple' sounds like 'couple', and couples are (ideally) immutable!

Takeaway. Tuple = couple = together forever = immutable.

When to Use Tuples

Fixed data, safer code.

Takeaway. Prefer tuples for constant collections; they make your intent clear.

Tuple Methods: count() and index()

Only two methods available.

Tuples have only two built-in methods: `count()` returns how many times a value appears, and `index()` returns the first index of a value.

Takeaway. Use count() and index() for basic tuple queries.

GPS Coordinates as Tuples

Latitude and longitude locked.

GPS coordinates are a perfect real-world tuple: latitude and longitude are always paired and never change for a fixed location. Storing them as a tuple prevents accidental modification and allows use as dictionary keys.

Takeaway. Tuples model fixed, paired data like coordinates naturally.

Tuple vs List in Exams

Spot the immutable one.

Exam questions often test the difference: if you see `()` it's a tuple (immutable); if you see `[]` it's a list (mutable). Remember: tuples cannot be changed after creation — no append, remove, or item assignment.

Takeaway. Parentheses = immutable; brackets = mutable.

Tuple is Like a Sealed Envelope

Contents fixed once sealed.

Think of a tuple as a **sealed envelope** containing items. You can look inside (access elements), count how many items are there, or pass the envelope to someone else. But you cannot add, remove, or change anything without breaking the seal.

Takeaway. Tuples are sealed containers — viewable but not modifiable.

Tuple Performance Edge

Faster than lists for iteration.

Because tuples are immutable, Python can optimize them internally. Iterating over a tuple is slightly faster than iterating over a list. For large datasets that don't change, tuples can give a small performance boost.

Takeaway. Tuples are faster than lists for read-only iteration.

Creating a Named Tuple

Tuple with field names.

For more readable code, use `namedtuple` from the `collections` module. It creates tuple-like objects with named fields, combining immutability with self-documenting code.

Takeaway. Use namedtuple for immutable objects with named fields.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages