Python Learning Series · @onlysamarrr · Day 16 / 30

Day 16: Tuples

Day 16 teaches tuples. A tuple is like a list, but it is immutable: once created it cannot be changed. That makes it usable as a dictionary key and slightly faster than a list.

Phase: Intermediate · Reels: 76/450 – 80/450

Quick Answer

Day 16 teaches tuples. A tuple is like a list, but it is immutable: once created it cannot be changed. That makes it usable as a dictionary key and slightly faster than a list.

What You Will Learn

Code

point = (3, 4)
print(point, type(point))
print(type((5)))
print(type((5,)))
a, b = 1, 2
a, b = b, a
print(a, b)
first, *rest = [1, 2, 3, 4]
print(first, rest)

Output

(3, 4) <class 'tuple'>
<class 'int'>
<class 'tuple'>
2 1
1 [2, 3, 4]

Code Explained, Line by Line

  1. point = (3, 4) creates a tuple inside round brackets.
  2. type((5)) is int, because without a comma it is just a value in brackets.
  3. type((5,)) is tuple: a single-item tuple needs a trailing comma.
  4. a, b = b, a swaps two values without a temporary variable, using packing and unpacking.
  5. first, *rest = [1, 2, 3, 4] puts the first item in first and the rest in a list called rest.
Common Mistake: The most common tuple mistake is forgetting the comma: (5) is an int, (5,) is a tuple.

Real-Life Use

Coordinates, RGB colours and database rows are often tuples, because they should not change by accident.

Practice Task: Swap two values using unpacking and explain the difference between a list and a tuple.

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What is type((5))?

Q2. What is type((5,))?

Q3. What does a, b = b, a do?

Today's 5 Reels (from the 90-Day Plan Excel)

76/450ConceptTuple kya hai
77/450Live demoPacking aur unpacking
78/450Common mistakeSingle item tuple ki galti
79/450QuizQuiz: values swap karo
80/450Interview / real lifeInterview: list vs tuple
← Day 15Day 17 →