Bird
Raised Fist0
Pythonprogramming~20 mins

Tuple indexing and slicing in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Tuple Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of tuple slicing with negative indices
What is the output of this Python code?
Python
t = (10, 20, 30, 40, 50, 60)
print(t[-4:-1])
A(40, 50, 60)
B(20, 30, 40)
C(30, 40, 50)
D(30, 40, 50, 60)
Attempts:
2 left
๐Ÿ’ก Hint
Remember that slicing excludes the end index and negative indices count from the end.
โ“ Predict Output
intermediate
2:00remaining
Result of tuple indexing with nested tuples
What will be printed by this code?
Python
t = (1, (2, 3), 4, (5, 6, 7))
print(t[3][1])
A6
B5
C7
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
First index selects the nested tuple, second index selects element inside it.
โ“ Predict Output
advanced
2:00remaining
Output of tuple slicing with step and negative step
What is the output of this code?
Python
t = (0, 1, 2, 3, 4, 5, 6)
print(t[5:1:-2])
A(5, 4, 3, 2)
B(5, 3)
C(5, 3, 1)
D()
Attempts:
2 left
๐Ÿ’ก Hint
Slicing with a negative step goes backwards and excludes the end index.
โ“ Predict Output
advanced
2:00remaining
Value of variable after tuple slicing and concatenation
What is the value of variable result after running this code?
Python
t = (1, 2, 3, 4, 5)
result = t[:2] + t[-2:]
print(result)
A(2, 3, 4)
B(1, 2, 3, 4, 5)
C(3, 4, 5)
D(1, 2, 4, 5)
Attempts:
2 left
๐Ÿ’ก Hint
Slicing t[:2] gets first two elements, t[-2:] gets last two elements, then they are joined.
โ“ Predict Output
expert
2:00remaining
Output of complex tuple slicing with omitted indices
What does this code print?
Python
t = (10, 20, 30, 40, 50, 60, 70)
print(t[::3][1:])
A(40, 70)
B(10, 40, 70)
C(30, 60)
D(40, 50, 60, 70)
Attempts:
2 left
๐Ÿ’ก Hint
First slice picks every 3rd element, second slice takes from index 1 to end.

Practice

(1/5)
1.

What does my_tuple[2] return if my_tuple = (10, 20, 30, 40)?

easy
A. 30
B. 20
C. 40
D. 10

Solution

  1. Step 1: Understand tuple indexing

    Indexing starts at 0, so index 2 means the third item in the tuple.
  2. Step 2: Identify the item at index 2

    The tuple is (10, 20, 30, 40), so the item at index 2 is 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Index 2 in tuple = 30 [OK]
Hint: Remember: indexing starts at zero in tuples [OK]
Common Mistakes:
  • Counting from 1 instead of 0
  • Confusing index 2 with index 3
  • Mixing up tuple with list indexing
2.

Which of the following is the correct syntax to get the last item of a tuple t?

easy
A. t[1]
B. t[-1]
C. t[last]
D. t[len(t)]

Solution

  1. Step 1: Recall negative indexing in tuples

    Negative index -1 accesses the last item in a tuple.
  2. Step 2: Check each option

    t[-1] correctly accesses the last item. t[1] accesses second item, t[last] is invalid syntax, t[len(t)] causes IndexError because indexing starts at 0.
  3. Final Answer:

    t[-1] -> Option B
  4. Quick Check:

    Last item index = -1 [OK]
Hint: Use -1 to get last item from any tuple [OK]
Common Mistakes:
  • Using len(t) as index (out of range)
  • Trying to use variable 'last' as index
  • Confusing positive and negative indexes
3.

What is the output of this code?

t = (5, 10, 15, 20, 25)
print(t[1:4])

medium
A. (10, 15, 20)
B. (10, 15, 20, 25)
C. (5, 10, 15)
D. (15, 20, 25)

Solution

  1. Step 1: Understand slicing syntax

    Slicing t[1:4] means start at index 1 up to but not including index 4.
  2. Step 2: Extract the slice from the tuple

    Tuple is (5, 10, 15, 20, 25). Index 1 is 10, index 2 is 15, index 3 is 20. Index 4 (25) is excluded.
  3. Final Answer:

    (10, 15, 20) -> Option A
  4. Quick Check:

    Slicing excludes end index [OK]
Hint: Slice excludes the end index, so stop before it [OK]
Common Mistakes:
  • Including the end index in slice
  • Confusing indexes with values
  • Using parentheses instead of brackets for slicing
4.

Find the error in this code:

t = (1, 2, 3, 4, 5)
print(t[5])

medium
A. Prints 5
B. SyntaxError
C. IndexError: tuple index out of range
D. Prints 1

Solution

  1. Step 1: Check tuple length and indexing

    Tuple has 5 items indexed 0 to 4. Index 5 is outside this range.
  2. Step 2: Understand what happens when accessing invalid index

    Accessing t[5] causes an IndexError because the index is out of range.
  3. Final Answer:

    IndexError: tuple index out of range -> Option C
  4. Quick Check:

    Index must be less than length [OK]
Hint: Index must be less than tuple length [OK]
Common Mistakes:
  • Using index equal to length
  • Confusing SyntaxError with IndexError
  • Assuming last index is length instead of length-1
5.

Given t = (2, 4, 6, 8, 10, 12), which expression returns a tuple with every second item starting from the last item going backwards?

hard
A. t[1::2]
B. t[::2]
C. t[-2::-1]
D. t[::-2]

Solution

  1. Step 1: Understand slicing with negative step

    A negative step means slicing backwards. t[::-2] starts from the end and takes every second item backwards.
  2. Step 2: Check what t[::-2] returns

    Tuple is (2,4,6,8,10,12). t[::-2] picks 12 (last), 8, 4, resulting in (12, 8, 4).
  3. Final Answer:

    t[::-2] -> Option D
  4. Quick Check:

    Negative step slices backwards skipping items [OK]
Hint: Use negative step in slice to go backwards [OK]
Common Mistakes:
  • Using positive step for backward slicing
  • Starting slice incorrectly
  • Confusing slice start and step positions