Allot a stream from marks

Allot a stream from marks

EasyIf-Else Programs · 64 of 64

Write a Python program that allots a stream: marks above 85 Science, above 70 Commerce, 35 to 70 Arts, below 35 Fail.

Input
88, 72, 50, 20
Output
88 -> Science
72 -> Commerce
50 -> Arts
20 -> Fail
70 -> Arts
35 -> Arts

Approach

  1. Test from the highest band down so every mark lands in exactly one stream.

Solution

Python
def stream(marks):    if marks > 85:        return "Science"    elif marks > 70:        return "Commerce"    elif marks >= 35:        return "Arts"    else:        return "Fail" for m in [88, 72, 50, 20, 70, 35]:    print(m, "->", stream(m))
Output
88 -> Science
72 -> Commerce
50 -> Arts
20 -> Fail
70 -> Arts
35 -> Arts

Line by line

Lines 2-9

Using >= for 35 and falling through from the band above means boundary values such as 70 and 35 are covered. The original ranges (35 < marks < 70) left 35 and 70 with no stream.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Calculator with an operator

Calculator with an operator

MediumIf-Else Programs · 63 of 64

Write a Python program that accepts two numbers and an operator (+, -, *, /) and prints the result.

Input
30, 45, +
Output
30 + 45 = 75
30 * 45 = 1350
9 / 0 = cannot divide by zero
2 % 3 = unknown operator

Approach

  1. Choose the operation with if-elif.
  2. Guard against division by zero.

Solution

Python
def calculate(a, b, op):    if op == "+":        return a + b    elif op == "-":        return a - b    elif op == "*":        return a * b    elif op == "/":        if b == 0:            return "cannot divide by zero"        return a / b    else:        return "unknown operator" for a, b, op in [(30, 45, "+"), (30, 45, "*"), (9, 0, "/"), (2, 3, "%")]:    print(a, op, b, "=", calculate(a, b, op))
Output
30 + 45 = 75
30 * 45 = 1350
9 / 0 = cannot divide by zero
2 % 3 = unknown operator

Line by line

Lines 8-11

A nested if checks for zero before dividing.

Lines 12-13

Any other symbol gets a clear message.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

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