• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if Number is Between Two Numbers Using Python

Check if Number is Between Two Numbers Using Python

June 1, 2022 Leave a Comment

In Python, you can easily check if a number is between two numbers with an if statement, and the and logical operator.

def between_two_numbers(num,a,b):
    if a < num and num < b: 
        return True
    else: 
        return False

You can also use the Python range() function to check if a number is in a range between two numbers.

def between_two_numbers(num,a,b):
    if b < a:
        a, b = b, a
    if num in range(a,b):
        return True
    else:
        return False

When working with numbers in Python, the ability to easily check for certain conditions is very valuable.

One such situation is if you want to check if a number is in a range of numbers or is between two numbers.

In Python, you can easily check if a number is between two numbers with an if statement, and the and logical operator.

All you need to do is check if a number is greater than the lower bound of the range and less than the upper bound of the range. Then, you can use and to create a multiple condition if statement.

Below is a simple function which will check if a number is between two numbers using Python.

def between_two_numbers(num,a,b):
    if a < num and num < b: 
        return True
    else: 
        return False

print(between_two_numbers(10,5,15))
print(between_two_numbers(20,5,15))

#Output:
True
False

Using range() to Check if a Number is Between Two Numbers in Python

Another way to check if a number is between two numbers in Python is to use the Python range() function and check if the number is included in a created range.

To create a range, you can pass two numbers to range(). Then you can use the in logical operator to check if a number is in the created range.

Below is a simple function which will check if a number is in a range of numbers and between two numbers using Python.

def between_two_numbers(num,a,b):
    if b < a:
        a, b = b, a
    if num in range(a,b):
        return True
    else:
        return False

print(between_two_numbers(10,5,15))
print(between_two_numbers(20,5,15))

#Output:
True
False

Hopefully this article has been useful for you to learn how to

Other Articles You'll Also Like:

  • 1.  Using Python to Check if Queue is Empty
  • 2.  Using Python to Reverse Tuple
  • 3.  pandas dropna – Drop Rows or Columns with NaN in DataFrame
  • 4.  Multiple Condition While Loops in Python
  • 5.  Reverse a List in Python Without Reverse Function
  • 6.  Split Column by Delimiter in pandas DataFrame
  • 7.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 8.  Multiply Each Element in List by Scalar Value with Python
  • 9.  Factorial Program in Python Using For Loop and While Loop
  • 10.  Using Python to Insert Tab in String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy