Module 3 - Section 1 - Loops

Site: ΕΛ/ΛΑΚ Moodle
Course: Python Lab
Book: Module 3 - Section 1 - Loops
Printed by: Guest user
Date: Thursday, 21 November 2024, 9:55 PM

Description

After the completion of this module the students will be able to:

  • state the need of using loops
  • use the for loop
  • create code using nested loops
  • calculate a sum of numbers using loops

3.1.1 - Range

What is a loop in programming? In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.

What will we do to print 10 times our best friends name? We could write the following code 10 times:

print("My best friends name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick") print("My best friend's name is Nick")


Instead we can order the computer to repeat the command 10 times.

First of all we have to learn the range command. When we write range(10), python generates a list of numbers, which is generally used to iterate over with for loops.

So range(5) contains 5 numbers [0, 1, 2, 3, 4]. In computer languages counting starts from 0 (zero).

If you want to view what is inside a range you can write the command  in CLI

>>> list(range(5))

Try also the following and try to understand what range does

>>> list(range(3,6))     # The result is [3, 4 , 5]

>>> list(range(9,16))   # The result is [9, 10 , 11, 12, 13, 14, 15]

So a list includes numbers starting from the first one until the last one without including the last one and increasing (stepping) them with 1.

If we omit the first number it is the same as writing 0

>>> list(range(5)) # is the same as 

>>> list(range(0,5))

What else can we do with range? Change the step.

Try the following in CLI

>>> list(range(2,20,3))

The first item is 2, the next is 5 (2+3), the next is 8 (5+3) .... and the last one is 17, 20 is not included in the list)

As an exercise try the following code and before execution try to foresee the result :

>>> list(range(1,9,2))

>>> list(range(7))

>>> list(range(7,17,4))

As an exercise write a range command that has 10 numbers.
Of course the easiest is range(10). List it with the command

>>> list(range(10))

3.1.2 - The For loop

When we use a loop? When we want to repeat a block of code. The for loop is used when we know how many times we want the code to be executed. Copy the following code to Thonny's editor and save it (save as) as Loops.py. To view the execution step by step you can press the Debug button, after that the Step into (F7) and finally Step over(F6). Take a closer look at the variable x in the right pane of Thonny.

for i in range(10): print("I will not chew gum in class.")


Also try the following :

for i in range(5): print("Please,") print("Can I go to the mall?")


And change it to this :

for i in range(5): print("Please,") print("Can I go to the mall?")


Try the following code and try to explain what is its purpose :

for i in range(10): print (i, "* 5 =", i*5)


Do you have an idea of how we can make it better? Try this one now :

for i in range(1,11): print (i, "* 5 =", i*5)


And now this one :

number = int(input("Please write a number for 1 to 10 : ")) for i in range(1,11): print (i, "* ",number," =", i*number)

3.1.3 - Nested for loops

Try to predict what the code below will print. Then enter the code and see if you are correct.

# What does this code prints? Why? for i in range(3): print("a") for j in range(3): print("b")

This next block of code is almost identical to the one above. The second for loop has been indented one tab stop so that it is now nested inside of the first for loop. This changes how the code runs significantly. Try it and see.

# What does this code prints? Why? for i in range(3): print("a") for j in range(3): print("b")


Now try to create a program that will output

Nested Loops Example
1
a
a
a
a
2
a
a
a
a
3
a
a
a

Here you can view a solution to the problem.

3.1.4 - Calculating a sum

A common operation in working with loops is to keep a running total. This “running total” code pattern is used a lot. Keep a running total of a score, total a person's account transactions, use a total to find an average, etc. In the code below, the user enters five numbers and the code totals up their values. Copy the code below and save it as sum.py inside your working folder.


So how can we calculate this sum 1+2+3+4+5+6+7+8+9+ ...... + 1000? What is the range of these numbers? Is it range(1000) or range (1,1001)? If you don't remember try to find listing the range. Of course feel free to think something different 

The commands are

>>> list(range(1000))

and 


>>> list(range(1,1001))

A solution to the problem is :


And what do you think this program does?


Carefully remove the # signs and take care of indentation to see the program running and printing information about what is doing. This a form of debugging.

So this program calculated the sum of even numbers between 0 and 100 (not included). Can you modify it to calculate the odd numbers from 1 to 99? You need to change only one number, but you can find your own solution. The answer is 2500.