An interactive introduction to programming in Python, for human beings and whoever else
For loops are really really really useful, but it can take a little work to start thinking about them in the “right” way. So I figure we can spend some time learning programmatic thinking while working with them - the process of converting a human idea into something a computer can understand. Just because you know what you want the computer to do, it doesn’t always mean you know how to tell the computer to do it!
For example: let’s say I have a bunch of ducks. How many ducks? I don’t know, we should count them! When I ask you how to do that, it seems like a pretty dumb question. You just, I don’t know, count them, right? But how do you get a computer to do it?
Honestly we would just use
len(ducks), but let’s pretend that
len doesn’t exist.
If we combine loops with the whole incrementing variables thing we learned last chapter, we can try to see how many times we’ve gone through the loop.
# We have a handful of numbers numbers = [10, 20, 30, 40, 50, 60] # Start from zero count = 0 print("Count starts off at", count) for number in numbers: print("----") count = count + 1 # Display the final count print("Count finishes at", count)
We see that it seems to work, but it might not be clear how it works.
Inside the loop Python does two things: she prints out
---- and adds
count. Each time she goes through the loop, she prints out
count gets one bigger.
Six numbers means six times through the loop. This means six times incrementing
count ends up as
But to help us see it happen, add two new lines inside the loop - first, one to print out the current
number you’re looking at, and then another to print the current value of
countout after you add one to it
print(number)to print out the current number
print(count)to print out the current value of
count = count + 1
countafter you add one to it, otherwise you won’t get confetti!
Indenting is very very important when dealing with loops. Why? Well, run the code below and see that it counts up to 6 correctly.
numbers = [10, 20, 30, 40, 50, 60] count = 0 for number in numbers: print("Looking at", number) count = count + 1 print("Count finishes at", count)
Now, remove the indent before
count = count + 1. How does the output change?
When you remove the indenting, you’re no longer incrementing every time you’re inside of the loop. Instead, it doesn’t increment until the loop is totally done. Yes, even though it’s right below the loop, only the indented parts are run as part of the loop. When we remove the indenting, we take
count = count + 1 out of the loop, so it only gets run once (after the loop is done). That’s why
count is only
1 when you run the edited code!
Python loves punctuation, and Python loves indenting. Using the wrong punctuation usually makes the program yell at you and not run, but using the wrong indenting usually just makes you get the wrong result. It’s tricky!
But back to counting in loops: if we want to talk about what we’re doing as programmatic thinking, we can break it down into a few steps.
count = 0.
count = count + 1
count! We print it out and we’re done.
Okay, let’s get back to those ducks. Remember how we wanted to count them without using
len? Give it a try. You’re only responsible for stage two here.
ducks = ["Leonard", "Starburst", "Leopold", "Lackluster"] # Initial state: ZERO ducks count = 0 for duck in ducks: print("Looking at", duck) # Answer: How many ducks? print("We found", count, "ducks")
count = count + 1to make
countone bigger each time you look at a duck
count = count + 1right after you print the duck’s name.
We started to learn about programming thinking, where we break down ideas into steps we can tell Python about. We also learned the word increment, which means to make someting one bigger.