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

Discount on a bill

Discount on a bill

EasyIf-Else Programs · 52 of 64

A shop gives 20% off bills above 2000 and 10% off bills above 1000. Write a Python program to calculate the discount.

Input
1500, 2500, 800
Output
Bill 1500: discount 150.0, pay 1350.0
Bill 2500: discount 500.0, pay 2000.0
Bill 800: discount 0, pay 800

Approach

  1. Check the bigger threshold first.

Solution

Python
for bill in [1500, 2500, 800]:    if bill > 2000:        discount = bill * 0.20    elif bill > 1000:        discount = bill * 0.10    else:        discount = 0    print(f"Bill {bill}: discount {discount}, pay {bill - discount}")
Output
Bill 1500: discount 150.0, pay 1350.0
Bill 2500: discount 500.0, pay 2000.0
Bill 800: discount 0, pay 800

Line by line

Lines 2-5

Testing > 2000 first stops a 2500 bill from getting only 10%.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Is the rectangle a square?

Is the rectangle a square?

EasyIf-Else Programs · 51 of 64

Write a Python program that takes the length and breadth of a rectangle and checks whether it is a square.

Input
length 4, breadth 5
Output
Length: 4
Breadth: 5
It is not a square

Approach

  1. A rectangle is a square when length equals breadth.

Solution

Python
length = float(input("Length: "))breadth = float(input("Breadth: "))if length == breadth:    print("It is a square")else:    print("It is not a square")
Outputtyped input: 4, 5
Length: 4
Breadth: 5
It is not a square

Line by line

Lines 1-2

float() accepts decimal lengths too.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Bonus eligibility

Bonus eligibility

EasyIf-Else Programs · 50 of 64

Write a Python program that gives a 10% bonus to employees who have served more than 4 years.

Input
(5 years, salary 60000), (3 years, salary 45000)
Output
5 years: eligible, bonus = 6000.0
3 years: not eligible for a bonus

Approach

  1. Compare years with 4; calculate 10% of the salary when eligible.

Solution

Python
for years, salary in [(5, 60000), (3, 45000)]:    if years > 4:        bonus = salary * 10 / 100        print(f"{years} years: eligible, bonus = {bonus}")    else:        print(f"{years} years: not eligible for a bonus")
Output
5 years: eligible, bonus = 6000.0
3 years: not eligible for a bonus

Line by line

Line 3

10% of the salary.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs