0% found this document useful (0 votes)
5 views3 pages

Lesson 5 - Iteration, WHILE, For

Uploaded by

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

Lesson 5 - Iteration, WHILE, For

Uploaded by

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

Iteration

Iteration is repeating a block of code until a condition is met.#


Python uses two types:
For Loops (count-controlled)
While Loops (condition-controlled)#
Think of it like this: driving around in a car park, until you find a space you
repeat this action.

What it Iteration?

It reduces lines and lines of code #


It saves a lot of time as we don’t need to repeat code#
This all make the code more efficient

Why do we need Iteration?

For Loops are set up using the following statement:

for x in range (n):


For Loop
The x is simply a variable. It could have any name.

It is however a special kind of variable known as a ‘stepper’/counter


The n is simply representing a number that we want the loop to repeat itself for.
We must finish the statement with a colon

A For Loop is used when you know exactly how many times you want to repeat a set of
code #
i is a counter, normally we use i however you can use any letter- it is just a
“number holder”#
range(5) repeats the code 5 times#
The output is…
For Loop

You can choose a range to print#


Note instead of i, we used a #
This program will print “This is number: “ with whichever number is currently in
a.#
The output:

For Loop
Why is there no number 10?

For Loop
for x in range (m,n,o):
Steps to move up in
Starting Number
Upper limit number – what the loop will stop at
There are some other tools that we can use to customise our For Loop.
We can change the starting number of our loop.

We can state the steps we want to go up in (it doesn’t have to go up in 1s)

For Loop
Here is a simple example of a FOR loop being used to display the numbers 1 – 10.

Here, the stepper variable is being used to output the number.

Because the stepper starts at 0, 1 has been added on to the variable at the moment
that it is being outputted to the screen.
8

While Loops
9

Think about when you log in…

“While the password you enter, doesn’t equal the password on your account, the
program will repeatedly ask you to enter your password.”

While Loops

A While Loop runs as long as a condition is True#


Set the count to 0 #
While counter is <3 the program will execute anything inside the loop#
count = count + 1 adds 1 each time to the counter variable.#
The program stops when count reaches 3- condition is now False
While Loops

While Loops

While Loops are set up using the following statement:

while x 0:

In the empty box, any of these conditions can be used:


!=
<
>=
<=
>
==

While Loops- Common Mistakes ⚠️


Don’t forget to update the loop variable in a while loop - it will cause an
infinite loop. For example:

Using = instead of ==#


Indentation!
This will print Oops! Infinite times as the loop is set to True
x = 0
while x == 0:
print(“Hello World”)

While Loop
x = 0
while x != 5:
x = int(input(“Please type in a number”))

print(“Loop has ended”)


Please type in a number:
Remember:
If you create a condition where a variable is being checked against an integer, you
must remember to convert the variable’s input into an integer (e.g. int())
Please type in a number:
Please type in a number:
Please type in a number:
1
-5
122
5
Loop has ended
0
1
-5
122
5
Here is an example of a while loop that will stop.

Because the statement in the loop is asking for a new value for x, it means that x
can be changed and so can stop if the correct value is entered.

While Loop Example

Example of a while loop in action:

You might also like