# Print the numbers from 1 to 10
for i in range(1, 11):
print(i)
For loop with an else clause:
# Print the numbers from 1 to 10, and print a message if the loop finishes normally
for i in range(1, 11):
print(i)
else:
print("The loop finished normally")
While loop:
# Print the numbers from 1 to 10
i = 1
while i <= 10:
print(i)
i += 1
Nested loops:
# Print the multiplication table for numbers from 1 to 3
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i*j}")
Break statement in a loop:
# Print the numbers from 1 to 10, but stop when the number is 5
for i in range(1, 11):
if i == 5:
break
print(i)
Continue statement in a loop:
# Print the numbers from 1 to 10, but skip the number 5
for i in range(1, 11):
if i == 5:
continue
print(i)
Loop over a list:
# Print the elements of a list
items = [1, 2, 3, 4, 5]
for item in items:
print(item)
Loop over a dictionary:
# Print the keys and values of a dictionary
items = {'a': 1, 'b': 2, 'c': 3}
for key, value in items.items():
print(f"{key}: {value}")
Loop over a string:
# Print the characters of a string
string = "Hello, world!"
for char in string:
print(char)
Loop over a file:
# Print the lines of a file
with open('file.txt', 'r') as f:
for line in f:
print(line)