# 03 - Loops **Notebook:** `03-loops.ipynb` ## Video <iframe width="720" height="406" src="https://www.youtube.com/embed/u2H66QIUi-Q?si=phM7ydtUKFUviq-Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> --- ## This is Where the Magic Happens Up till now, we've been getting to see the words "The Raven" show up. Wow. I could've done that myself by typing! We're not actually gaining a whole lot through coding just yet. In order to do that, we're going to start wanting to do things **at scale**. This is why we've made this encyclopedia exercise the very first one—because if you're going to not type things manually into ChatGPT, we want to make sure you understand what the payoff of that might be. And the payoff is by way of **loops** and **lists**. --- ## The For Loop Here's the syntax for a [**loop**](/glossary/coding-basics-py/loop) in Python: ```python for item in list: do_something(item) ``` Let's break this down: - `for` — keyword that starts the loop - `item` — a new variable name we create (we get to choose this!) - `in` — very important word, we don't control that, gotta use the word "in" - `list` — the list we're looping through - `:` — colon at the end of the line - Indented code — what we want to do with each item The variable `item` that we define up here needs to be used down here in the indented code. --- ## How It Works Here's the interesting thing: unlike the variables we had earlier where they kept having the same value, what's going to happen here is this little line of code (and any other lines indented underneath it) is going to be **run for every single item in the list**. So if we have: ```python poems = ["The Raven", "Ode to a Nightingale", "Hope is the Thing with Feathers"] for poem in poems: print(poem) ``` Output: ```text The Raven Ode to a Nightingale Hope is the Thing with Feathers ``` The first time through the loop, `poem` is "The Raven". The second time, `poem` is "Ode to a Nightingale". The third time, `poem` is "Hope is the Thing with Feathers". That's the magic happening in a loop: **for every single element in this list, it's going to do X with that element**. --- ## Naming Your Loop Variable We could call the loop variable something else. We could call it `i` if we wanted: ```python for i in poems: print(i) ``` This would work the same way. But I recommend using a descriptive name like `poem` — it's more intuitive and you won't get confused about what's happening, especially if you have a loop within a loop later on. --- ## Notice the Difference This output is different from when we just printed `poems` directly: ```python print(poems) # ['The Raven', 'Ode to a Nightingale', 'Hope is the Thing with Feathers'] ``` When we loop through and print each one individually, they're on separate lines, printed as plain strings. We're calling the print function on every single one of the strings in isolation—not on the whole list. --- ## Making It More Complex This is still not a big enough payoff. We're ultimately going to make the thing that happens inside the loop **huge**. Once we start asking OpenAI questions about every single one of these poems, we'll see the real power. Let's try something slightly more complex: ```python for poem in poems: print(f"Analyzing {poem}") print(f"Title has {len(poem)} characters") ``` Output: ```text Analyzing The Raven Title has 9 characters Analyzing Ode to a Nightingale Title has 20 characters Analyzing Hope is the Thing with Feathers Title has 31 characters ``` Interesting: we can use that `len()` function on a string as well! When it's a list, it tells us how many items are in the list. When we do it on a string, it tells us how many **characters** are in the string. --- ## The Payoff I hope it's starting to seem why this is worth your while. > **Once you can loop through a large list of stuff—like a giant list of poems—and then you can do something that can become really complex with every single element in that list... well then you kind of have superpowers.** And honestly, we're kind of there. You've learned the building blocks that we need to understand what was going on in that code earlier on. There are a couple more things we're going to look at, but this is where I hope you feel like we're getting there—to understanding **why we would want to use code** to call these LLMs instead of just manually typing into ChatGPT or Gemini. --- ## Try It 1. Create a list of items you're interested in 2. Write a loop that does something to each item 3. Try combining loops with f-strings to create more interesting output 4. Think about what happens when your list has 10 items, or 100, or 1000... --- ## Summary In this notebook, you learned: - A **for loop** runs code for every item in a list - The syntax is `for item in list:` followed by indented code - You get to **name** the loop variable (use something descriptive!) - `len()` works on strings too (counts characters) - Loops are where coding becomes **powerful** — doing things at scale Next up: **dictionaries** — more complex data structures!