for
loopswhile
loopsfor
Loopfor
loop is used to iterate over a sequence (e.g., list, tuple, string, range) or other iterable objects.Example:
Output:
range()
in for
Loopsrange()
function generates a sequence of numbers.Example:
Output:
while
Loopwhile
loop repeats a block of code as long as a condition is True
.Example:
Output:
break
: Exits the loop immediately.continue
: Skips the rest of the code in the current iteration and moves to the next iteration.pass
: A placeholder for future code (does nothing).Examples:
break
:
Output:
continue
:
Output:
pass
:
Output:
Example:
Output:
Examples:
List:
String:
Dictionary:
for
Loop:
while
Loop:
Nested Loops:
Loop Control:
name
instead of x
).while
loops by ensuring the condition eventually becomes False
.break
and continue
sparingly to keep the code readable.Example: