For loops

Introduction to Python for Developers

Jasmin Ludolf

Senior Data Science Content Developer

Individual comparisons

# Ingredient quantities
quantities = [500, 400, 15, 20, 30, 5]
# Validate values
quantities[0] < 10
False
quantities[1] < 10
False
Introduction to Python for Developers

For loop syntax

for value in sequence:
    action
  • for each value in sequence, perform this action

    • action is indented because of the colon in the previous line
  • sequence = iterable e.g., list, dictionary, etc.

  • value = iterator, i.e., the index
    • Placeholder (can give it any name), i is common
Introduction to Python for Developers

Print individual values

# Ingredients list
ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]


# Loop through and print each ingredient for ingredient in ingredients:
print(ingredient)
pasta
tomatoes
garlic
basil
olive oil
salt
Introduction to Python for Developers

Conditional statements in for loops

quantities = [1000, 800, 40, 30, 30, 15]

for qty in quantities:
Introduction to Python for Developers

Conditional statements in for loops

quantities = [1000, 800, 40, 30, 30, 15]

for qty in quantities:

# Check if quantity is more than 500 if qty > 500: print("Plenty in stock") elif qty >= 100: print("Enough for a small portion") else: print("Nearly out!")
Introduction to Python for Developers

Conditional statements in for loops

Plenty in stock
Enough for small
Nearly out!
Nearly out!
Nearly out!
Introduction to Python for Developers

Looping through strings

ingredient_name = "pasta"

# Loop through each character for letter in ingredient_name: print(letter)
p
a
s
t
a
  • Useful for validating text, checking for special characters
Introduction to Python for Developers

Looping through dictionaries

ingredients = {"pasta": 500, "tomatoes": 400, "garlic": 30}

# Loop through keys and values for item, qty in ingredients.items(): print(item, ":", qty, "grams")
pasta : 500 grams
tomatoes : 400 grams
garlic : 30 grams
  • item = key (ingredient name)
  • qty = value (quantity)
Introduction to Python for Developers

Looping through dictionaries

ingredients = {"pasta": 500, "tomatoes": 400, "garlic": 30}
factor = 2

# Calculate scaled quantities for item, qty in ingredients.items(): scaled_qty = qty * factor print(item, ":", scaled_qty, "grams")
pasta : 1000 grams
tomatoes : 800 grams
garlic : 60 grams
Introduction to Python for Developers

Looping through dictionaries

# Loop through keys only
for item in ingredients.keys():
    print(item)
pasta
tomatoes
garlic
# Loop through values only
for qty in ingredients.values():
    print(qty, "grams")
500 grams
400 grams
30 grams
Introduction to Python for Developers

Range

range(start, end + 1)
  • start = starting number
  • end = ending number
  • Used to generate or modify values
for i in range(1, 6):
    print(i)
1
2
3
4
5
Introduction to Python for Developers

Let's practice!

Introduction to Python for Developers

Preparing Video For Download...