Sitemap

Loops in Python

3 min readJun 3, 2026

--

# FOR LOOPS IN PYTHON

ingredients = [“fusilli”, “tomatoes”, “garlic”, “basil”, “olive oil”, “salt”]

# Loop through each ingredient in the list

for item in ingredients:

print(item)

# Iterate over the number of ingredients

for item in range(1,7):

print(“Adding ingredient”, item)

# example — -

quantities = [500, 400, 20, 15, 15, 7]

# Loop through each quantity in the recipe

for qty in quantities:

# Check if it’s a large quantity (400g or more)

if qty >= 400:

print(‘Large quantity’)

# Check if it’s a medium quantity (200g or more)

elif qty >= 200:

print(‘Medium quantity’)

# Otherwise it’s a small quantity

else:

print(‘Small quantity’)

# example — — —

recipe = {

“fusilli”: 500,

“tomatoes”: 400,

“basil”: 20,

“garlic”: 15,

“olive oil”: 15,

“salt”: 7

}

# Loop through the recipe dictionary items

for ingredient, qty in recipe.items():

# Calculate the scaled quantity by multiplying by 2

scaled_qty = qty *2

print(ingredient, “:”, scaled_qty, “g”)

#While Loops in Python

total_confirmations = 10

guest_count = 0

# Count confirmations using a while loop

while guest_count<total_confirmations:

guest_count+=1

print(guest_count, “guests so far!”)

print(“We have”, guest_count, “guests coming!”)

# example — -

total_ingredients = 7

ingredients_checked = 0

# Set up the loop

while ingredients_checked < total_ingredients:

# Increment the counter

ingredients_checked += 1

# Check if less than 4 ingredients reviewed

if ingredients_checked < 4:

print(“More than half remaining”)

# Check if 6 or fewer ingredients reviewed

elif ingredients_checked <= 6:

Get Techsolutionsold’s stories in your inbox

Join Medium for free to get updates from this writer.

print(“Nearly finished checking”)

else:

print(“All ingredients verified!”)

# example — -

# Create an empty shopping list

shopping_list = []

# Loop through each ingredient and required quantity

for ingredient, required_qty in recipe.items():

# Check if we need more than what we have

if required_qty > pantry_stock[ingredient]:

# Add the ingredient to our shopping list

shopping_list.append(ingredient)

# Display the shopping list

print(“Shopping list:”, shopping_list)

# example — -

# Count how many items to buy

items_to_buy = 0

for item in shopping_list:

items_to_buy += 1

# Display results

print(shopping_list)

print(items_to_buy)

# example — -

shopping_list = []

# Loop through each ingredient and amount in the recipe

for ingredient, amount in recipe.items():

print(ingredient)

# example — — Inside the loop, calculate the needed_amount by multiplying the amount by the scale_factor.

shopping_list = []

# Loop through each ingredient and amount in the recipe

for ingredient, amount in recipe.items():

# Calculate the amount needed for the party

needed_amount = amount*scale_factor

# example — — Check if the ingredient is not in pantry OR if the needed_amount is greater than the quantity in pantry[ingredient]. If either condition is true, append the ingredient to shopping_list.

shopping_list = []

# Loop through each ingredient and amount in the recipe

for ingredient, amount in recipe.items():

# Calculate the amount needed for the party

needed_amount = amount * scale_factor

# Check if we need to buy this ingredient

if ingredient not in pantry or needed_amount > pantry[ingredient]:

shopping_list.append(ingredient)

print(“Shopping list for your party:”)

print(shopping_list)

# loops

def validate_password(password):

# Check if password is at least 8 characters long

if len(password) >= 8:

# Check if password contains a special character

for char in password:

if char in string.punctuation:

return True

return False

# Call the function and store the result

is_valid = validate_password(user_password)

print(“Is the password valid? “, is_valid)

--

--