Season from a month

Season from a month

EasyIf-Else Programs · 38 of 64

Write a Python program that reads a month name and prints its season (Indian seasons: Winter Dec-Feb, Summer Mar-May, Monsoon Jun-Sep, Post-monsoon Oct-Nov).

Input
"February", "May", "August", "October"
Output
February -> Winter
May -> Summer
August -> Monsoon
October -> Post-monsoon

Approach

  1. Check which group of months contains the input.

Solution

Python
def season(month):    month = month.capitalize()    if month in ("December", "January", "February"):        return "Winter"    elif month in ("March", "April", "May"):        return "Summer"    elif month in ("June", "July", "August", "September"):        return "Monsoon"    elif month in ("October", "November"):        return "Post-monsoon"    return "Unknown month" for m in ["February", "May", "August", "October"]:    print(m, "->", season(m))
Output
February -> Winter
May -> Summer
August -> Monsoon
October -> Post-monsoon

Line by line

Lines 3-10

Each elif checks one season’s months.

Line 11

A final return covers anything that matched no season.

Note

The original sample said February is Summer. Season boundaries vary by country; this solution uses the common Indian classification with February in Winter.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Isosceles triangle check

Isosceles triangle check

EasyIf-Else Programs · 37 of 64

Write a Python program to check whether a triangle is isosceles (at least two equal sides).

Input
(10, 15, 10), (8, 9, 10)
Output
(10, 15, 10) is isosceles
(8, 9, 10) is not isosceles

Approach

  1. Any one pair of equal sides is enough, so join the checks with or.

Solution

Python
for a, b, c in [(10, 15, 10), (8, 9, 10)]:    if a == b or b == c or a == c:        print((a, b, c), "is isosceles")    else:        print((a, b, c), "is not isosceles")
Output
(10, 15, 10) is isosceles
(8, 9, 10) is not isosceles

Line by line

Line 2

or is true if any pair matches. By this definition an equilateral triangle is also isosceles.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Scalene triangle check

Scalene triangle check

EasyIf-Else Programs · 36 of 64

Write a Python program to check whether a triangle is scalene (all three sides different).

Input
(10, 15, 18), (10, 15, 10)
Output
(10, 15, 18) is scalene
(10, 15, 10) is not scalene

Approach

  1. All sides are different when no two are equal.

Solution

Python
for a, b, c in [(10, 15, 18), (10, 15, 10)]:    if a != b and b != c and a != c:        print((a, b, c), "is scalene")    else:        print((a, b, c), "is not scalene")
Output
(10, 15, 18) is scalene
(10, 15, 10) is not scalene

Line by line

Line 2

All three pairs must differ; len({a, b, c}) == 3 is a shorter test.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Equilateral triangle check

Equilateral triangle check

EasyIf-Else Programs · 35 of 64

Write a Python program to check whether a triangle is equilateral (all three sides equal).

Input
(10, 10, 10), (10, 15, 10)
Output
(10, 10, 10) is equilateral
(10, 15, 10) is not equilateral

Approach

  1. Compare all three sides with a chained ==.

Solution

Python
for a, b, c in [(10, 10, 10), (10, 15, 10)]:    if a == b == c:        print((a, b, c), "is equilateral")    else:        print((a, b, c), "is not equilateral")
Output
(10, 10, 10) is equilateral
(10, 15, 10) is not equilateral

Line by line

Line 2

a == b == c means a == b and b == c.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Days in a month

Days in a month

EasyIf-Else Programs · 34 of 64

Write a Python program to convert a month name to its number of days.

Input
"February", "April", "July"
Output
February -> 28/29 days
april -> 30 days
July -> 31 days
Smarch -> invalid month

Approach

  1. Group months by their length with in.

Solution

Python
def days_in(month):    month = month.capitalize()    if month == "February":        return "28/29 days"    elif month in ("April", "June", "September", "November"):        return "30 days"    elif month in ("January", "March", "May", "July", "August", "October", "December"):        return "31 days"    else:        return "invalid month" for m in ["February", "april", "July", "Smarch"]:    print(m, "->", days_in(m))
Output
February -> 28/29 days
april -> 30 days
July -> 31 days
Smarch -> invalid month

Line by line

Line 2

capitalize() accepts any letter case from the user.

Lines 5-8

Tuples list the 30-day and 31-day months.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check a consonant

Check a consonant

EasyIf-Else Programs · 33 of 64

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

Input
"B", "e", "7"
Output
B is a consonant
e is not a consonant
7 is not a consonant

Approach

  1. A consonant is a letter (isalpha()) that is not a vowel.

Solution

Python
for ch in ["B", "e", "7"]:    if ch.isalpha() and ch.lower() not in "aeiou":        print(ch, "is a consonant")    else:        print(ch, "is not a consonant")
Output
B is a consonant
e is not a consonant
7 is not a consonant

Line by line

Line 2

Checking isalpha() first stops digits and symbols from being called consonants.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

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