Python - if in range(), if not in range() - Examples
Python ¨C if in Range, if not in Range
Python IF Number in Range
You can check if a number is present or not present in a Python range() object.
To check if given number is in a range, use Python if statement with in keyword as shown below.
if number in range(start, stop[, step]):
statement(s)
number in range() expression returns a boolean value: True if number is present in the range(), False if
number is not present in the range.
You can also check the other way around using not to the existing syntax.
if number not in range(start, stop[, step]):
statement(s)
Now the expression: number not in range() returns True if the number is not present in the range, and False
if number is present in the range.
Example 1 ¨C If Number in range()
In this example, we will create a range object that represents some sequence of numbers, and check if a given
number is present in the range or not.
Python Program
range_1 = range(2, 20, 3)
number = int(input('Enter a number : '))
if number in range_1 :
print(number, 'is present in the range.')
else :
print(number, 'is not present in the range.')
Output
D:\workspace\python> python example.py
Enter a number : 4
4 is not present in the range.
D:\workspace\python> python example.py
Enter a number : 5
5 is present in the range.
D:\workspace\python> python example.py
Enter a number : 11
11 is present in the range.
D:\workspace\python> python example.py
Enter a number : 12
12 is not present in the range.
Example 2 ¨C If Number not in range()
You can also check if the number is not in range.
In this example, we will create a range object that represents some sequence of numbers, and check if a given
number is not present in the range.
Python Program
range_1 = range(2, 20, 3)
number = int(input('Enter a number : '))
if number not in range_1 :
print(number, 'is not present in the range.')
else :
print(number, 'is present in the range.')
Output
D:\workspace\python> python example.py
Enter a number : 4
4 is not present in the range.
D:\workspace\python> python example.py
Enter a number : 5
5 is present in the range.
D:\workspace\python> python example.py
Enter a number : 11
11 is present in the range.
D:\workspace\python> python example.py
Enter a number : 12
12 is not present in the range.
Conclusion
Concluding this Python Tutorial, we learned how to check if a given number is present in the range() or not
using if in range and if not in range expressions.
Python Programming
?
Python Tutorial
?
Install Python
?
Install Anaconda Python
?
Python HelloWorld Program
?
Python Variables
?
Python Variable Data Type Conversion
?
Python Comments
Control Statements
?
Python If
?
Python If Else
?
Python While Loop
?
Python For Loop
Python String
?
Python String Methods
?
Python String Length
?
Python String Replace
?
Python Split String
?
Python Count Occurrences of Sub-String
?
Python Sort List of Strings
Functions
?
Python Functions
Python Collections
?
Python List
?
Python Dictionary
Advanced
?
Python Multithreading
Useful Resources
?
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- ppyytthhoonn mmoocckk tteesstt iiiiii tutorialspoint
- programs list for class xii cs
- recursion programs com
- exploring data using python 3 charles r severance etsu
- tries and string matching stanford university
- using python for computing on elliptic curves very preliminary draft
- programs on string
- python if in range if not in range examples
- real python python 3 cheat sheet
- s python cheat sheet data science free
Related searches
- python for in range list
- python mkdir if not exist
- python make directory if not exist
- python make dir if not exist
- python mkdir if not exists
- python if file not exist
- python if not int
- python if not in
- python if not in dictionary
- python create directory if not exist
- python if not in list
- python if in list statement