In [ ]:
*** For loops ***
In [ ]:
When we want to repeat things over and over again, we can use for loop. The for loop is one of the several ways to repeat
things in Python. The general format for the for loops is:
for variablename in range(number of times to repeat):
statements to be repeated
In [2]:
for i in range(5): # i is the variablename. Statements will be repeated 5 times. Do not forget the : sign.
print("Hello!") # The statements should be indented. Indentation tells Python what statements will be repeated.
Hello! Hello! Hello! Hello! Hello!
In [3]:
for i in range(3): # Always use a variable name even if you will not use it with the statements.
num = eval(input("Enter a number: "))
print ("The square of your number is", num*num)
print("This line shows that the loop is done and will not be executed inside the loop, because it is not indented.")
The square of your number is 25
The square of your number is 4
The square of your number is 144 This line shows that the loop is done and will not be executed inside the loop, because it is not indented.
The following examples will make clear the importance of understanding the indentation. We will use the "for loop" to do some things again and again using indentation.
In [4]:
print("Hello!")
print("My name is ")
for i in range(3):
print("John")
print("Doe.")
print("It was nice to meet you!")
Hello! My name is John Doe. John Doe. John Doe. It was nice to meet you!
In [5]:
print("Hello!")
print("My name is ")
for i in range(3):
print("John")
for j in range(2):
print("Doe.")
print("It was nice to meet you!")
Hello! My name is John John John Doe. Doe. It was nice to meet you!
In [ ]:
After learning how important the indentation is, now we can focus on the loop variable. When we use the for loop with range()
function, then the loop variable is set to 0 by default, increases by 1 and repeats the statements inside the for loop
the specified number of times.
In [6]:
for i in range(5): # Loop will be executed 5 times. Starting value for i is 0 and ending value i-1.
print(i)
0 1 2 3 4
In [7]:
for k in range(5):
print(k+1, "--- Hello!")
1 --- Hello! 2 --- Hello! 3 --- Hello! 4 --- Hello! 5 --- Hello!
In [ ]:
When we work with loops, it is very important to know what the range() function does. The range() function creates a vector
of numbers changing from the beginning value up to but not including the ending value. Its general form is:
range(start, stop, step)
If it has only one argument, range(stop), beginning value will be 0.
In [5]:
list(range(10))
Out[5]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [6]:
list(range(1, 10))
Out[6]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [7]:
list(range(3, 11))
Out[7]:
[3, 4, 5, 6, 7, 8, 9, 10]
In [8]:
list(range(3, 15, 2))
Out[8]:
[3, 5, 7, 9, 11, 13]
In [9]:
list(range(11, 3, -1))
Out[9]:
[11, 10, 9, 8, 7, 6, 5, 4]
In [ ]:
Now we know what the range() function produces. We can use this information with extra examples.
In [ ]:
Write a program that prints your name 5 times.
In [10]:
for i in range(5):
print("Jane Doe")
Jane Doe Jane Doe Jane Doe Jane Doe Jane Doe
In [ ]:
Write a program that outputs 5 lines, numbered 1 to 5, each with your name on it.
In [12]:
for j in range(5):
print(j+1, "Jane Doe")
1 Jane Doe 2 Jane Doe 3 Jane Doe 4 Jane Doe 5 Jane Doe
In [ ]:
Write a program that prints out a list of the integers from 1 to 5 and their squares.
In [13]:
for k in range(5):
print(k, "---", k*k)
0 --- 0 1 --- 1 2 --- 4 3 --- 9 4 --- 16
In [ ]:
Write a program that uses a for loop to print the numbers 3, 6, 9, 12, 15, . . . , 51, 54, 57.
In [25]:
for i in range(3,60,3):
print(i, end=" ")
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57
In [ ]:
Use a for loop to print an upside down triangle like the one below. Allow the user to specify how high the triangle
should be.
In [1]:
h = eval(input("How high do you want the triangle to be? "))
for i in range(h, 0, -1): # range(4, 0, -1) will produce [4,3,2,1] if h is 4
print("*" * i) # just repeats * character i times
**** *** ** *