10 Python Dry-Run Patterns for Beginners
Master Python output prediction with essential dry-run patterns covering loop boundaries, list slicing, and scope resolution.
Online coding practice is most effective when you practice mental execution tracing rather than trial-and-error running. Developing the discipline to predict the exact output before pressing run is the fastest way to build software engineering intuition.
1. Off-by-One Range Boundary Traps
total = 0
for i in range(1, 5):
total += i
print(total)Python's range(1, 5) stops strictly BEFORE 5. The loop iterates over 1, 2, 3, and 4. The sum is 10, not 15. Always remember that the stop index in Python ranges is exclusive.
2. Negative Step String Slicing
text = 'Python' print(text[::-1]) print(text[4:1:-1])
Slicing with a step of -1 traverses from right to left. text[::-1] reverses the whole word to 'nohtyP'. text[4:1:-1] starts at index 4 ('o') and steps backward until index 1 (exclusive), yielding 'oht'.
3. List Mutation vs Reassignment
nums = [1, 2, 3] nums.append(4) nums = nums + [5] print(nums)
nums.append(4) mutates the existing list in place. nums = nums + [5] creates a brand new list in memory and rebinds the variable name nums to it. The final output is [1, 2, 3, 4, 5].
4. Dictionary Key Overwrites
data = {'a': 1, 'b': 2}
data['a'] = 100
print(len(data), data['a'])Dictionary keys are unique. Assigning to an existing key updates its value in place without increasing the dictionary length. The output is 2 100.
Quick Concept Quiz
What does range(2, 6) produce when converted to a list in Python?
Ready to test your execution tracing skills?
Hop directly into the Python trace map to start coding, grading queries, and logging XP metrics to your workspace profile.