Sitemap

Member-only story

Loops in Python {1}

4 min readDec 13, 2024

--

Hello,

Press enter or click to view image in full size
Photo by Tine Ivanič on Unsplash

1. Basic for Loop

python
Copy code
for i in range(5):
print(i) # 0, 1, 2, 3, 4

2. for Loop with List

python
Copy code
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

3. for Loop with Index (enumerate)

python
Copy code
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")

4. Nested for Loop

python
Copy code
for i in range(3):
for j in range(3):
print(f"i={i}, j={j}")

5. for Loop with break

python
Copy code
for i in range(10):
if i == 5:
break
print(i) # Stops at 4

6. for Loop with continue

python
Copy code
for i in range(10):
if i % 2 == 0:
continue
print(i) # Prints odd numbers

--

--