Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Created September 14, 2026 10:08
Show Gist options
  • Select an option

  • Save luisjunco/7fa1eb3c55ac5c03ee2a8fc16714b0a5 to your computer and use it in GitHub Desktop.

Select an option

Save luisjunco/7fa1eb3c55ac5c03ee2a8fc16714b0a5 to your computer and use it in GitHub Desktop.
Exercise to practice Python for loops

Practice: for loop

Iteration 1

You have a list of AI models. Print each one on its own line.

models = ["GPT-4", "Claude", "Gemini", "Llama", "Mistral"]

Expected output:

GPT-4
Claude
Gemini
Llama
Mistral
Solution – Iteration 1
models = ["GPT-4", "Claude", "Gemini", "Llama", "Mistral"]

for model in models:
    print(model)

Iteration 2

Now print each model with its position in the list (starting at 1).

Expected output:

1. GPT-4
2. Claude
3. Gemini
4. Llama
5. Mistral
Solution – Iteration 2
models = ["GPT-4", "Claude", "Gemini", "Llama", "Mistral"]

for i, model in enumerate(models):
    print(f"{i + 1}. {model}")

Bonus 1

Count how many models have a name shorter than 6 characters, and print the result.

Solution – Bonus 1
models = ["GPT-4", "Claude", "Gemini", "Llama", "Mistral"]

count = 0
for model in models:
    if len(model) < 6:
        count += 1

print(f"Models with short names: {count}")

Bonus 2

Ask the user to enter a model name. Loop through the list and print "Found it!" if it matches, or "Not found." if it doesn't.

Solution – Bonus 2
models = ["GPT-4", "Claude", "Gemini", "Llama", "Mistral"]

search = input("Enter a model name: ")

for model in models:
    if model == search:
        print("Found it!")
        break
else:
    print("Not found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment