• 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
  • VBA
  • 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 Increment Dictionary Value
  • 2.  Write Variable to File Using Python
  • 3.  Create Empty Tuple in Python
  • 4.  pandas mean – Get Average of Series or DataFrame Columns
  • 5.  pandas variance – Compute Variance of Variables in DataFrame
  • 6.  Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 7.  Check if Character is Letter in Python
  • 8.  Using Python to Replace Backslash in String
  • 9.  How to Check if Number is Divisible by 3 in Python
  • 10.  Remove Extension from Filename in Python

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy