So i have learned more about the difference between if not and != but still am confused in my code.
In this case I am going to find the middle 2 numbers in a list and get the average or if the list is odd just get the middle number.
def median(numlist): numlist = sorted(numlist) half = len(numlist) // 2 if not len(numlist) % 2: return (numlist[half] + numlist[half - 1]) / 2.0 else: return numlist[half]
in the line :
if not len(numlist) % 2:
this one is the correct form and when I do
median([4, 5, 5, 4])
I get 4.5, correct answer.
Why if I use:
if len(numlist) % 2 !=0 :
do i get 5 instead?
Am I correct that —if not len(numlist) % 2:— is saying if the length of numlist is not even…? As in a answer of 0 means true and something else means false? I am missing something on the short hand of some coding I have recently realized…