0% found this document useful (0 votes)
2 views5 pages

34 Textbook: Intro to Python Textbook | CodeHS

Chapter 3 of the Intro to Python textbook covers looping concepts, including while loops, for loops, and nested control structures. It explains how to implement nested loops and provides examples of their usage, such as printing values and calculating averages. The chapter also includes exercises to reinforce understanding of these concepts.

Uploaded by

xilifo7836
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

34 Textbook: Intro to Python Textbook | CodeHS

Chapter 3 of the Intro to Python textbook covers looping concepts, including while loops, for loops, and nested control structures. It explains how to implement nested loops and provides examples of their usage, such as printing values and calculating averages. The chapter also includes exercises to reinforce understanding of these concepts.

Uploaded by

xilifo7836
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Intro to Python Textbook

(/textbook/intropython_textbook/) CHAPTER 3

1. Python and Console Interaction Looping


(/textbook/intropython_textbook/1)
2. Conditionals
(/textbook/intropython_textbook/2)
3. Looping
(/textbook/intropython_textbook/3)
3.1 While Loops
(/textbook/intropython_textbook/3.1)
3.2 For Loops
(/textbook/intropython_textbook/3.2) 3.4 Nested Control Structures
3.3 Break and Continue
In this lesson, you will learn about nested control structures,
(/textbook/intropython_textbook/3.3)
which will allow you to solve problems that require loops within
3.4 Nested Control Structures
loops.
(/textbook/intropython_textbook/3.4)
Nested Control Structures

Double For Loop  Nested Control Structures


For Loop + While Loop As you recall, the structure of a for loop is as follows:
Average Test Score, Part 2
1 for i in range(5):
Check Your Understanding
2 print("hello")
Exercise: Rolling Dice

4. Functions and Exceptions


Any code that you want executed as the for loop runs must be
(/textbook/intropython_textbook/4)
indented beneath line 1. The same logic applies to nesting a
5. Strings
second for loop inside of the first one: the second for loop must
be indented inside the first one:

1 for i in range(5):
2 for j in range(4):
3 print("hello")

There are a couple of things to notice here:


The iteration variable in the second loop is j and not i . That
is because i is already being used by the first loop, and
therefore is already storing a value. In reality, these iteration
variables can have any names, they just have to be different
between nested loops.
The print function is indented inside the second for loop.
This means that the second for loop will print “hello” 4 times
every time it runs.
Let’s take a closer look to deepen our understanding of how
nested for loops work. Imagine for a moment that only the first
line of our code exists, blocking out the succeeding lines, like the
code below:

1 for i in range(5):
2 # block of code
Think of this first for loop as an umbrella. Here, all of the code in
the block falls underneath the umbrella and is expected to run 5
times. It just so happens that the code in the block is the second
for loop, for j in range(4) , which means that this second for
loop will run 5 times:

1 for i in range(5):
2 for j in range(4):
3 print ("hello")

The same rule applies to the second for loop. Since one run of
the second loop will print “hello” 4 times, and it was just
determined that the second loop will be run 5 times, “hello” will
be printed 5 x 4 = 20 times.

 Double For Loop


Review the code in the example below and try to decipher what
will happen before running it. The spaces in the print on line 9
make it a little easier to visualize what’s going on with each for
loop.
This example exhibits the same flow as the first example above:
1. Start the first for loop, with i = 0 by default.
2. Print the value of i on line 7.

3. Start the second for loop, with j = 0 by default. Note i is


still equal to 0.
4. Print the value of j .
5. Complete all of the next iterations of the second loop, with j
equaling 1, 2, and 3, and printing these values.
6. Return to the first for loop on line 6 and start with the next
iteration, i equaling 1.
7. Repeat steps 2 - 6 until the first for loop finishes with i
equaling 3.

Double For Loop


1 # This program visualizes nested for loops
Output
2 # by printing number 0 through 3 and then
0
3 # through 3 for the nested loop.
Run Stop
4
5 for i in range(4):
6 print("Outer for loop: " + str(i))
7
8 # This for loop will complete all 4
cycles before
9 # the program returns to the top for
loop.
10 for j in range(4):
11 print(" Inner for loop: " + str
(j))
 For Loop + While Loop
Now let’s look at an example of a while loop being nested inside
of a for loop. Based on the previous example, can you follow the
path of the code?
Even though it’s a nested while loop, the same logic applies: for
every one iteration of the top for loop, the value of i will print,
the value of i will be set to x , and the the while loop will operate
until completion. That process will happen 4 times (based on the
integer in the top range function).

 Average Test Score, Part 2


This program asks for the test scores for multiple people, and
reports the average test score for each person.
 Check Your Understanding

 Exercise: Rolling Dice


Write a program that will print out all combinations that can be
made when two special 10-sided dice are rolled.
Your output should look like this:

1,1
1,2
1,3
1,4
1,5
1,6
1,7
1,8
1,9
1,10
2,1
2,2
2,3
2,4

And should continue until all values up to 10,10 are printed.


Since you know exactly how many times each loop should
iterate, you should use nested for loops to complete this
program.
Previous Section Next Section
 3.3 Break and Continue
(/textbook/intropython_textbook/3.3)
4 Functions and E

You might also like