How to Check If a Number Is Within a Range in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a number falls within a specific range in Python. The lab focuses on defining numeric ranges and utilizing comparison operators to determine if a given number lies within a defined interval.

You will create a Python script named ranges.py to explore these concepts. The script will demonstrate how to define a number and then use comparison operators (e.g., <=) to check if that number is within a specified range, such as 10 to 100. By modifying the number and running the script, you'll observe how the output changes based on whether the number falls within or outside the defined range.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/BasicConceptsGroup -.-> python/booleans("Booleans") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/numeric_types -.-> lab-559555{{"How to Check If a Number Is Within a Range in Python"}} python/booleans -.-> lab-559555{{"How to Check If a Number Is Within a Range in Python"}} python/conditional_statements -.-> lab-559555{{"How to Check If a Number Is Within a Range in Python"}} python/build_in_functions -.-> lab-559555{{"How to Check If a Number Is Within a Range in Python"}} end

Define Numeric Ranges

In this step, you will learn how to define numeric ranges in Python. Numeric ranges are useful when you need to check if a number falls within a specific interval. Python provides several ways to define and use numeric ranges, including using comparison operators and the range() function.

First, let's create a Python script named ranges.py in the ~/project directory using the VS Code editor.

#!/usr/bin/env python3

## Define a variable
number = 50

## Check if the number is within a range using comparison operators
if 10 <= number <= 100:
    print(f"{number} is within the range of 10 to 100")
else:
    print(f"{number} is outside the range of 10 to 100")

In this script:

  • We define a variable number and assign it the value 50.
  • We use comparison operators (<=) to check if number is within the range of 10 to 100.
  • If the condition is true, we print a message indicating that the number is within the range. Otherwise, we print a message indicating that the number is outside the range.

Now, let's run the script:

python ~/project/ranges.py

You should see the following output:

50 is within the range of 10 to 100

Let's modify the script to check a different number. Change the value of the number variable to 5 and run the script again.

#!/usr/bin/env python3

## Define a variable
number = 5

## Check if the number is within a range using comparison operators
if 10 <= number <= 100:
    print(f"{number} is within the range of 10 to 100")
else:
    print(f"{number} is outside the range of 10 to 100")

Run the script:

python ~/project/ranges.py

You should see the following output:

5 is outside the range of 10 to 100

This demonstrates how to use comparison operators to define and check numeric ranges in Python.

Use Comparison Operators

In this step, you will deepen your understanding of comparison operators in Python and how they can be used to define more complex numeric ranges. Comparison operators allow you to create conditions that check relationships between values.

Let's continue working with the ranges.py file in the ~/project directory. We'll modify the script to include more comparison operators.

#!/usr/bin/env python3

## Define a variable
number = 50

## Check if the number is greater than or equal to 20 and less than 80
if number >= 20 and number < 80:
    print(f"{number} is greater than or equal to 20 and less than 80")
else:
    print(f"{number} is not greater than or equal to 20 and less than 80")

## Check if the number is equal to 50 or equal to 100
if number == 50 or number == 100:
    print(f"{number} is either 50 or 100")
else:
    print(f"{number} is not 50 or 100")

In this script:

  • We use the >= (greater than or equal to) and < (less than) operators to check if number is within a specific range.
  • We use the and operator to combine two conditions, both of which must be true for the entire condition to be true.
  • We use the == (equal to) operator to check if number is equal to a specific value.
  • We use the or operator to combine two conditions, where at least one of them must be true for the entire condition to be true.

Now, let's run the script:

python ~/project/ranges.py

You should see the following output:

50 is greater than or equal to 20 and less than 80
50 is either 50 or 100

Let's modify the script again to change the value of number to 100 and observe the output.

#!/usr/bin/env python3

## Define a variable
number = 100

## Check if the number is greater than or equal to 20 and less than 80
if number >= 20 and number < 80:
    print(f"{number} is greater than or equal to 20 and less than 80")
else:
    print(f"{number} is not greater than or equal to 20 and less than 80")

## Check if the number is equal to 50 or equal to 100
if number == 50 or number == 100:
    print(f"{number} is either 50 or 100")
else:
    print(f"{number} is not 50 or 100")

Run the script:

python ~/project/ranges.py

You should see the following output:

100 is not greater than or equal to 20 and less than 80
100 is either 50 or 100

This demonstrates how to use comparison operators and logical operators (and, or) to define more complex conditions for numeric ranges in Python.

Check with range() for Integers

In this step, you will learn how to use the range() function in Python to generate a sequence of numbers and check if an integer falls within that range. The range() function is particularly useful when you need to iterate over a sequence of numbers or create a list of integers within a specific interval.

Let's create a new Python script named range_check.py in the ~/project directory using the VS Code editor.

#!/usr/bin/env python3

## Define a variable
number = 25

## Check if the number is within the range of 1 to 50 (exclusive)
if number in range(1, 50):
    print(f"{number} is within the range of 1 to 49")
else:
    print(f"{number} is outside the range of 1 to 49")

## Check if the number is within the range of 0 to 100 with a step of 5
if number in range(0, 101, 5):
    print(f"{number} is within the range of 0 to 100 with a step of 5")
else:
    print(f"{number} is outside the range of 0 to 100 with a step of 5")

In this script:

  • We define a variable number and assign it the value 25.
  • We use the range(1, 50) function to generate a sequence of numbers from 1 up to (but not including) 50.
  • We use the in operator to check if number is present in the generated sequence.
  • We use the range(0, 101, 5) function to generate a sequence of numbers from 0 up to (but not including) 101, with a step of 5 (i.e., 0, 5, 10, 15, ..., 100).

Now, let's run the script:

python ~/project/range_check.py

You should see the following output:

25 is within the range of 1 to 49
25 is within the range of 0 to 100 with a step of 5

Let's modify the script to change the value of number to 7 and observe the output.

#!/usr/bin/env python3

## Define a variable
number = 7

## Check if the number is within the range of 1 to 50 (exclusive)
if number in range(1, 50):
    print(f"{number} is within the range of 1 to 49")
else:
    print(f"{number} is outside the range of 1 to 49")

## Check if the number is within the range of 0 to 100 with a step of 5
if number in range(0, 101, 5):
    print(f"{number} is within the range of 0 to 100 with a step of 5")
else:
    print(f"{number} is outside the range of 0 to 100 with a step of 5")

Run the script:

python ~/project/range_check.py

You should see the following output:

7 is within the range of 1 to 49
7 is outside the range of 0 to 100 with a step of 5

This demonstrates how to use the range() function and the in operator to check if an integer falls within a specific range in Python.

Summary

In this lab, you learned how to define numeric ranges in Python and check if a number falls within a specific interval. The initial step involved creating a Python script named ranges.py and using comparison operators (<=) to determine if a given number was within the range of 10 to 100.

The lab demonstrated how to modify the script to test different numbers and observe the corresponding output, confirming whether each number was within or outside the defined range. This provided a practical understanding of using comparison operators for range checking in Python.