Check a vowel

Check a vowel

EasyIf-Else Programs · 32 of 64

Write a Python program to check whether a letter is a vowel.

Input
"A", "k"
Output
A True
k False

Approach

  1. Lower-case the letter and check membership in “aeiou”.

Solution

Python
for ch in ["A", "k"]:    if ch.lower() in "aeiou":        print(ch, True)    else:        print(ch, False)
Output
A True
k False

Line by line

Line 2

in on a string checks whether the character appears in it.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Fizz Buzz for multiples of 2 and 3

Fizz Buzz for multiples of 2 and 3

EasyIf-Else Programs · 31 of 64

Write a Python program that prints “FizzBuzz” for multiples of both 2 and 3, “Fizz” for multiples of 2 and “Buzz” for multiples of 3; otherwise print the number.

Input
14, 9, 6, 7
Output
14 Fizz
9 Buzz
6 FizzBuzz
7 7

Approach

  1. Test the combined case first, then each single case.

Solution

Python
for n in [14, 9, 6, 7]:    if n % 2 == 0 and n % 3 == 0:        print(n, "FizzBuzz")    elif n % 2 == 0:        print(n, "Fizz")    elif n % 3 == 0:        print(n, "Buzz")    else:        print(n, n)
Output
14 Fizz
9 Buzz
6 FizzBuzz
7 7

Line by line

Line 2

Multiples of both must be checked first, otherwise 6 would stop at “Fizz”.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check a leap year

Check a leap year

EasyIf-Else Programs · 30 of 64

Write a Python program to check whether a year is a leap year.

Input
2000, 1900, 2024
Output
2000 is a leap year
1900 is not a leap year
2024 is a leap year

Approach

  1. Divisible by 400: leap. Else divisible by 100: not leap. Else divisible by 4: leap.

Solution

Python
for year in [2000, 1900, 2024]:    if year % 400 == 0:        print(year, "is a leap year")    elif year % 100 == 0:        print(year, "is not a leap year")    elif year % 4 == 0:        print(year, "is a leap year")    else:        print(year, "is not a leap year")
Output
2000 is a leap year
1900 is not a leap year
2024 is a leap year

Line by line

Lines 2-7

The rules are checked from the most specific (400) to the most general (4), which is why the order matters.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Electricity bill with slabs

Electricity bill with slabs

MediumIf-Else Programs · 29 of 64

Write a Python program to calculate an electricity bill. Units 1-50 cost Rs 0.50, units 51-100 Rs 0.75, units 101-250 Rs 1.25 and units above 250 Rs 1.50 each. Add a 17% surcharge.

Input
350 units
Output
40 units -> 23.4
120 units -> 102.38
350 units -> 468.0

Approach

  1. Charge each slab only for the units that fall in it.
  2. Add 17% to the total.

Solution

Python
def bill(units):    amount = 0    if units > 250:        amount += (units - 250) * 1.50        units = 250    if units > 100:        amount += (units - 100) * 1.25        units = 100    if units > 50:        amount += (units - 50) * 0.75        units = 50    amount += units * 0.50    return round(amount * 1.17, 2) for u in [40, 120, 350]:    print(u, "units ->", bill(u))
Output
40 units -> 23.4
120 units -> 102.38
350 units -> 468.0

Line by line

Lines 3-5

Units above 250 are charged at 1.50, then units is capped at 250 for the next slab.

Lines 6-11

Each lower slab takes the units between its limits.

Line 12

The first 50 units (or fewer) cost 0.50 each.

Line 13

Multiplying by 1.17 adds the 17% surcharge.

Note

The original sample output (438.75 for 350 units) did not follow these slabs. With slab pricing, 350 units cost 25 + 37.5 + 187.5 + 150 = 400, plus 17% = 468.0.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Print 10 to 15 except 13

Print 10 to 15 except 13

EasyIf-Else Programs · 28 of 64

Write a Python program to print all the numbers from 10 to 15 except 13.

Output
10
11
12
14
15

Approach

  1. Loop through the range and skip 13 with continue.

Solution

Python
for n in range(10, 16):    if n == 13:        continue    print(n)
Output
10
11
12
14
15

Line by line

Lines 2-3

continue skips the rest of the loop body for 13 only.

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 string

Check if a value is a string

EasyIf-Else Programs · 27 of 64

Write a Python program to check whether the given input is a string.

Input
"sqatools", 100
Output
'sqatools' True
100 False

Approach

  1. Use isinstance(value, str).

Solution

Python
for value in ["sqatools", 100]:    print(repr(value), isinstance(value, str))
Output
'sqatools' True
100 False

Line by line

Line 2

isinstance returns the True/False answer directly.

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 float

Check if a value is a float

EasyIf-Else Programs · 26 of 64

Write a Python program to check whether a given value is a float.

Input
12.6, 12
Output
12.6 is a float
12 is not a float

Approach

  1. Use isinstance(value, float).

Solution

Python
for value in [12.6, 12]:    if isinstance(value, float):        print(value, "is a float")    else:        print(value, "is not a float")
Output
12.6 is a float
12 is not a float

Line by line

Line 2

12 is an int, even though it equals 12.0 numerically.

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 an integer

Check if a value is an integer

EasyIf-Else Programs · 25 of 64

Write a Python program to check whether a given value is an integer.

Input
54, 5.4, "54"
Output
54 True
5.4 False
'54' False
True False

Approach

  1. Use isinstance(value, int), and exclude booleans, which are a kind of int.

Solution

Python
for value in [54, 5.4, "54", True]:    if isinstance(value, int) and not isinstance(value, bool):        print(repr(value), True)    else:        print(repr(value), False)
Output
54 True
5.4 False
'54' False
True False

Line by line

Line 2

isinstance checks the type. True is technically an int in Python, so it is excluded explicitly.

Line 3

repr() shows strings with quotes, so “54” is clearly text.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check a lowercase character

Check a lowercase character

EasyIf-Else Programs · 24 of 64

Write a Python program to check whether a character is lowercase.

Input
"c", "Q"
Output
c True
Q False

Approach

  1. Use islower(), or compare with the range “a” to “z”.

Solution

Python
for ch in ["c", "Q"]:    if "a" <= ch <= "z":        print(ch, True)    else:        print(ch, False)
Output
c True
Q False

Line by line

Line 2

Characters compare by their Unicode order, so a chained comparison checks the a-z range. ch.islower() works too and also handles non-English letters.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Larger of two numbers

Larger of two numbers

EasyIf-Else Programs · 22 of 64

Write a Python program to print the larger of two numbers.

Input
25, 63
Output
63

Approach

  1. Compare them with >; handle equal numbers too.

Solution

Python
a, b = 25, 63if a > b:    print(a)elif b > a:    print(b)else:    print("Both are equal:", a)
Output
63

Line by line

Lines 6-7

Equal numbers get their own message.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs