✍️ For Loops
Level 1
Ranges
Print “hello” 5 times using a for-loop
how do you create ranges?
Print the values from 1 to 10.
How can you define the start and end of a range?
Print every 3rd number from 1 to 10.
How can you define the start and end of a range?
Print every number from 10 down to 1 (inclusive).
Did anyone say that you can’t use a negative number for the step/increment?
Tracing Loops
What will be the output of this program?
grade = 78
for _ in range(10):
grade += 1
print(grade)Note: it’s actually legal to name a variable as just an underscore _ like this and the convention is that we do this when we have to create a variable but we have no intention of actually using the variable.
What happens when I add 1 to a grade 10 times?
88
Tracing MORE Loops
What will be the output of this program:
number = 3
value = 0
for i in range(4):
value += number
print(value)What happens when I add 3 to a value 4 times?
12
Countdown (aborted)
Write a countdown timer using range(10, 0, -1) that counts down from 10 to 1. However, when the count hits 3, print “Emergency stop triggered!” and exit the loop immediately using break.
Level 2
You got the Power
Write a program that prints the powers of 2 from 2⁰ to 2¹⁵. Each line should display the exponent and the corresponding power of 2 in the format:
2 to the power of 0 is 1
2 to the power of 1 is 2
...
2 to the power of 15 is 32768
Think about starting with the smallest power, 2⁰, and repeatedly multiplying by 2. The base case is the first power, 2⁰ = 1.
That’s odd
Print only odd numbers up to 100
A number is even if is divisible by 2 and to check divisibility, you can use an operator that gives the remainder of a division. Which operator in Python tells you the remainder when dividing by 2?
Don’t Panic
Print only even numbers up to 100. If the number is divisible by 42, print “DON’T PANIC” instead of the number.
How do you check if a number is even? Apply the same idea to test divisibility by 42 👀
Break-out!
Print numbers between 7 and 100 but stop as soon as you find a number that is divisble by 6.
Take a break with your rarely used operator modulus (%)
Factorial
Calculate the factorial of a number num:
The factorial of 3 is: 1 x 2 x 3
JAC
Write a program that prints each character (i.e. each letter) of the string “John Abbott College” on a new line. However, if the character is a vowel (a, e, i, o, u), skip it using continue.
Remember that a for loop can go through the individual characters of a string (refer back to the for-loop page if needed.)
We can make the code much shorter by using in to test whether the letter we are considering is present anywhere in a given string:
sdrawkcaB
Ask the user to enter some text, then print it backwards (using a loop). In Thonny, start with:
something: str = input("Type something here: ")Note: the
inputfunction won’t work in this webpage, so either use Thonny for this, or instead of usinginputyou can just assign some string value.
Adding isn’t just for numbers!
46?
Ask yourself what the 2 outputs of this program should be and then try it out and make sure you understand what the code is doing!
What value(s) will the loop variable i be assigned as the loops repeat?
4
3
Each of these loops is iterating across the sequence of characters (e.g. letters) in a string, so a string of length 4 (meaning 4 characters long like "abcd" or "desk" or "four"...) will loop 4 times. This code is essentially counting how many characters are in a string and then printing the result.
Clock
Display every minute of every hour in a day like:
0:00
0:01
0:02
…
23:58
23:59
Save yourself a lot of typing by using 2 loops!
Continue breaking
Write a program using range(1, 51) that prints numbers from 1 to 50, but with two rules: 1. Use continue to skip any number that is a multiple of 3.
2. Use break to stop the loop completely as soon as the sum of the non multiple of three numbers seen reaches or exceeds 40.
As part of this, you need to accumulate a total based on the numbers that are not multiples of 3.