Check if a number is the cube of 3

Check if a number is the cube of 3

EasyIf-Else Programs · 42 of 64

Write a Python program to check whether a number is the cube of 3, and in general whether it is a perfect cube.

Input
27, 28
Output
27 cube of 3: True | perfect cube: True
28 cube of 3: False | perfect cube: False

Approach

  1. Compare with 3 ** 3; for the general test round the cube root and cube it again.

Solution

Python
for n in [27, 28]:    root = round(n ** (1 / 3))    print(n, "cube of 3:", n == 3 ** 3, "| perfect cube:", root ** 3 == n)
Output
27 cube of 3: True | perfect cube: True
28 cube of 3: False | perfect cube: False

Line by line

Line 2

n ** (1/3) is a float cube root such as 3.0000000000000004, so it is rounded before checking.

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 number is the square of 6

Check if a number is the square of 6

EasyIf-Else Programs · 41 of 64

Write a Python program to check whether a number is the square of 6, and in general whether it is a perfect square.

Input
37, 36
Output
37 square of 6: False | perfect square: False
36 square of 6: True | perfect square: True

Approach

  1. Compare with 6 ** 2, and use math.isqrt for the general test.

Solution

Python
import math for n in [37, 36]:    root = math.isqrt(n)    print(n, "square of 6:", n == 6 ** 2, "| perfect square:", root * root == n)
Output
37 square of 6: False | perfect square: False
36 square of 6: True | perfect square: True

Line by line

Line 4

isqrt gives the whole-number square root.

Line 5

A number is a perfect square when that root squared gives the number back.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Check divisibility by 12

Check divisibility by 12

EasyIf-Else Programs · 40 of 64

Write a Python program to check whether a number is divisible by 12.

Input
121, 144
Output
121 False
144 True

Approach

  1. Check n % 12 == 0.

Solution

Python
for n in [121, 144]:    if n % 12 == 0:        print(n, True)    else:        print(n, False)
Output
121 False
144 True

Line by line

Line 2

121 leaves a remainder of 1, so it is not divisible by 12.

Note

The original sample output said True for 121, but 121 / 12 is 10 remainder 1, so the correct answer is False.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

Round a float to 2 decimals

Round a float to 2 decimals

EasyIf-Else Programs · 39 of 64

Write a Python program that checks whether a value is a float and, if so, rounds it to 2 decimal places.

Input
25.3614, 40
Output
25.3614 -> 25.36
40 is not a float

Approach

  1. Check the type with isinstance, then use round(x, 2).

Solution

Python
for value in [25.3614, 40]:    if isinstance(value, float):        print(value, "->", round(value, 2))    else:        print(value, "is not a float")
Output
25.3614 -> 25.36
40 is not a float

Line by line

Line 3

round(value, 2) keeps two digits after the decimal point.

Learn the concept

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

More if-else programs

← All 64 If-Else Programs

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