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

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