Is the water boiling?

Is the water boiling?

EasyIf-Else Programs · 62 of 64

Write a Python program that reads a temperature in Fahrenheit and says whether water is boiling (212 °F or more).

Input
190
Output
Enter temperature in F: 190
Water is not boiling (22 degrees to go)

Approach

  1. Compare with 212.

Solution

Python
temp = float(input("Enter temperature in F: "))if temp >= 212:    print("Water is boiling")else:    print(f"Water is not boiling ({212 - temp:.0f} degrees to go)")
Outputtyped input: 190
Enter temperature in F: 190
Water is not boiling (22 degrees to go)

Line by line

Line 2

212 °F is the boiling point at sea level.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Smallest of three numbers

Smallest of three numbers

EasyIf-Else Programs · 61 of 64

Write a Python program to find the smallest of three numbers.

Input
A = 45, B = 23, C = 68
Output
Smallest: 23 | min(): 23

Approach

  1. Compare each with the other two using <=.

Solution

Python
a, b, c = 45, 23, 68if a <= b and a <= c:    smallest = aelif b <= c:    smallest = belse:    smallest = cprint("Smallest:", smallest, "| min():", min(a, b, c))
Output
Smallest: 23 | min(): 23

Line by line

Line 4

If a is not the smallest, only b and c remain to compare.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Senior citizen check

Senior citizen check

EasyIf-Else Programs · 60 of 64

Write a Python program to check whether a person is a senior citizen (age greater than 60).

Input
70, 45
Output
70 -> senior citizen
45 -> not a senior citizen

Approach

  1. Compare the age with 60.

Solution

Python
for age in [70, 45]:    if age > 60:        print(age, "-> senior citizen")    else:        print(age, "-> not a senior citizen")
Output
70 -> senior citizen
45 -> not a senior citizen

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Monuments of a city

Monuments of a city

EasyIf-Else Programs · 59 of 64

Write a Python program that accepts a city name (Pune or Mumbai) and displays its monuments.

Input
Pune
Output
Enter city name: Pune
Shaniwar Wada
Lal Mahal
Sinhagad Fort

Approach

  1. Compare the city name and print the matching monuments.

Solution

Python
city = input("Enter city name: ").strip().title()if city == "Pune":    monuments = ["Shaniwar Wada", "Lal Mahal", "Sinhagad Fort"]elif city == "Mumbai":    monuments = ["Gateway of India", "Chhatrapati Shivaji Terminus", "Elephanta Caves"]else:    monuments = [] if monuments:    for m in monuments:        print(m)else:    print("No data for", city)
Outputtyped input: Pune
Enter city name: Pune
Shaniwar Wada
Lal Mahal
Sinhagad Fort

Line by line

Line 1

title() makes “pune” and “PUNE” match “Pune”.

Line 9

An empty list is falsy, so the else branch handles unknown cities.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Day name from a number

Day name from a number

EasyIf-Else Programs · 58 of 64

Write a Python program that reads a number from 1 to 7 and prints the day (1 = Sunday, 2 = Monday, … 7 = Saturday).

Input
7
Output
Enter number: 7
Saturday

Approach

  1. Validate the range, then index into a list of day names.

Solution

Python
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]n = int(input("Enter number: "))if 1 <= n <= 7:    print(days[n - 1])else:    print("Enter a number from 1 to 7")
Outputtyped input: 7
Enter number: 7
Saturday

Line by line

Line 4

List indexes start at 0, so subtract 1.

Tip

A list lookup replaces seven elif branches.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Road tax by car price

Road tax by car price

EasyIf-Else Programs · 57 of 64

Write a Python program to display road tax: price below 5,00,000 pays 15,000; below 10,00,000 pays 50,000; below 15,00,000 pays 80,000.

Input
1200000
Output
450000 -> tax: 15000
1200000 -> tax: 80000
1800000 -> tax: not covered by these slabs

Approach

  1. Test the price bands from the lowest upwards.

Solution

Python
def road_tax(price):    if price < 500000:        return 15000    elif price < 1000000:        return 50000    elif price < 1500000:        return 80000    else:        return "not covered by these slabs" for price in [450000, 1200000, 1800000]:    print(price, "-> tax:", road_tax(price))
Output
450000 -> tax: 15000
1200000 -> tax: 80000
1800000 -> tax: not covered by these slabs

Line by line

Lines 2-7

Each elif only runs when the price is above the previous band.

Note

The original sample said a 12,00,000 car pays 50,000, but by the given rules prices from 10,00,000 to 15,00,000 pay 80,000.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Print 1 for Hello and 0 for Bye

Print 1 for Hello and 0 for Bye

EasyIf-Else Programs · 56 of 64

Write a Python program that prints 1 if the user enters Hello and 0 if the user enters Bye.

Input
Hello
Output
Enter your choice: Hello
1

Approach

  1. Compare the cleaned input with each word.

Solution

Python
choice = input("Enter your choice: ").strip().lower()if choice == "hello":    print(1)elif choice == "bye":    print(0)else:    print("Please type Hello or Bye")
Outputtyped input: Hello
Enter your choice: Hello
1

Line by line

Line 1

strip() removes stray spaces and lower() accepts any capitalisation.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Is the last digit divisible by 4?

Is the last digit divisible by 4?

EasyIf-Else Programs · 55 of 64

Write a Python program to check whether the last digit of a number is divisible by 4.

Input
58, 37
Output
58 -> last digit 8 is divisible by 4
37 -> last digit 7 is not divisible by 4

Approach

  1. Get the last digit with n % 10, then test % 4.

Solution

Python
for n in [58, 37]:    last = n % 10    if last % 4 == 0:        print(n, "-> last digit", last, "is divisible by 4")    else:        print(n, "-> last digit", last, "is not divisible by 4")
Output
58 -> last digit 8 is divisible by 4
37 -> last digit 7 is not divisible by 4

Line by line

Line 2

The remainder after dividing by 10 is the last digit.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Exam attendance eligibility

Exam attendance eligibility

EasyIf-Else Programs · 54 of 64

Write a Python program that allows a student into the exam only if attendance is at least 75%.

Input
attended 52 of 65 classes, 40 of 65 classes
Output
80.0% -> eligible
61.5% -> not eligible

Approach

  1. Calculate the percentage, then compare with 75.

Solution

Python
for attended in [52, 40]:    total = 65    percent = attended / total * 100    if percent >= 75:        print(f"{percent:.1f}% -> eligible")    else:        print(f"{percent:.1f}% -> not eligible")
Output
80.0% -> eligible
61.5% -> not eligible

Line by line

Line 3

Percentage = attended / total × 100.

Line 5

:.1f prints one decimal place.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs

Absolute value

Absolute value

EasyIf-Else Programs · 53 of 64

Write a Python program to print the absolute value of a number.

Input
-1, 7
Output
-1 -> 1 | abs(): 1
7 -> 7 | abs(): 7

Approach

  1. If the number is negative, negate it; or use abs().

Solution

Python
for n in [-1, 7]:    if n < 0:        result = -n    else:        result = n    print(n, "->", result, "| abs():", abs(n))
Output
-1 -> 1 | abs(): 1
7 -> 7 | abs(): 7

Line by line

Lines 2-5

Negating a negative number makes it positive.

Line 6

The built-in abs() gives the same result.

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs