FOR Loops (Ranged)

An introduction to the use of Iteration in Python

FOR Loops (Ranged)

Using loops in computer programming allows us to automate and repeat similar tasks multiple times. In this tutorial, we’ll be covering Python’s for loop.

A for loop implements the repeated execution of code based on a loop counter or loop variable. This means that for loops are used most often when the number of iterations is known before entering the loop, unlike while loops which are conditionally based.

In Python, for loops are constructed like so:

for [iterating variable] in [sequence]:
    [do something]

The something that is being done will be executed until the sequence is over.

Let’s look at a for loop that iterates through a range of values:

for i in range(0,5):
   print(i)
Show Output

When we run this program, the output looks like this:

0
1
2
3
4

This for loop sets up i as its iterating variable, and the sequence exists in the range of 0 to 5.

Then within the loop we print out one integer per loop iteration. Keep in mind that in programming we tend to begin at index 0, so that is why although 5 numbers are printed out, they range from 0-4.

You’ll commonly see and use for loops when a program needs to repeat a block of code a number of times.

Introduction

One of Python’s built-in immutable sequence types is range(). In loops, range() is used to control how many times the loop will be repeated.

When working with range(), you can pass between 1 and 3 integer arguments to it:

  • start states the integer value at which the sequence begins, if this is not included then start begins at 0
  • stop is always required and is the integer that is counted up to but not included
  • step sets how much to increase (or decrease in the case of negative numbers) the next iteration, if this is omitted then step defaults to 1
We’ll look at some examples of passing different arguments to range().

First, let’s only pass the stop argument, so that our sequence set up is range(stop):

for i in range(6):
   print(i)
Show Output

In the program above, the stop argument is 6, so the code will iterate from 0-6 (exclusive of 6):

0
1
2
3
4
5

Next, we’ll look at range(start, stop), with values passed for when the iteration should start and for when it should stop:

for i in range(20,25):
   print(i)
Show Output

Here, the range goes from 20 (inclusive) to 25 (exclusive), so the output looks like this:

20
21
22
23
24

The step argument of range() is similar to specifying stride while slicing strings in that it can be used to skip values within the sequence.

With all three arguments, step comes in the final position: range(start, stop, step). First, let’s use a step with a positive value:

for i in range(0,15,3):
   print(i)

In this case, the for loop is set up so that the numbers from 0 to 15 print out, but at a step of 3, so that only every third number is printed, like so:

0
3
6
9
12

We can also use a negative value for our step argument to iterate backwards, but we’ll have to adjust our start and stop arguments accordingly:

for i in range(100,0,-10):
   print(i)

Here, 100 is the start value, 0 is the stop value, and -10 is the range, so the loop begins at 100 and ends at 0, decreasing by 10 with each iteration. We can see this occur in the output:

100
90
80
70
60
50
40
30
20
10

When programming in Python, for loops often make use of the range() sequence type as its parameters for iteration.

Using Range

Write a program that prints your name 5 times using a FOR loop.

Write a program that asks the user for a number and prints what they type that number of times in a FOR loop.

Create a program that loops from 100 to 200 and prints the loop number each time.

10.1 FOR Loops (Range) Task
Submit your completed task...
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.