Middle Item: question solution

Doing the “Middle Item” challenge, I was shown the following solution:

#Write your function here
def middle_element(lst):
  if len(lst) % 2 == 0:
    sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]
    return sum / 2
  else:
    return lst[int(len(lst)/2)]

#Uncomment the line below when your function is done
print(middle_element([5, 2, -10, -4, 4, 5]))

I am looking for an explanation for this part:

else:
    return lst[int(len(lst)/2)]

If the length of the list is and odd number how is this handled? Example with a length of ‘5’.
This is ‘5/2 = 2.5’
How does it know what the correct index number is? It is also not a division with ‘//’ that it would end up with an integer.

Exercise link (exercise 5):
https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/articles/advanced-python-code-challenges-lists

because int() converts to integer. Which will essentially floor/round down the number to an integer.

Thank you so much for the quick response; it also makes sense and I understand it now :-).

I am not sure whether I came across the usage of the int() function (yet?) in the “computer science” course - or maybe I forgot - hence I did not know…

Thanks!

If you run into functions you don’t remember, you can always consult documentation. simple google something like python int()