Python Check Integer Number in Range Using Multiple Ways – My programming school

This tutorial offers you a number of strategies to verify if an integer quantity lies in the given variable or not. It consists of a number of examples to convey readability.

Let’s first outline the issue. We wish to confirm whether or not an integer worth lies between two different numbers, for instance, 1000 and 7000:

So, we’d like an easy technique that may inform us about any numeric worth if it belongs to a given variable. Hence, in this put up, we’ll describe 3 ways of fixing this drawback. You can select which of those fits you perfectly.

Two of those strategies work in Python 3, and the third one is particularly for Python 2.7.

Python Check Integer Numbers in Range or Between Two Numbers

Let’s now open up all of the 3 ways to verify if the integer quantity is in vary or not.

Using the Python comparability operator

In Python programming, we will use comparability operators to verify whether or not worth is larger or lower than the opposite. And then, we will take some motions based mostly on the consequence.

The put-up outlines the built-in comparability operators obtainable in Python.

Python Operators

let’s now try a pattern code to see learn how to use the comparability operators.

"""
Desc:
Python program to verify if the integer quantity is in between a spread
"""

# Given vary
X = 1000
Y = 7000

def verifyRange(num):
   # utilizing comaparision operator
   if X <= num <= Y:
       print('The quantity  is in vary (, )'.format(num, X, Y))
   else:
      print('The quantity  is not in vary (, )'.format(num, X, Y))

# Test Input List
take a look atInput = [X-1, X, X+1, Y+1, Y, Y-1]

for everyItem in take a look atInput:
   verifyRange(everyItem)

We’ve written a perform to verify whether or not the entered integer quantity is in the given variable or not. It is utilizing the next comparability operator syntax:

if X <= num <= Y:

Also, we’ve read the take a look at knowledge in an inventory accordingly. We want to take a look at all +ve and edge instances as properly. Hence, we made take a look at entering the checklist, as proven under:

# Test Input List
take a look atInput = [X-1, X, X+1, Y+1, Y, Y-1]

This checklist helps us run some common take a look at instances, and higher and decrease boundary assessments. So, after we run the above program, it will get us the next consequence:

The quantity 999 is not in vary (1000, 7000)
The quantity 1000 is in vary (1000, 7000)
The quantity 1001 is in vary (1000, 7000)
The quantity 7001 is not in vary (1000, 7000)
The quantity 7000 is in vary (1000, 7000) # We've included higher vary as properly
The quantity 6999 is in vary (1000, 7000)

Python vary() to verify between two integer quantity

We can even use Python vary perform that does this job for us. It can fairly simply establish if the integer quantity lies between two numbers or not.

Please verify the next instance:

"""
Desc:
Python vary to verify if the integer is in between two numbers
"""

# Given vary
X = 1000
Y = 7000

def verifyRange(num):
   # utilizing comaparision operator
   if num in vary(X, Y):
       print('The quantity  is in vary (, )'.format(num, X, Y))
   else:
      print('The quantity  is not in vary (, )'.format(num, X, Y))

# Test Input
take a look atInput = [X-1, X, X+1, Y+1, Y, Y-1]

for everyItem in take a look atInput:
   verifyRange(everyItem)

Here, we’ve referred to the vary() perform which consists of the decrease vary (X) but discards the sting worth, i.e., Y.

Hence, while you execute the above code, it outcomes in the under output:

The quantity 999 is not in vary (1000, 7000)
The quantity 1000 is in vary (1000, 7000)
The quantity 1001 is in vary (1000, 7000)
The quantity 7001 is not in vary (1000, 7000)
The quantity 7000 is not in vary (1000, 7000) # Python vary does not embody higher vary worth
The quantity 6999 is in vary (1000, 7000)

Python xrange() to verify between two integer numbers

This technique (xrange()) would solely work in Python 2.7 or under. But since Python 2.7 is nonetheless in use, so we’re giving an instance of the identical.

Please see the under coding snippet utilizing the Python xrange perform:

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

# Given vary
X = 1000
Y = 7000

def verifyRange(num):
   # utilizing comaparision operator
   if num in xrange(X, Y):
       print('The quantity  is in vary (, )'.format(num, X, Y))
   else:
      print('The quantity  is not in vary (, )'.format(num, X, Y))

# Test Input
take a look atInput = [X-1, X, X+1, Y+1, Y, Y-1]

for everyItem in take a look atInput:
   verifyRange(everyItem)

Here is the output that can you get after working on the above program.

The quantity 999 is not in vary (1000, 7000)
The quantity 1000 is in vary (1000, 7000)
The quantity 1001 is in vary (1000, 7000)
The quantity 7001 is not in vary (1000, 7000)
The quantity 7000 is not in vary (1000, 7000)
The quantity 6999 is in vary (1000, 7000)

The output of xrange() is virtually similar to what vary() gave us.

We hope that after wrapping up this tutorial, it’s best to know to learn how to verify if an integer quantity lies in between two numbers or not. However, it’s possible you’ll follow more with examples to achieve confidence.

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

For more, you can refer to Wikipedia

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment