Is a number in a range?

EasyFunction Programs · 8 of 39

Write a Python function to check whether a number is within a range (inclusive).

Advertisement
Input
7 in 2 to 20; 25 in 2 to 20
Output
7 is in the range
25 is outside the range

Approach

  1. A chained comparison.

Solution

Python
def in_range(n, low, high):    return low <= n <= high for n in [7, 25]:    print(n, "is in the range" if in_range(n, 2, 20) else "is outside the range")
Output
7 is in the range
25 is outside the range
Advertisement

Learn the concept

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

More function programs

← All 39 Function Programs