For Loop Examples
Print the numbers 1 to 9
for k in range(1,10):
print(k)
Countdown from 10 to 1
for k in range(10,0,-1):
print(k)
Print the days of the week
days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
for day in days:
print(day)
Print the five times table
for k in range(1,11):
print("5 x {} = {}".format(k, 5*k))
For Loop Exercises
Write for loops to output the following sequences of numbers
- 0,1,2,3,4,5,6,7,8,9,10
- 0,2,4,6,8,10,12,14,16
- 1,2,3,4,5, … 97,98,99,100
- 7,14,21, … 63,70,77
- 20,18,16, … 4,2,0,-2
- 2,5,8,11,14,17,20,23,26,29
- 99,88,77,66,55,44,33,22,11,0
- Numbers 1 to 1000.
- Even numbers from 0 to 100.
- Odd numbers from -50 to 50
- All multiples of 3 up to 500.
Look at the days of the week example above. Use lists and for loops to do the following
- Print out the seasons of the year (Spring, Summer, Autumn, Winter)
- Create a list of the names of the four people closest to you. Use a for loop to print that list.
Extension
Look at the examples above.
- Use a for loop to print the 5 times table up to 12 x 5
- Use a for loop to print the 7 times table up to 12 x 7 in the form “3 x 7 = 21”
- Use a for loop to print the following sequence: 0.5, 0.4, 0.3, 0.2, 0.1, 0
- Use a for loop to print the following sequence: 0.03, 0.02, 0.01, 0, -0.01, -0.02, -0,03
- Use a for loop to print five random numbers between 1 and 10
- Use a for loop to print the first ten square numbers: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
- Use a for loop to print the first ten triangle numbers: 1, 3, 6, 10, 15, 21, 28, 36,45, 55