Take Two Numbers and Print the Larger One

Compare two numbers and print the larger one.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 6 of 10
take-two-numbers-and-print-the-larger-one.py
1# Take two numbers as input
2num1 = float(input("Enter first number: "))
3num2 = float(input("Enter second number: "))
4
5# Find and print larger number
6if num1 > num2:
7 print(f"Larger number: {num1}")
8elif num2 > num1:
9 print(f"Larger number: {num2}")
10else:
11 print("Both numbers are equal")

Output

Enter first number: 10
Enter second number: 20
Larger number: 20.0

What's going on

Compare two numbers using relational operators.

Key Concepts:

Use > to check if first number is greater
Use elif to check if second number is greater
Use else for the case when both are equal