While it may be possible to implicitly define a float by dividing by a float, it is not a recommended practice. Explicit declaration is better.
D.R.Y. means do not repeat yourself. The above code is repeated three times which suggests we could simplify the code by defining a variable with this value cached.
mid = list_length / 2
Length is an integer, and dividing by an integer yields an integer (in Python 2.x). Now consider that lists are zero-indexed, so dividing length by 2 will give a value that is the middle index of an odd length list, and the right hand index of the middle two values in an even length list.
9 / 2 == 4
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Notice there are four indices on either side?
8 / 2 == 4
[0, 1, 2, 3, 4, 5, 6, 7]
Notice that there are three indices on the right side, and four on the left side. This means that the left side middle index is 4 minus 1, not plus.
The variabale does not need to be initialized since your code assigns a value which replaces the initial value.
Since you are testing the length directly in the if statement, the above line may be replaced with the mid statement given earlier in this post.
1 is an odd length, so the mid will be index 0. The above line is not needed.
No. The middle index is an integer. The value at that index may be anything.
3 / 2 == 1
in Python 2.x. The median is 3, given the list, [1, 3, 5] since it is the value at index 1 of the sorted list.
Python 3.x requires explicit declaration of integers, opposite of 2.x, which requires (so to speak) explicit declaration of floats. We can get in the habit of writing more explicitly even while the interpreter is 2.x. That way we can write in the 3.x paradigm using 2.x models.
int( 3 / 2 )
This method call supercedes any that are built into the environment. We explicitly want an integer, plain and simple. Self documentation +1.