For Loop Examples

Carson West

For Loops

For Loop Examples

This note covers examples of using for loops in Python.

my_list = 1, 2, 3, 4, 5
for item in my_list:
    print(item)
my_string = "hello"
for char in my_string:
    print(char.upper())
my_dict = {"a": 1, "b": 2, "c": 3}
for key in my_dict:
    print(f"Key: {key}, Value: {my_dict[key]]}")

#Or using items():

for key, value in my_dict.items():
  print(f"Key: {key}, Value: {value}")
for i in range(5):  #Prints 0 to 4
    print(i)

for i in range(2, 10, 2): #Starts at 2, goes up to (but not including) 10, incrementing by 2.
  print(i)
for i in range(3):
    for j in range(2):
        print(f"i: {i}, j: {j}")
for i in range(5):
    if i == 3:
        break  # Exit the loop when i is 3
    print(i)

for i in range(5):
    if i == 2:
        continue # Skip the iteration when i is 2
    print(i)

List Comprehensions (Loop Control Statements) Range Function