Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check if Number in Range

Test whether a whole number lies between one hundred and nine hundred ninety-nine. A Python lesson on inclusive chained comparisons and boundary testing.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 8 of 10
check-if-number-in-range.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Check range
5if 100 <= num <= 999:
6 print("Number is in range [100, 999]")
7else:
8 print("Number is not in range [100, 999]")

Output

Enter a number: 500
Number is in range [100, 999]

Enter a number: 50
Number is not in range [100, 999]

In this one

  • Write an inclusive range
  • Use chained comparisons
  • Test both endpoints

How it works

  1. 1

    Convert the response to int.

  2. 2

    Evaluate 100 <= num <= 999.

  3. 3

    Print whether the value lies inside that interval.

Edge cases

Is 100 inside?

Is 100 inside?

Yes. The lower bound is inclusive.

Is -500 inside?

Is -500 inside?

No. It is below the lower bound despite having three written digits.

What's going on

The chained expression 100 <= num <= 999 requires both bounds to hold. Python reads it like a pair of comparisons joined by and.

Input 500 passes, while 50 fails the lower bound. Both endpoints count as inside because the operators include equality. Unlike the magnitude-based digit classifier, this program does not apply abs, so negative three-digit numbers fail.

Is 100 inside? Yes. The lower bound is inclusive.

Is -500 inside? No. It is below the lower bound despite having three written digits.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which value is outside?

Pick an answer to check it.

Question 2

Is 100 inside?

Pick an answer to check it.

Questions people ask

Is 100 inside?

Yes. The lower bound is inclusive.

Is -500 inside?

No. It is below the lower bound despite having three written digits.

Which value is outside?

1000.