Look for repeated patterns or phrases and cache, rather than repeat.
Avoid using the names of built-in functions, even if it doesn’t hurt anything, just so you don’t get in the habit.
s = sorted(x)
n = len(s)
We do not need a special case for length of 1, that will fall under ‘odd’. Consider what the middle index will be…
m = int(n / 2)
Now when we divide
n by 2, we get the index of the median for any odd length sample.
With those three pieces of information we have enough to proceed with our return values.
if n % 2: # will be True for odd
return float(s[m - 1] + s[m]) / 2
Above, and earlier, we used explicit type casting so the reader has no question in their mind what we are after. Notice that only the numerator above is cast to a float, and we leave the counting number in the denominator untouched.