I want to check if m in between l and h. I can write
if m<=h and m>=l
or
m in range(l,h+1)
The latter one looks better, but does it take more than O(1) time?
I want to check if m in between l and h. I can write
if m<=h and m>=l
or
m in range(l,h+1)
The latter one looks better, but does it take more than O(1) time?
O(1):
if m<=h and m>=l:
...
O(n) in Python 2, but O(1) in Python 3:
if m in range(l, m+1):
...
Better style:
if low <= m <= high:
...
Note: The behaviour is different if m can be a non-integer (consider a float, for example).
It's better to use the first one but you can rewrite it like it:
if 1<=m<=h
The function range
create a list and in
searches if m is in that list. It's worse in performance that two comparisons.
That only applies to python 2, in python 3 both ways are optimal.