0% found this document useful (0 votes)
20 views10 pages

For Loops Lesson

The document discusses for and while loops in Python. It explains that a for loop is used to iterate over a sequence, using range to generate number sequences from 0 to the given end point. The activities demonstrate how for loops can be used to print numbers in a range or generate times tables, and contrast for loops with while loops which repeat an action as long as a condition is true.

Uploaded by

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

For Loops Lesson

The document discusses for and while loops in Python. It explains that a for loop is used to iterate over a sequence, using range to generate number sequences from 0 to the given end point. The activities demonstrate how for loops can be used to print numbers in a range or generate times tables, and contrast for loops with while loops which repeat an action as long as a condition is true.

Uploaded by

trinagle.org
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Starter activity

Make a prediction (think, write, pair, share)

1 for x in range(5):
2 print(x) Question .

What will the output be when this code is


executed?
Starter activity

Make a prediction (think, write, pair, share)

1 for x in range(5):
2 print(x) The values 0 to 4 will be output on the
screen.

0
1
2
3
4
>>>
Objectives

Objectives L9-12
In this lesson, you will:
● Understand the difference between a for and a while loop.
● Be able to use counter controlled (for) loops.
● Be able to use condition controlled (while) loops.

3
Activity 1

What is a for loop?

1 for x in range(5):
2 print(x) This is an example of a for loop.

A for loop is another tool to control the


flow of execution in your programs.
Activity 1

What is a for loop?

for this sequence:


do this When you use a for loop you are saying:

“For every element in this sequence, do


this.”

For loops can be used for many types of


sequence, for example:

● Letters in a word
● Items in a list
● Numbers in a range
Activity 1

What is a for loop?

1 for x in range(6):
2 print(x) range() is the sequence that you are going
to iterate through.

We will use this function throughout the


lesson.

range() is a built-in function just like


input().

It generates a sequence of numbers.


Activity 1

What is a for loop?

1 for x in range(6):
2 print(x) When you call the range function, you can
pass it up to three values:

1. The start number


2. The end number
3. The step

If you only pass one value, then the


function will use it as the last number and
the sequence will start at 0.

0,1,2,3,4,5
Activity 1

Examples of number sequences

range(5) Pass one value and it will be used as the end point.
0, 1, 2, 3, 4

range(3,10)
Pass two values and they will be used as the start and
3, 4, 5, 6, 7, 8, 9
end point.

range(1, 11, 2) Pass three values and they will be used as the start,
1, 3, 5, 7, 9 end, and the increment (step).
Activity 2

Times table generator

Use the worksheet to explore and extend a


times table generator.
Plenary

Questions

1. Describe a for loop.

2. Describe a while loop.

3. Describe how each are different.

You might also like