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

Dhakshina Vijayaprakash - 04 Iteration (Count Controlled) - Procedural Python

The document provides an introduction to FOR loops in Python, explaining their structure and functionality through examples. It describes how to set up basic and advanced FOR loops, including counting and modifying iterations. Additionally, it includes practical programming tasks to reinforce understanding of FOR loops.

Uploaded by

d
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 views8 pages

Dhakshina Vijayaprakash - 04 Iteration (Count Controlled) - Procedural Python

The document provides an introduction to FOR loops in Python, explaining their structure and functionality through examples. It describes how to set up basic and advanced FOR loops, including counting and modifying iterations. Additionally, it includes practical programming tasks to reinforce understanding of FOR loops.

Uploaded by

d
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
You are on page 1/ 8

Introduction

Iterations are a programming construct which enables the repeated execution of lines of code.
The FOR loop is a type of iteration which is count controlled. This means that it loops for a set
number of iterations.

The Basic FOR Loop

In python, a basic FOR loop is set up like this:

Basic FOR Loop Example

Study the code below

The loop has been set up to iterate (loop) 5 times.

The variable x, counts from zero to the number in the brackets, so to keep count of the number of
iterations the loop has to perform. Therefore, on the first iteration, x is zero, as shown by the first
print statement.

The variable x then counts on to 1, and this is shown in the second print statement.

The variable x continues to count on, until it reaches the number in the brackets, at which point
the loop ends.

Notice how 5 is not outputted. The moment x becomes 5, is the moment the loop ends, so there is
no print statement execute to display the number 5.
A More Advanced FOR Loop

In python, there are actually some other values that we can use to have more control over our
FOR loop as shown below:

Another FOR Loop Example

Study the code below

In this example, you can see that the bracket contains (0,10, 2).

This means that the loop has been set up to count from 0 to 10, but counting up in 2s.

Therefore, the variable x counts 0, 2, 4, 6, 8 (stopping the loop at 10) therefore executing 5 times,
printing out the 5 print statements that you can see above.

Hopefully the above has given you an understanding of how FOR loops can be set up and coded
in Python. However, the most effective way of truly understand how to program FOR loops, is to
get stuck into some practical programming tasks.

Below are a series of tasks designed to guide you through programming FOR loops in the python
programming language.
PRIMM TASKS
1: Predict

Code

What do you think the code will do?

I think that the code will subtract from 10. It will then print the numbers subtracted by
one.

Write the program’s output exactly as you think it will appear.

>>> 10
>>> 9
Predictions
>>> 8
>>> 7
>>> 6
>>> 5
>>> 4
>>> 3
>>> 2
>>> 1
>>> 0
>>> Blast Off!
2: Run

Now type in the code and run the program.


Show a screenshot of the actual output below.

Was it the same as your prediction? Yes No


Were there differences? If so, show or describe these below.

Yes, there were a few differences- it didn’t create a new prompt for each number.

3: Investigate

What happens if you alter It does the same thing, it just counts backwards from 20 instead of 10.
the value in the range()
brackets to 20? Explain
the result.
What happens if you put I changed the range() brackets to (5, 20). It then counted down from
another number BEFORE 15 to 1.
the current number in the
range() brackets (e.g.
range(5, 10)? Explain the
result.

4: Modify

The ‘x’ variable is a Screenshot /Explanation

counter variable which


starts at zero and goes up 0
by one on each loop. 5
Modify the code so that 10
the print statement outputs 15
the 5 times table (instead 20
of a countdown). BLAST OFF!
>>>

Explain what your code is


doing. The code counts up in fives from 0 to 20. Once it counts up to 20, it
prints “BLAST OFF!”

5: Make

Create a program in python for each point below and add a screenshot of your code in the boxes
provided:

1) Write a program that displays the word ‘Hello’ on the screen 4 times using the ‘for loop’.

for _ in range(4):
print(“Hello”)
2) Write a program which prompts the user to enter a short message and the number of times it is
to be displayed and then displays the message the required number of times.

sentence = input("Enter a message: ")


number = int(input("Enter the number of times you want your message to be displayed: "))
for _ in range(number):
print(sentence)

3) Write a program to display the squares of all the integers from 1 to 12 in two columns headed
‘Number’ and ‘Square of Number’.

4) Write a program that asks the user to enter the number of stars per row and the number of
rows to be displayed. For example, entering 5 and 3 should display:

*****

*****

*****
5) Write a program that asks the user to enter how many numbers are to be averaged, then asks
the user to enter these numbers in turn, then calculates the average. The program should
display the average on the screen.

6) Write a program to display an ‘n times table’ for a given integer ‘n’. For n = 4, the output
should be:

1*4=4

2*4=8

3 * 4 = 12

12 * 4 = 48

You might also like