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)