www.codegalatta.
com
Python For Loops
1. Introduction to For Loops
• A for loop is used to iterate over a sequence (like a list, tuple, string,
dictionary, or range).
• Executes a block of code once for each item in the sequence.
2. Looping Through a String
• Strings are iterable character-by-character.
3. Looping Through a Range
• Use range(start, stop, step) to generate a sequence of numbers.
• When no start is provided, it defaults to 0. When no step is provided, it
defaults to 1.
• A negative step allows counting down, but the start must be greater than
stop.
1
www.codegalatta.com
• If no valid numbers exist between start and stop with the given step, the
range will be empty.
4. Looping Through a Dictionary
• Loop through keys, values, or key-value pairs.
5. Nested For Loops
• A loop inside another loop.
2
www.codegalatta.com
6. Using break in For Loop
• break exits the loop prematurely.
7. Using continue in For Loop
• continue skips the current iteration.
8. Using else with For Loop
• else block runs when the loop finishes normally (no break).