We will perform other for loop examples in Python in order to consolidate what we have studied so far.
For loop examples Python – first example
Design an algorithm that writes all pairs of natural numbers whose sum is 20.
Therefore the algorithm will have to print the pairs (20.0), (19.1), (18.2), (17.3), (16.4) etc …
To design this algorithm, therefore, it is sufficient to decrease the number of the first pair by one and increase the number of the second pair by 1.
By setting n equal to 20, I then use a for loop with the index i starting from 0 up to n + 1 and therefore increasing by 1. After we decrease n by 1.
Here is a possible solution:
n = 20
for i in range(n+1):
print(n,i)
n -= 1
If we don’t want repetitions just put:
n = 20
for i in range(n // 2 + 1):
print(n,i)
n -= 1
We can also generalize the algorithm by asking the user to enter the value of n.
n = int(input('Insert n: '))
for i in range(n // 2 + 1):
print(n,i)
n -= 1
For loop examples Python – second example
Given N pairs of real numbers, count those that generate a negative, positive or zero product without performing the operation.
The product of two numbers is positive when the numbers are both positive or both negative. The product of two numbers is zero when only one of the two numbers equals zero.
In other cases it is negative.
Here is the complete algorithm:
N = int(input('How many pairs?'))
p, n, z = 0, 0, 0
for i in range (N):
a = int(input('Insert the first number:'))
b = int(input('Insert the second number:'))
if a> 0 and b> 0 or a <0 and b <0:
p += 1
elif a == 0 or b == 0:
z += 1
else:
n += 1
print('Positive product:', p, '. Negative:', n, '. Equal to zero:', z)
For loop examples Python - third example
Given n pairs of real numbers, count those that generate a positive, negative, or null sum without performing the operation.
Let's create a first solution by making some considerations.
So for example let's look at these operations:
7 + 3, 3 + 7, -5 + 6, 5-4 positive sum
-7-3, -3-7, -5 + 4, 5-6 negative sum
6-6 adds up to zero
From these examples we deduce that the sum is positive in these cases:
- the terms are both positive or
- only a is greater than zero and simultaneously is greater than the absolute value of b or
- only b is greater than zero and simultaneously is greater than the absolute value of a.
The sum, on the other hand, is zero when a is the opposite of b and this also includes zero.
In all other cases the sum is negative.
Here is the complete algorithm:
N = int(input('How many pairs?'))
p, n, z = 0, 0, 0
for i in range (N):
a = int(input('Insert the first number:'))
b = int(input('Insert the second number:'))
if a> abs (b) or b> abs (a):
p += 1
elif a == -b:
z += 1
else:
n += 1
print('Positive sum:', p, '. Negative:', n, '. Equal to zero:', z)
To simplify the solution of the algorithm we can exchange a and b if the value of the variable a is less than b.
Then we check if a is greater than the absolute value of b and if it is true we increment the variable p. Otherwise we check if a is the opposite of b and increase the value of z. If none of these conditions are true then we increment the variable n.
Here is a possible solution to the proposed algorithm:
N = int(input('How many pairs?'))
p, n, z = 0, 0, 0
for i in range (N):
a = int(input('Insert the first number:'))
b = int(input('Insert the second number:'))
if a abs (b):
p += 1
elif a == -b:
z += 1
else:
n += 1
print('Positive sum:', p, '. Negative:', n, '. Equal to zero:', z)
Try the examples in the compiler below:
Conclusion
We have solved some for loop examples in Python, in the next lesson we will go deeper into the subject.
Some useful links
How to find the maximum of N numbers