In this lesson we will go through some examples of Python if statement, in order to consolidate the arguments studied so far. This Python tutorial is packed with exercises and practical examples to help you learn the theoretical concepts better.
First example on Python if
Determine if an angle is acute, obtuse or right.
The algorithm is solved very easily, in fact for example in the first condition we ask ourselves: is the angle equal to 90 degrees?
If this is true then it is a right angle. Otherwise, if it is not true, check if the angle is less than 90 degrees. Otherwise, if this is not true either, it means, by exclusion, that the angle is obtuse.
Clearly we could also reason differently, for example by entering angle < 90 as the first condition and so on.
angle = int(input('Enter the angle:'))
if angle == 90:
print('The angle is right')
elif angle < 90:
print('The angle is acute' ')
else:
print('The angle is obtuse')
Second example on Python if
Let's now develop a second corner algorithm in Python.
Determine if an angle is right, flat, round, or any.
This algorithm is also quite simple to implement, for example we can start from the condition: angle == 90.
If this condition is true, then the angle is right. Otherwise I check if it is equal to 180 and I see that the angle is flat. Otherwise I check if the angle is equal to 360 and in this case I visualize that the angle is round. If all the previous conditions are not verified then it is any angle.
angle = int(input('Enter the angle'))
if angle == 90:
print('The angle is right')
elif angle == 180:
print('The corner is flat')
elif angle == 360:
print('The corner is round')
else:
print('The angle is any')
We have solved some examples Python if statement, proposing some possible solutions. Feel free to propose your observations or solutions in the comments below.
Some useful links
How to find the maximum of N numbers