In this Python lesson we will give some for loop examples, in order to better understand how it works and develop logical thinking.
Python for loop examples – first example
Design an algorithm that writes all pairs of numbers that give 60 as a product.
The solution is to use a for loop to find the divisors of 60, so for each divisor we also find the quotient.
In fact, let’s recall how the terms of the division are called by giving an example:
60:3 = 20
So 60 is called a dividend, 3 is called a divisor, and 20 is called a quotient. This means that if I multiply the quotient obtained by the divisor I get the dividend. In fact in this specific case we have: 20 * 3 = 60.
Then we initialize n to 60.
After we set the range with the index i must start from 1 as otherwise there would be a division by zero and this is not possible and it stops at n + 1 because we have to divide by 60 if we want to find all the pairs.
Then, doing this operation n% i == 0 we check if the remainder of the division is zero. If it is true it means that i is the divisor, so we find the quotient.
Finally we print the pairs.
Here, then, is the algorithm that uses the for loop in python to find the pairs that give 60 as a product:
n = 60 for i in range (1, n + 1): if n% i == 0: q = n // i print ('Pair', i, 'e', q)
You can try the code in Python compiler online in that link: Python compiler.
Let’s carry out some algorithm steps to explain it better.
n = 60
Python for loop examples – Step by step explanation of the algorithm
for i in range (1, n + 1): # i = 1
if n% i == 0: # 60% 1 == 0 is true
q = n//i #troviamo il quoziente cioè q=60//1=60
print(‘Pair ‘,i,’ and ‘,q) #Print the first pair, that is: i=1 and q=60
for i in range (1, n + 1): # i = 2
if n% i == 0: # 60% 2 == 0 is true
q = n // i # we find the quotient that is q = 60 // 2 = 30
print (‘Pair’, i, ‘and’, q) # We print the second pair: i = 2 and q = 30
for i in range (1, n + 1): # i = 3
if n% i == 0: # 60% 3 == 0 is true
q = n // i # we find the quotient that is q = 60 // 3 = 20
print (‘Pair ‘, i, ‘ and’, q) # We print the third pair: i = 3 and q = 20
And so on until the last step where we will divide the number by itself.
Python for loop examples – second example
Prints the first N odd numbers following the number A.
We take as input the number to start from, that is A and the quantity of odd numbers to be displayed following A.
ES: We insert N = 5 and A = 6.
So the next 5 odd numbers of A that we need to visualize are: 7, 9, 11, 13, 15.
Then the index i of the for must start from 7 and end at 15. From 7 to 15 there are 2 * N -1 (9) numbers of which 5 are odd and 4 even.
Another example: We insert N = 3 and A = 7.
So the next 3 odd numbers of A that we need to visualize are: 9, 11, 13.
Then the index i of the for must start from 9 and end at 13. From 9 to 13 there are 2 * N -1 (5) numbers of which 3 are odd and 2 even.
Looking at the examples above, to avoid creating two for loops, we execute an if and if A is even we make it odd, if A is odd we increment it by 2.
Then we create a for loop that initializes the index i to A, as it represents the starting number and increases the value of A by 2 until reaching the sum of A plus double the required numbers.
So just simply print the index to get what the algorithm requires.
Here is the complete code using the for loop in Python:
N = int(input('How many numbers to display?: ')) A = int(input('Enter the value to start from: ')) if A % 2 == 0: A += 1 else: A += 2 for i in range(A,A+N*2,2): print (i)
These are just a few examples of the for loop in Python, we will do some more in the next lessons.