2. Loops
Loops repeat code. Python has two loops:forwhile
for Loop
Used for iterating over sequences.Syntax
Loop through List
Loop through String
Using range()
range(stop)
range(start, stop)
range(start, stop, step)
Nested Loops
Loop through Dictionary
Loop Control Statements
break
Stops the loop immediately.
continue
Skips current iteration.
pass
Placeholder.
else with Loop
Runs only if loop finishes normally.
while Loop
Repeats while condition is True.Syntax
Example
Infinite Loop
break in while
continue in while
Quick Revision
Loops
for→ Iterate over sequences.while→ Repeat while condition is True.break→ Exit loop.continue→ Skip current iteration.pass→ Placeholder.- Loop
else→ Executes if loop completes normally.