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

Which group of ten a number belongs to

Which group of ten a number belongs to

MediumIf-Else Programs · 49 of 64

Write a Python program that splits 1-100 into 10 groups (1-10, 11-20, … 91-100) and finds which group a number belongs to, using nested if-else.

Input
36, 100, 1, 105
Output
36 belongs to the 4th group
100 belongs to the 10th group
1 belongs to the 1st group
105 is outside 1-100

Approach

  1. Check the range is valid with the outer if.
  2. Calculate the group with (n - 1) // 10 + 1 inside it.

Solution

Python
def group_of(n):    if 1 <= n <= 100:        group = (n - 1) // 10 + 1        if group == 1:            suffix = "st"        elif group == 2:            suffix = "nd"        elif group == 3:            suffix = "rd"        else:            suffix = "th"        return f"{n} belongs to the {group}{suffix} group"    else:        return f"{n} is outside 1-100" for n in [36, 100, 1, 105]:    print(group_of(n))
Output
36 belongs to the 4th group
100 belongs to the 10th group
1 belongs to the 1st group
105 is outside 1-100

Line by line

Line 2

The outer check rejects values outside the range.

Line 3

Subtracting 1 makes 1-10 map to 0, 11-20 to 1 and so on; adding 1 gives the group number.

Lines 4-11

The inner if-else picks the English ordinal suffix.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Roller coaster eligibility

Roller coaster eligibility

EasyIf-Else Programs · 48 of 64

Write a Python program to check whether a person can ride a roller coaster: eligible when age is greater than 12.

Input
15, 12
Output
15 -> You are eligible
12 -> You are not eligible

Approach

  1. Compare the age with 12 using >.

Solution

Python
for age in [15, 12]:    if age > 12:        print(age, "-> You are eligible")    else:        print(age, "-> You are not eligible")
Output
15 -> You are eligible
12 -> You are not eligible

Line by line

Line 2

“Greater than 12” excludes exactly 12.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check if a value is a dictionary

Check if a value is a dictionary

EasyIf-Else Programs · 47 of 64

Write a Python program to check whether a value is a dictionary.

Input
{"name": "Virat", "sport": "cricket"}
Output
It is a dictionary with keys ['name', 'sport']

Approach

  1. Use isinstance(value, dict).

Solution

Python
player = {"name": "Virat", "sport": "cricket"}if isinstance(player, dict):    print("It is a dictionary with keys", list(player))else:    print("It is not a dictionary")
Output
It is a dictionary with keys ['name', 'sport']

Line by line

Line 3

list(player) lists the dictionary’s keys.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check if a value is a list

Check if a value is a list

EasyIf-Else Programs · 46 of 64

Write a Python program to check whether a value is a list.

Input
[1, 3, 6, 8], (1, 3)
Output
[1, 3, 6, 8] True
(1, 3) False

Approach

  1. Use isinstance(value, list).

Solution

Python
for value in [[1, 3, 6, 8], (1, 3)]:    print(value, isinstance(value, list))
Output
[1, 3, 6, 8] True
(1, 3) False

Line by line

Line 2

A tuple is a different type, so it returns False.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check a boolean value

Check a boolean value

EasyIf-Else Programs · 45 of 64

Write a Python program to check whether a value is of boolean type.

Input
True, 1
Output
True is a boolean
1 is not a boolean

Approach

  1. Use isinstance(value, bool); note 1 is an int, not a bool.

Solution

Python
for value in [True, 1]:    if isinstance(value, bool):        print(value, "is a boolean")    else:        print(value, "is not a boolean")
Output
True is a boolean
1 is not a boolean

Line by line

Line 2

True == 1 is True, but 1 is still an int, so the type check distinguishes them.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check a complex number

Check a complex number

EasyIf-Else Programs · 44 of 64

Write a Python program to check whether a value is of complex type.

Input
5+6j, 5
Output
(5+6j) True
5 False

Approach

  1. Use isinstance(value, complex).

Solution

Python
for value in [5 + 6j, 5]:    print(value, isinstance(value, complex))
Output
(5+6j) True
5 False

Line by line

Line 1

j marks the imaginary part in Python.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check if two numbers are equal

Check if two numbers are equal

EasyIf-Else Programs · 43 of 64

Write a Python program to check whether two numbers are equal.

Input
(26, 88), (5, 5.0)
Output
26 88 -> the numbers are not equal
5 5.0 -> the numbers are equal

Approach

  1. Use ==.

Solution

Python
for a, b in [(26, 88), (5, 5.0)]:    if a == b:        print(a, b, "-> the numbers are equal")    else:        print(a, b, "-> the numbers are not equal")
Output
26 88 -> the numbers are not equal
5 5.0 -> the numbers are equal

Line by line

Line 2

5 == 5.0 is True because the values are equal even though the types differ.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs