Python: Test whether a number is within 100 of 1000 or 2000
Python Basic: Exercise-17 with Solution
Write a Python program to test whether a number is within 100 of 1000 or 2000.
Python abs(x) function:
The function returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.
def near_thousand(n): return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100)) print(near_thousand(1000)) print(near_thousand(900)) print(near_thousand(800)) print(near_thousand(2200))
True True False False
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference.
Next: Write a Python program to calculate the sum of three given numbers, if the values are equal then return thrice of their sum.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
try: foo() except Exception: print("Exception occured") else: print("Exception didnt occur") finally: print("Always gets here")
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework