Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Check whether a number is in a given range - Python Program

Example 1:

num = int(input("Enter a number: ")) if 10 <= num <= 100: print("Within range 10 to 100") else: print("Out of range")

Output

 
OUTPUT 1:
Enter a number: 55
Within range 10 to 100

OUTPUT 2:
Enter a number: 101
Out of range
 

Example 2: Advanced Program

def check_number_in_range(number, lower_bound, upper_bound): """ Checks if a given number is within a specified range (inclusive). Args: number: The integer to check. lower_bound: The lower limit of the range (inclusive). upper_bound: The upper limit of the range (inclusive). Returns: True if the number is within the range, False otherwise. """ if lower_bound <= number <= upper_bound: return True else: return False # Get input from the user num_to_check = int(input("Enter the number to check: ")) range_start = int(input("Enter the lower bound of the range: ")) range_end = int(input("Enter the upper bound of the range: ")) # Check and print the result if check_number_in_range(num_to_check, range_start, range_end): print(f"{num_to_check} is within the range [{range_start}, {range_end}].") else: print(f"{num_to_check} is NOT within the range [{range_start}, {range_end}].")

Output

 
OUTPUT 1:
Enter the number to check: 5
Enter the lower bound of the range: 1
Enter the upper bound of the range: 10
5 is within the range [1, 10].

OUTPUT 2:
Enter the number to check: 11
Enter the lower bound of the range: 1
Enter the upper bound of the range: 10
11 is NOT within the range [1, 10].