Logic in Python #1 | Using If-else to find the largest number
On this occasion, I will make a program to find out which number is the largest of the 3 numbers. So there are 3 numbers, for example, the numbers are 234, 77855555, 18176 from this case it has been found out which is the largest number, which is 77855555 okay, I’ll check it in python later.
actually, it is very easy to find the largest number of these 3 numbers I can use a list like the below.
list = [234, 77855555, 18176]
print('the largest number is : ', list[-1])
By using sort(), the numbers will be sorted from the lowest value to the largest value. To search for the largest number means that it is in the last index, so I just display what number is in the last index and the largest number will be found.
But that’s not what I want to do. to strengthen programming logic we must learn to solve it with logic.
What’s the logic?
Okey, Suppose I have 3 variables a, b, and c.
When is variable b said to be the largest?
So the answer is that variable b will be said to be the largest if it is greater than the value of a and also greater than the value of c.
Ok, let’s try to start with python, first I define 3 variables with the values I set earlier.
a, b, c = (234, 77855555, 18176 )
print('show value A : ', a)
print('show value B : ', b)
print('show value C : ', c)
if a > b and a > c:
print('the largest A value with the value : ', a)
elif b > a and b > c:
print('the largest B value with the value : ', b)
print('the largest C value with the value ', c)