TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Check Integer Number in Range
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Python Check Integer Number in Range

Last updated: Jun 05, 2023 11:09 pm
By Meenakshi Agarwal
Share
7 Min Read
Python Check Integer Number in Range
Python Check Integer Number in Range
SHARE

This tutorial provides you with multiple methods to check if an integer number lies in the given range or not. It includes several examples to bring clarity.

Contents
Using Python comparison operatorPython range() to check integer in between two numbersPython xrange() to check integer in between two numbers

Let’s first define the problem. We want to verify whether an integer value lies between two other numbers, for example, 1000 and 7000:

So, we need a simple method that can tell us about any numeric value if it belongs to a given range. Hence, in this post, we’ll describe three ways of solving this problem. You can choose which of these suits you the best.

Two of these methods work in Python 3, and the third one is specific to Python 2.7.

Python | Check Integer in Range or Between Two Numbers

Let’s now open up all three ways to check if the integer number is in range or not.

Using Python comparison operator

In Python programming, we can use comparison operators to check whether a value is higher or less than the other. And then, we can take some action based on the result.

The below tutorial defines the built-in comparison operators available in Python. Check it out.

Python Operators

let’s now check out a sample code to see how to use the comparison operators.

"""
Desc:
Python program to check if the integer number is in between a range
"""

# Given range
X = 1000
Y = 7000

def checkRange(num):
   # using comaparision operator
   if X <= num <= Y:
       print('The number {} is in range ({}, {})'.format(num, X, Y))
   else:
      print('The number {} is not in range ({}, {})'.format(num, X, Y))

# Test Input List
testInput = [X-1, X, X+1, Y+1, Y, Y-1]

for eachItem in testInput:
   checkRange(eachItem)

We’ve written a function to check whether the input number is in the given range or not. It is using the following comparison operator syntax:

if X <= num <= Y:

Also, we’ve prepared the test data in a list accordingly. We wish to test all +ve and edge cases as well. Hence, we made the test input list, as shown below:

# Test Input List
testInput = [X-1, X, X+1, Y+1, Y, Y-1]

This list helps us run some regular test cases, upper and lower boundary tests. So, when we run the above program, it gets us the following result:

The number 999 is not in range (1000, 7000)
The number 1000 is in range (1000, 7000)
The number 1001 is in range (1000, 7000)
The number 7001 is not in range (1000, 7000)
The number 7000 is in range (1000, 7000) # We've included upper range as well
The number 6999 is in range (1000, 7000)

Python range() to check integer in between two numbers

We can also use the Python range function that does this job for us. It can quite easily identify if the integer lies between two numbers or not.

Please check the following example:

"""
Desc:
Python range to check if the integer is in between two numbers
"""

# Given range
X = 1000
Y = 7000

def checkRange(num):
   # using comaparision operator
   if num in range(X, Y):
       print('The number {} is in range ({}, {})'.format(num, X, Y))
   else:
      print('The number {} is not in range ({}, {})'.format(num, X, Y))

# Test Input
testInput = [X-1, X, X+1, Y+1, Y, Y-1]

for eachItem in testInput:
   checkRange(eachItem)

Here, we’ve called the range() function which includes the lower range (X) but discards the edge value, i.e., Y.

Hence, when you execute the above code, it results in the below output:

The number 999 is not in range (1000, 7000)
The number 1000 is in range (1000, 7000)
The number 1001 is in range (1000, 7000)
The number 7001 is not in range (1000, 7000)
The number 7000 is not in range (1000, 7000) # Python range doesn't include upper range value
The number 6999 is in range (1000, 7000)

Python xrange() to check integer in between two numbers

This method (xrange()) would only work in Python 2.7 or below. But since Python 2.7 is still in use, so we are giving an example of the same.

Please see the below coding snippet using the Python xrange function:

"""
Desc:
Python xrange to check if the integer is in between two numbers
"""

# Given range
X = 1000
Y = 7000

def checkRange(num):
   # using comaparision operator
   if num in xrange(X, Y):
       print('The number {} is in range ({}, {})'.format(num, X, Y))
   else:
      print('The number {} is not in range ({}, {})'.format(num, X, Y))

# Test Input
testInput = [X-1, X, X+1, Y+1, Y, Y-1]

for eachItem in testInput:
   checkRange(eachItem)

Here is the output that will you get after running the above program.

The number 999 is not in range (1000, 7000)
The number 1000 is in range (1000, 7000)
The number 1001 is in range (1000, 7000)
The number 7001 is not in range (1000, 7000)
The number 7000 is not in range (1000, 7000)
The number 6999 is in range (1000, 7000)

The output of xrange() is almost identical to what range() gave us.

We hope that after wrapping up this tutorial, you should know how to check if an integer lies in between two numbers or not. However, you may practice more with examples to gain confidence.

Also, to learn Python from scratch to depth, do read our step-by-step Python tutorial.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article How to Merge Dictionaries in Python Merge Dictionaries in Python
Next Article Python float() function with examples Python Float() Explained

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x
x