0
0
Pythonprogramming~10 mins

Tuple indexing and slicing in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Tuple indexing and slicing
Start with tuple
Choose index or slice
If index: get single element
If slice: get sub-tuple
Output result
End
We start with a tuple, pick an index or a slice, then get either one element or a smaller tuple as output.
Execution Sample
Python
t = (10, 20, 30, 40, 50)
print(t[2])
print(t[1:4])
This code gets the element at index 2 and a slice from index 1 to 3 from the tuple.
Execution Table
StepExpressionEvaluationResult
1t = (10, 20, 30, 40, 50)Create tuple(10, 20, 30, 40, 50)
2t[2]Get element at index 230
3t[1:4]Get slice from index 1 to 3(20, 30, 40)
4EndNo more expressionsExecution stops
💡 All expressions evaluated, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
tundefined(10, 20, 30, 40, 50)(10, 20, 30, 40, 50)(10, 20, 30, 40, 50)(10, 20, 30, 40, 50)
result_indexundefinedundefined303030
result_sliceundefinedundefinedundefined(20, 30, 40)(20, 30, 40)
Key Moments - 2 Insights
Why does t[2] return a single number but t[1:4] returns a tuple?
Because t[2] uses a single index to get one element (row 2 in execution_table), while t[1:4] uses a slice which returns a new tuple with elements from index 1 up to but not including 4 (row 3).
What happens if the slice end index is beyond the tuple length?
Python safely returns elements up to the end of the tuple without error. This is shown by the slice behavior in row 3, which stops before index 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of t[2] at step 2?
A20
B30
C(30, 40)
D40
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the slice t[1:4] get evaluated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the expression 't[1:4]' in the execution_table rows.
If we change t[1:4] to t[1:10], what would happen to the result_slice variable?
AIt would return elements from index 1 to the end of the tuple
BIt would return the same slice as before
CIt would cause an error
DIt would return an empty tuple
💡 Hint
Remember Python slices safely handle end indexes beyond tuple length, as explained in key_moments.
Concept Snapshot
Tuple indexing and slicing:
- Use t[index] to get one element.
- Use t[start:end] to get a sub-tuple slice.
- Indexing returns a single value.
- Slicing returns a new tuple.
- Slice end is exclusive and safe beyond length.
Full Transcript
This visual execution shows how tuple indexing and slicing work in Python. We start by creating a tuple t with five numbers. Then we get the element at index 2, which is 30, a single number. Next, we get a slice from index 1 to 4, which returns a new tuple with elements 20, 30, and 40. The slice end index is exclusive, so it stops before index 4. The variable tracker shows how the tuple stays the same, while the results for index and slice change as we evaluate expressions. Key moments clarify why indexing returns one element and slicing returns a tuple, and how slices handle end indexes beyond the tuple length safely. The quiz tests understanding of these steps and results.