0
0
Pythonprogramming~5 mins

Tuple indexing and slicing in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is tuple indexing in Python?
Tuple indexing means accessing individual elements of a tuple using their position number, starting from 0 for the first element.
Click to reveal answer
beginner
How do you access the last element of a tuple named t?
You use negative indexing: t[-1] gives the last element of the tuple.
Click to reveal answer
beginner
What does tuple slicing do?
Tuple slicing extracts a part of the tuple by specifying a start and end position, returning a new tuple with those elements.
Click to reveal answer
beginner
Explain the syntax of tuple slicing with an example.
Syntax: tuple[start:end] extracts elements from index start up to but not including end. For example, t = (10, 20, 30, 40); t[1:3] returns (20, 30).
Click to reveal answer
beginner
Can you change an element of a tuple using indexing?
No, tuples are immutable. You cannot change elements by indexing. You can only read elements.
Click to reveal answer
What will t[2] return if t = (5, 10, 15, 20)?
A20
B10
C15
D5
What does t[-2] return for t = (1, 2, 3, 4, 5)?
A2
B3
C5
D4
What is the result of t[1:4] if t = (7, 8, 9, 10, 11)?
A(8, 9, 10)
B(7, 8, 9, 10)
C(9, 10, 11)
D(8, 9, 10, 11)
Which of these is NOT a valid way to slice a tuple t?
At[2:]
Bt(1:3)
Ct[1:5:2]
Dt[:3]
Can you assign a new value to t[0] if t = (1, 2, 3)?
ANo, tuples are immutable.
BYes, tuples are mutable.
COnly if you use slicing.
DOnly if you convert to a list first.
Describe how to access elements in a tuple using indexing and negative indexing.
Think about counting positions from the start and from the end.
You got /3 concepts.
    Explain how tuple slicing works and what the result looks like.
    Imagine cutting a piece from a string or a list.
    You got /4 concepts.