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.2 For Loops
For Loops
Print Ten Hellos For Loops
The Range Function
In this lesson, you will learn about another programming
Counting in Range
construct that will allow you to repeat a task a specific number of
One More Range Parameter times. Unlike the while loop, the for loop is one that requires
Running Total more restrictive parameters. Instead of telling the program to
Running Total, Part 2 generally run until a certain condition is met, the for loop allows
Check Your Understanding you to execute code for a pre-determined number of times.
Exercise: Average Receipt The syntax of a for loop begins with a line that resembles
Value
something similar to for i in range (4): . This reads: repeat
3.3 Break and Continue everything inside of this loop 4 times. To tell the for loop how
(/textbook/intropython_textbook/3.3)
many times to repeat, all you have to do is put an integer (or a
3.4 Nested Control Structures
variable that holds an integer value) inside the range() function
(more details below). After this line, every line of code that you
want repeated within the body needs to be is indented.
Print Ten Hellos
This program prints ten hellos. Change the integer value inside
the range function and see what happens!
Print Ten Hellos
1 for i in range(10):
2 print("Hello") Output
Run Stop
The Range Function
The range() function determines the value that i takes each
repetition of the loop, thereby determining the number of times
the loop will repeat itself. Take a look at the code below and try
to anticipate what will print onto the screen:
1 for i in range(5):
2 print (i)
Since i is a variable and not a string, the letter “i” will not be
printed. Instead, the value of i , as set by the range function,
prints to the screen. What you might not have suspected, by
default, i starts at 0 and increases by 1 up to, but not including
the integer in range() , which in this is case 5:
0
1
2
3
4
Notice that while i never equals 5, the loop still repeats 5 times.
While starting at 0 is the default behavior, you can customize the
range of values that i will take on by putting two arguments into
the range function like: for i in range (1, 5): . In this case,
the program will start at 1 and increment by 1 up to, but not
including 5.
Counting in Range
Before you run the example below, try to guess the outputs of
each loop! After running it, change the values in the range
functions to see what happens.
One More Range Parameter
Taking a step further, you can also tell the program to change i
by a different step size other than 1. In the example below, i will
begin at 1 and increase by two steps up to, but not including 7.
Running Total
While the for loop is intended for a set number of iterations, you
can create a program to have the user dictate how many times it
needs to loop. One way to do this is by using input() , as shown
in the following example:
Running Total, Part 2
In the example below, the program asks the user the total
number of integers they would like to add together. It then stores
that number inside a variable called my_number and runs for the
number of times, equal to the value of my_number .
Check Your Understanding
Exercise: Average Receipt Value
Write a program that asks the user for four receipt amounts. The
program should then report the average of these receipts. You
will need:
A call to input . You should only have one line that has this!
A for loop.
Arithmetic operators.
Vocabulary
Term Definition
Loop A loop is a way to repeat code in your program.
Indentation is the visual structure of how your code is laid
Indentation out. It uses tabs to organize code into a hierarchy.
Indentation out. It uses tabs to organize code into a hierarchy.
Previous Section Next Section
3.1 While Loops
(/textbook/intropython_textbook/3.1)
3.3 Break and Co