Sitemap
Saro

Saro

Member-only story

Loops in Python {2}

4 min readDec 13, 2024

--

Hello,

Press enter or click to view image in full size
Photo by XVIIIZZ on Unsplash

31. Iterate Over Lines in a File

python
Copy code
with open('example.txt', 'r') as file:
for line in file:
print(line.strip())

32. Loop Over a Range with Floating Point Steps

python
Copy code
start, end, step = 0, 1, 0.2
while start < end:
print(round(start, 2))
start += step

33. Iterate Over a String in Reverse

python
Copy code
string = "hello"
for char in reversed(string):
print(char)

34. Skip Specific Elements in a for Loop

python
Copy code
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 3:
continue
print(num) # Skips 3

35. Loop Through Two Lists Using zip

python
Copy code
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
for x, y in zip(list1, list2):
print(x, y)

36. while Loop for Exponential Growth

--

--