Program - 3
Python Code
# Find the greatest of three numbers.
#Input three numbers.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
#Compare the numbers.
if(num1>=num2 and num1>=num3):
print(num1," is the greatest")
if(num2>=num1 and num2>=num3):
print(num2," is the greatest")
if(num3>=num1 and num3>=num2):
print(num3," is the greatest")
Explanation
Comment (Explanation line)
# Find the greatest of three numbers.
# means comment in Python.
Python ignores this line.
It only helps humans understand what the program does.
Taking input from user
First number
num1 = int(input("Enter the first number: "))
input() → takes value from keyboard (always as string)
int() → converts that string into integer
The value is stored in variable num1
Example:
User types → 10
num1 = 10
Second number
num2 = int(input("Enter the second number: "))
Stores second number in num2.
Third number
num3 = int(input("Enter the third number: "))
Stores third number in num3.
Comparing numbers
Condition 1
if(num1>=num2 and num1>=num3):
print(num1," is the greatest")
Meaning:
Check →
👉 Is num1 ≥ num2 AND num1 ≥ num3 ?
>= → greater than or equal
and → both conditions must be true
If true → print num1 as greatest.
Condition 2
if(num2>=num1 and num2>=num3):
print(num2," is the greatest")
Check →
👉 Is num2 ≥ num1 AND num2 ≥ num3 ?
If true → print num2 as greatest.
Condition 3
if(num3>=num1 and num3>=num2):
print(num3," is the greatest")
Check →
👉 Is num3 ≥ num1 AND num3 ≥ num2 ?
If true → print num3 as greatest.
Important Concept (Very Important!)
We have used three separate if statements, NOT elif.
So, Python checks ALL conditions, not just one.
That means multiple outputs can occur.
Example 1
Input:
10 25 15
Output:
25 is the greatest
Example 2 (Equal numbers)
Input:
50 50 20
Output:
50 is the greatest
50 is the greatest
Because:
num1 ≥ num2 and num1 ≥ num3 ✔
num2 ≥ num1 and num2 ≥ num3 ✔
So, it prints twice.
Example 3
Input:
555
Output:
5 is the greatest
5 is the greatest
5 is the greatest
All three conditions are true.
Key Learning
if → checks independently
elif → checks only when previous condition is false