Learning outcomes
By the end of this lecture, you should be able to:- use all
rangeforms confidently - handle reverse loops with negative step
- iterate without
rangeover strings/lists - avoid off-by-one loop errors
range behavior recap
Negative step examples
for loop without range
String traversal
List traversal
Dictionary traversal
Index + value using enumerate
Exam hints and traps
- stop value in
rangeis excluded - wrong sign in step can produce empty iteration
- direct iteration gives values, not indexes
Practice
- Print indexes and characters for
"python". - Reverse-print list
[3, 5, 7, 9]using range indexes. - Predict output:
Answers
- No output.
