Write a python program to test whether a number is within 100 Of 1000 or 2000

Write a python program to test whether a number is within 100 of 1000 or 2000. is provided on this page.

Write A Python Program To Test Whether A Number Is Within 100 Of 1000 Or 2000.

The abs() function will return absolute value of n and then check if its less than 100. We check absolute value of two calculations, one is (1000 - number) and second is (2000 - number), take absolute of these two and check if they are less than 1000 and 2000 respectively.

If only one exression is true, its suffient.

Program 1


def near_thousand(n):
      return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100))



print(near_thousand(1100))
print(near_thousand(800))
print(near_thousand(900))   
print(near_thousand(1000))

Related Python Programs

How do you delete file
How do you rename file
Lambda functions cannot be pickled

Comments