Testing whether a number is nearly a multiple of 10 I'm going through a basic python exercise, stumbled upon this query. 


  Given a non-negative number "num", return True if num is within 2 of a multiple of 10. 


Here's the my solution:

def near_ten(num):
  return (num % 10) in range(8, 10) or (num % 10) in range(0, 3)


It shows "All Correct", but I'm feeling like my code is bit digressive as I have hard-coded the "numbers within 2 of a multiple of 10".

Can one please review this line if its the right approach?
I would suggest you to go further in  hard coding those values:
I mean I would write:

def is_near_ten(num):
   return (num % 10) in [0, 1, 2, 8, 9]


You can see visually the difference (less instructions) and you can confirm that:

&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit('(128 % 10) in [0, 1, 2, 8, 9]')
0.23481132300003082
&gt;&gt;&gt; timeit.timeit('(128 % 10) in range(8, 10) or (128 % 10) in range(0, 3)')
1.3901589560000502


You can notice that further hard coding those values as suggested makes your function more than 5 times faster.