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.
1# Take number2num = int(input("Enter a number: "))34# Check range5if 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
Convert the response to int.
- 2
Evaluate 100 <= num <= 999.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum