Python Tutorial chapters (19)
  1. 01Python Introduction
  2. 02Python Syntax, Keywords & Operators
  3. 03Python Variables & Data Types
  4. 04Python Strings
  5. 05Python Control Statements
  6. 06Python Lists
  7. 07Python Tuples
  8. 08Python Sets
  9. 09Python Dictionaries
  10. 10Python Functions
  11. 11Python Modules
  12. 12Python OOPs
  13. 13Python Exception Handling
  14. 14Python File Handling
  15. 15Python Searching & Sorting
  16. 16Python Advanced Topics
  17. 17Python MySQL
  18. 18Python MongoDB & SQLite
  19. 19Python MCQ

Chapter 07

Python Tuples

2 topics · 1 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

A tuple is an ordered, immutable collection.

You will learn how to create tuples, pack and unpack them, slice them, and use their two methods count() and index().

Tuples protect data from accidental changes and are used for dictionary keys, function returns and fixed records.

Coordinates, RGB colours, database rows, returning multiple values from a function.

1

Python Tuples

A tuple is an ordered, immutable sequence of items, usually written inside round brackets like (1, 2, 3). Once created, its items cannot be added, removed or replaced.

In short: A tuple is a list that is locked. You can read it but not edit it.

Tuple = fixed ordered pack

Think about it

Immutability gives safety, slightly better speed, and allows tuples to be dictionary keys. A GPS location (latitude, longitude) should never be changed accidentally.

The comma, not the parentheses, creates a tuple: t = 1, 2, 3 works, and a single item needs (5,).

Tuples support indexing, negative indexing, slicing, len, in, + and *, all returning new tuples.

Packing means putting values into a tuple, and unpacking assigns its items to variables: x, y = (3, 4).

The number of variables must match unless you use a star: first, *rest = (1, 2, 3).

Swapping uses tuples: a, b = b, a.

A function that does return a, b actually returns one tuple.

Because tuples are immutable they are hashable (if every item is hashable), so they can be used as dict keys.

Immutability is shallow: a mutable object inside (like a list) can still change.

To "modify" a tuple, convert to a list, edit and convert back.

Key points

  • Packing: Putting several values into one tuple.
  • Unpacking: Assigning tuple items to separate variables.
  • Star unpacking: *name collects the remaining items into a list.
  • Immutable: Items cannot be changed after creation.

Example

Code
Tuple creation, packing and unpacking:
t = (10, 20, 30, 40)
print(t[0], t[-1], t[1:3])     # 10 40 (20, 30)

single = (5,)
not_tuple = (5)
print(type(single), type(not_tuple))   # tuple int

packed = 1, 2, 3               # packing without brackets
a, b, c = packed               # unpacking
print(a, b, c)                 # 1 2 3

first, *rest = (1, 2, 3, 4)
print(first, rest)             # 1 [2, 3, 4]

x, y = 3, 4
x, y = y, x                    # swap using tuples
print(x, y)                    # 4 3

def min_max(nums):
    return min(nums), max(nums)   # returns one tuple
lo, hi = min_max([4, 9, 1])
print(lo, hi)                  # 1 9

print((1, 2) + (3,), (0,) * 3) # (1, 2, 3) (0, 0, 0)

lst = list(t)                  # to edit, convert to list and back
lst[0] = 99
t = tuple(lst)
print(t)                       # (99, 20, 30, 40)

for item in t:                 # tuples are iterable
    print(item)
2

Python Tuple Methods

A tuple has only two built-in methods: count(x) returns how many times x occurs and index(x) returns the position of the first x. Common built-in functions such as len, min, max, sum, sorted and tuple also work with tuples.

In short: Because tuples cannot change, they need only two methods: count things and find things.

Think about it

Exams ask which methods tuples have and what operations are legal. Counting how many times a result occurs in a fixed data record.

`t.

count(x)` returns 0 if x is absent.

`t.

index(x, start, end)` raises ValueError if x is not found, so check with in first.

Since there is no append or remove, changing contents means creating a new tuple.

sorted(t) returns a LIST, not a tuple, so wrap it with tuple(sorted(t)) if you need a tuple.

sum, min and max work when the items are comparable.

`tuple(iterable)` converts a list, string, set or range into a tuple.

Tuple comparison is lexicographic: (1, 2) < (1, 3) is True.

Tuples can be compared and sorted, which is why they are used as sort keys.

Key points

  • count(): Number of occurrences of a value.
  • index(): Position of the first occurrence; ValueError if missing.
  • tuple(): Constructor converting an iterable to a tuple.

Example

Code
Tuple methods and helper functions:
t = (3, 1, 2, 3, 3)
print(t.count(3))          # 3
print(t.index(2))          # 2
print(t.index(3, 1))       # 3  (search starts at position 1)
print(t.count(9))          # 0

if 9 in t:
    print(t.index(9))      # safe: only runs when present

print(len(t), min(t), max(t), sum(t))   # 5 1 3 12
print(sorted(t))                        # [1, 2, 3, 3, 3]  (a list)
print(tuple(sorted(t)))                 # (1, 2, 3, 3, 3)

print(tuple("abc"))        # ('a', 'b', 'c')
print(tuple(range(3)))     # (0, 1, 2)
print((1, 2) < (1, 3))     # True

# t.append(4)  -> AttributeError: tuple has no append

Solved examples

Example 01 · Basic

What is the output of t = (1, 2, 2, 3); print(t.count(2), t.index(3))?

Given Data:
    Required To Find:

    Two numbers

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: 2 appears twice
    Step 2: 3 is at index 3
    Final Answer: 2 3

    Approach note: Index starts from 0.

    Example 02 · Basic

    Swap the values of a = 5 and b = 8 without a temporary variable.

    Given Data:
      Required To Find:

      Swapped values

      Formula Applied:—
      Step-by-Step Solution:
      Step 1: The right side builds a tuple (b, a)
      Step 2: It is unpacked into a and b
      Final Answer: a, b = b, a # a = 8, b = 5

      Approach note: The right side is fully evaluated before assignment.

      Example 03 · Intermediate

      Change the first item of t = (1, 2, 3) to 100.

      Given Data:
        Required To Find:

        New tuple

        Formula Applied:—
        Step-by-Step Solution:
        Step 1: Convert to list
        Step 2: Change item 0
        Step 3: Convert back to tuple
        Final Answer: l = list(t); l[0] = 100; t = tuple(l) # (100, 2, 3)

        Approach note: This builds a new tuple; the old one is not modified.

        Example 04 · Basic

        Unpack t = (5, 10, 15, 20) into a, *mid, b and print a, mid, b.

        Given Data:
          Required To Find:

          Unpacked values

          Formula Applied:—
          Step-by-Step Solution:
          Step 1: a takes the first element 5
          Step 2: *mid collects the middle as a list [10, 15]
          Step 3: b takes the last element 20
          Final Answer: a, *mid, b = (5, 10, 15, 20) print(a, mid, b) # 5 [10, 15] 20

          Approach note: Exactly one starred target is allowed in an unpack.

          Real-world applications

          Python

          Using Python Tuples in real projects

          Coordinates, RGB colours, database rows, returning multiple values from a function.

          Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

          Common mistakes

          ❌ Myth: Tuples are just slow lists.

          Why this is wrong: Tuples are slightly faster and smaller.

          ✅ Verified Truth: They are chosen for safety and hashability.

          ⚠️ Exam Tip: Mention dict keys in interviews.

          ❌ Myth: An immutable tuple can never change.

          Why this is wrong: Only the tuple structure is fixed.

          ✅ Verified Truth: A list inside a tuple can still be modified.

          ⚠️ Exam Tip: Immutability is shallow.

          ❌ Myth: Parentheses make a tuple.

          Why this is wrong: Commas make a tuple.

          ✅ Verified Truth: 1, 2 is a tuple; (1) is not.

          ⚠️ Exam Tip: Single-item tuples need a trailing comma.

          Revision notes

          A tuple is an ordered, immutable collection. You will learn how to create tuples, pack and unpack them, slice them, and use their two methods count() and index().

          High-Yield Quick Revision Points

          1. Tuple: ordered, immutable, ( )
          2. Comma creates tuple: (5,)
          3. Unpacking: a, b = t; a, *rest = t
          4. Only count() and index()
          5. sorted(t) returns a list
          6. Hashable if all items hashable

          Practice questions

          Question 1 · MCQ (Interview / semester)

          Which type is (7)?

          Question 2 · MCQ (Interview / semester)

          Which methods does a tuple have?

          Question 3 · MCQ (Interview / semester)

          What does a, *b = (1, 2, 3) give for b?

          Question 4 · MCQ (Interview / semester)

          What does sorted((3, 1, 2)) return?

          Question 5 · MCQ (Interview / semester)

          What happens with t[0] = 5 on a tuple?

          Question 6 · MCQ (Interview / semester)

          A function returning "return a, b" returns: