# Lesson 3: Loop
This lesson learns are based on Python book page 32-35
# Tools
https://trinket.io/ (opens new window)
# Lesson Learns
Computers are great at doing boring tasks without complaining. Programmers aren’t, but they are good at getting computers to do repetitive work for them—by using loops. A loop runs the same block of code over and over again. There are several different types of loop.
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
print("Hey!")
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# For loop
for i in range(10):
print("Hey!")
1
2
2
output
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
for i in range(10):
print(i)
1
2
2
output
0
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
for i in range(10,15):
print(i)
1
2
2
output
10
11
12
13
14
1
2
3
4
5
2
3
4
5
# While loop
- while(boolean) => stops when boolean is False
i=0
while(i<10):
print("Hey!")
i = i + 1
1
2
3
4
2
3
4
output
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
Hey!
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
i=0
while(i<10):
print(i)
i = i + 2
1
2
3
4
2
3
4
output
0
2
4
6
8
1
2
3
4
5
2
3
4
5
# Loop in Loop in Loop and in Loop
for i in range(5):
for j in range(i+1):
print("*")
print("-----")
1
2
3
4
2
3
4
output
*
-----
*
*
-----
*
*
*
-----
*
*
*
*
-----
*
*
*
*
*
-----
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Exercises
PythonExercise.pdf Page 21 (Only No.1 and 2)