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

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