Loops
Video
Loops
- Essentially,
loops
are a way to do something over and over again. - Begin by typing
code cat.py
in the terminal window. -
In the text editor, begin with the following code:
Running this code by typingpython cat.py
, you’ll notice that the program meows three times. -
In developing as a programmer, you want to consider how one could improve areas of one’s code where one types the same thing over and over again. Imagine where one might want to “meow” 500 times. Would it be logical to type that same expression of
print("meow")
over and over again? Loops enable you to create a block of code that executes over and over again.
While Loops
- The while loop is nearly universal throughout all coding languages.
- Such a loop will repeat a block of code over and over again.
-
In the text editor window, edit your code as follows:
Notice how even though this code will executeprint("meow")
multiple times, it will never stop! It will loop forever.while
loops work by repeatedly asking if the condition of the loop has been fulfilled. In this case, the compiler is asking “does i not equal zero?” When you get stuck in a loop that executes forever, you can press Ctrl+C on your keyboard to break out of the loop. -
To fix this loop that lasts forever, we can edit our code as follows
Notice that now our code executes properly, reducingi
by1
for each “iteration” through the loop. This term iteration has special significance within coding. By iteration, we mean one cycle through the loop. The first iteration is the “0th” iteration through the loop. The second is the “1st” iteration. In programming we count starting with 0, then 1, then 2. -
We can further improve our code as follows:
Notice that when we codei = i + 1
we assign the value ofi
from the right to the left. Above, we are startingi
at one, like most humans count (1, 2, 3). If you execute the code above, you’ll see it meows three times. It’s best practice in programming to begin counting with zero. -
We can improve our code, to start counting with zero:
Notice how changing the operator toi < 3
allows our code to function as intended. We begin by counting with 0 and it iterates through our loop three times, producing three meows. Also, notice howi += 1
is the same as sayingi = i + 1
.
There is one last improvement we can make to the code. That is to use descriptive variable names. In this case, we could use the name number_of_meows
instead of i
. This makes it clear what the variable is used for. This example is trivial, but as programs get more complex, descriptive variable names become increasingly important.
Our code at this point is illustrated as follows:
flowchart TD
id1([Start])
id2([Stop])
id1 --> id3[i = 0]
id3 --> id4{i < 3}
id4 -- True --> id5["meow"]
id5 --> id6[i += 1]
id6 --> id4
id4 -- False --> id2
Notice how our loop counts i
up to, but not through 3
.
For Loops
- A
for
loop is a different type of loop. - To best understand a
for
loop, it’s best to begin by talking about a new variable type called alist
in Python. As in other areas of our lives, we can have a grocery list, a to-do list, etc. -
A
Notice how clean this code is compared to your previousfor
loop iterates through alist
of items. For example, in the text editor window, modify yourcat.py
code as follows:while
loop code. In this code,i
begins with0
, meows,i
is assigned1
, meows, and, finally,i
is assigned2
, meows, and then ends. -
While this code accomplishes what we want, there are some possibilities for improving our code for extreme cases. At first glance, our code looks great. However, what if you wanted to iterate up to a million? It’s best to create code that can work with such extreme cases. Accordingly, we can improve our code as follows:
Notice howrange(3)
provides back three values (0
,1
, and2
) automatically. This code will execute and produce the intended effect, meowing three times. -
Our code can be further improved. Notice how we never use
Notice how changing thei
explicitly in our code. That is, while Python needs thei
as a place to store the number of the iteration of the loop, we never use it for any other purpose. In Python, if such a variable does not have any other significance in our code, we can simply represent this variable as a single underscore_
. Therefore, you can modify your code as follows:i
to_
has zero impact on the functioning of our program. -
Our code can be further improved. To stretch your mind to the possibilities within Python, consider the following code:
Notice how it will meow three times, but the program will producemeowmeowmeow
as the result. Consider: How could you create a line break at the end of each meow? -
Indeed, you can edit your code as follows:
Notice how this code produces three meows, each on a separate line. By addingend=""
and the\n
we tell the compiler to add a line break at the end of each meow.
Improving with User Input
- Perhaps we want to get input from our user. We can use loops as a way of validating the input of the user.
- A common paradigm within Python is to use a
while
loop to validate the input of the user. - For example, let’s try prompting the user for a number greater than or equal 0:
Notice that we’ve introduced two new keywords in Python,
continue
andbreak
.continue
explicitly tells Python to go to the next iteration of a loop.break
, on the other hand, tells Python to “break out” of a loop early, before it has finished all of its iterations. In this case, we’llcontinue
to the next iteration of the loop when n is less than 0—ultimately reprompting the user with “What’s n?”. If though,n
is greater than or equal to 0, we’llbreak
out of the loop and allow the rest of our program to run. -
It turns out that the
Notice how this while loop will always run (forever) untilcontinue
keyword is redundant in this case. We can improve our code as follows:n
is greater than0
. Whenn
is greater than0
, the loop breaks. -
Bringing in our prior learning, we can use functions to further to improve our code:
Notice how not only did we change your code to operate in multiple functions, but we also used adef main(): number = get_number() meow(number) def get_number(): while True: n = int(input("What's n? ")) if n > 0: break return n def meow(n): for _ in range(n): print("meow")
return
statement toreturn
the value ofn
back to themain
function.
More About Lists
- Consider the world of Hogwarts from the famed Harry Potter universe.
- In the terminal, type
code hogwarts.py
. -
In the text editor, code as follows:
Notice how we have alist
of students with their names as above. We then print the student who is at the 0th location, “Hermoine”. Each of the other students are printed as well. -
Just as we illustrated previously, we can use a loop to iterate over the list. You can improve your code as follows:
Notice that for eachstudent
in thestudents
list, it will print the student as intended. You might wonder why we did not use the_
designation as discussed prior. We choose not to do this becausestudent
is explicitly used in our code. -
You can learn more in Python’s documentation of lists.
Length
- We can utilise len as a way of checking the length of the list called students.
-
Imagine that you don’t simply want to print the name of the student, but also their position in the list. To accomplish this, you can edit your code as follows:
Notice how executing this code results in not only getting the position of each student plus one using i + 1, but also prints the name of each student. len allow you to dynamically see how long the list of the students is regardless how much it grows. -
You can learn more in Python’s documentation of len.
Dictionaries
- dicts or dictionaries is a data structure that allows you to associate keys with values.
- Where a list is a list of multiple values, a dict associates a key with a value.
- Considering the houses of Hogwarts, we might assign specific students to specific houses.
Hermoine | Harry | Ron | Draco |
---|---|---|---|
Gryffindor | Gryffindor | Gryffindor | Slytherin |
-
We could use lists alone to accomplish this:
Notice that we could promise that we will always keep these lists in order. The individual at the first position of students is associated with the house at the first position of the houses list, and so on. However, this can become quite cumbersome as our lists grow! -
We can better our code using a dict as follows:
Notice how we usestudents = { "Hermoine": "Gryffindor", "Harry": "Gryffindor", "Ron": "Gryffindor", "Draco": "Slytherin", } print(students["Hermoine"]) print(students["Harry"]) print(students["Ron"]) print(students["Draco"])
{}
curly braces to create a dictionary. Where lists use numbers to iterate through the list, dicts allow us to use words. -
Run your code and make sure your output is as follows:
We can improve our code as follows: Notice how executing this code, the for loop will only iterate through all the keys, resulting in a list of the names of the students. How could we print out both values and keys? -
Modify your code as follows:
Notice how students[student] will go to each student’s key and find the value of the their house. Execute your code and you’ll notice how the output is a bit messy. -
We can clean up the print function by improving our code as follows:
Notice how this creates a clean separation of astudents = { "Hermoine": "Gryffindor", "Harry": "Gryffindor", "Ron": "Gryffindor", "Draco": "Slytherin", } for student in students: print(student, students[student], sep=", ")
,
between each item printed. -
If you execute python hogwarts.py, you should see the following:
- What if we have more information about our students? How could we associate more data with each of the students?
Name | House | Patronus | |
---|---|---|---|
0 | Hermione | Gryffindor | Otter |
1 | Harry | Gryffindor | Stag |
2 | Ron | Gryffindor | Jack Russell terrier |
3 | Draco | Slytherin | None |
-
You can imagine wanting to have lots of data associated multiple things with one key. Enhance your code as follows:
Notice how this code creates a list of dicts. The list called students has four dicts within it: One for each student. Also, notice that Python has a special None designation where there is no value associated with a key. -
Now, you have access to a whole host of interesting data about these students. Now, further modify you code as follows:
Notice how the for loop will iterate through each of the dicts inside the list called students.students = [ {"name": "Hermoine", "house": "Gryffindor", "patronus": "Otter"}, {"name": "Harry", "house": "Gryffindor", "patronus": "Stag"}, {"name": "Ron", "house": "Gryffindor", "patronus": "Jack Russell terrier"}, {"name": "Draco", "house": "Slytherin", "patronus": None}, ] for student in students: print(student["name"], student["house"], student["patronus"], sep=", ")
-
You can learn more in Python’s Documentation of dicts.
Mario
- Remember that the classic game Mario has a hero jumping over bricks. Let’s create a textual representation of this game.
-
Begin coding as follows:
Notice how we are copying and pasting the same code over and over again. -
Consider how we could better the code as follows:
Notice how this accomplishes essentially what we want to create. -
Consider: Could we further abstract for solving more sophisticated problems later with this code? Modify your code as follows:
Notice how our column can grow as much as we want without any hard coding. -
Now, let’s try to print a row horizontally. Modify your code as follows:
Notice how we now have code that can create left to right blocks. -
Examining the slide below, notice how Mario has both rows and columns of blocks.
-
Consider, how could we implement both rows and columns within our code? Modify your code as follows:
Notice that we have an outer loop addresses each row in the square. Then, we have an inner loop that prints a brick in each row. Finally, we have a print statement that prints a blank line. -
We can further abstract away our code:
def main():
print_square(3)
def print_square(size):
for i in range(size):
print_row(size)
def print_row(width):
print("#" * width)
main()
Summing Up
You now have another power in your growing list of your Python abilities. In this lecture we addressed…
- Loops
while
for
len
list
dict