Python Loops Guide
Python Loops Guide
1. What is a Loop?
A loop is used to repeat a block of code multiple times. Python has two types of loops: 'for' and 'while'.
# code block
print(i)
print(i)
Note:
Format:
initialization
while condition:
# code block
increment/decrement
i=1
while i <= 5:
Python Loops and Range - Explained
print(i)
i += 1
for i in range(5):
if i == 3:
break
print(i)
for i in range(5):
if i == 2:
continue
print(i)
5. Nested Loops
Example:
print(f"i={i}, j={j}")
6. Summary Table
|-----------|-----------------------|----------------------------|