# Middle Item

i have notebook where i am tying to write down everything what i have learnt and there is not nothing about int can, can anyone step by step explain me all!

``````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]))

``````

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! Welcome @giosurguladze2525007 to the forums! The `int()` function turns a string or a float into an integer. In python an integer is a number with no decimal point or speech marks(’’) around it. An integer in python would look like this:

``````this_is_an_integer = 1
``````

I hope this helps!

1 Like

Thank you very much!

can you explain my this calculation sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]
in the code

as i guess if len(lst) % 2 == 0 is (6/2=3 ==0) that is not correct yes
then is variable by sum and here i stuck

``sum = lst[int(len(lst)/2)] + lst[int(len(lst)/2) - 1]``

This means the variable `sum` is equal to the item at the list index(point in the list) `int(len(lst)/2)` + the item of the list at the point `lst[int(len(lst)/2) - 1]`.
In other words, you code finds two points in a list `lst` and adds them. You can tell this due to the square brackets `[]`, which in Python denote the list index of a list.

Within those square brackets, is the list index.

The `int()` function makes sure the list index is not a decimal.

It then finds the length of the list `lst`, with the `len()` function, which returns a number.

It then divides that by two.

The second part, after the + sign does the same thing, except after it does the above calculation, it subtracts 1. Then, the code picks those two elements of the list, and adds them. Consider the code below:

``````lst = [1, 2, 3, 4, 5, 6, 7, 8]
variable1= lst[len(lst) / 2] #this would return 5, as that is the element of the list at the index 5.
lst = [1, 2, 3]
variable1 = lst[len(lst) / 2] #This would try to return the element of the list at 1.5, which does not exist. This is why you use the int() function. It makes sure the calculation lst[int(len(lst) / 2)] returns a  number that is not a decimal. In this case, it would return 3.
#So, put it all together, in the following code:
lst = [1, 2, 3]
sum = lst[int(len(lst) / 2)] + lst[int(len(lst) / 2) - 1]
#What would this return?
``````

I really hopes this helps! Sorry if I have worded it badly!

1 Like

P.S. In future, if you post code, to make it look fancy, click the button at the top of the post thing that looks like this: </>
Here is a screenshot:

When you click the button, it comes up with this: Type over the text to get properly formatted code! I hope this helped as well!
Happy coding!

1 Like

If you use the `int()` function, you do not need the `if...else` statement. Sorry, I didn’t fully read your reply!

can you please tell me am i right ?
look in the lst there is 8 objects and and it would be 8 divided by 2 is 4 and why its 5 ?
sorry for but i am trying to catch a logic thank you very much again you made my day !

It is five because the list index of 4 `lst` returns the fifth item of the list, because list indexing starts from zero. i.e. `lst=` `1` is at the zeroth place in the list.
In other words, to get `lst=` to return `1`, you would have to write `return lst`.

This means that the 5 in `lst=[1, 2, 3, 4, 5, 6, 7, 8]` is at the ‘fourth’ position.

(1 is at 0, 2 is at 1, 3 is at 2, 4 is at 3 and 5 is at 4.)
Consider the following code: If you want, here is the link to the CodeCademy excercise on lists:
This interactive lesson covers all you need to know on list indexes.
Happy Coding!

actually, 0 is the first, not zeroth, and that makes 4 the fifth index

blame english.

this might make more sense if you used zoo animals instead of numbers as indices.

you wouldn’t say the tigerth value. you’d say third. and numbering them does not have the effect of re-ordering them, so it would remain the third, regardless of how you numbered them.

and 0 being the first makes plenty of sense, for example, what is the first amount of potatoes you have? zero, that’s what you have before you get one

you are right the count begins at 0 and

it would be 9 objects sum = lst[int(len(lst)/2)] |||= 7 / 2 = 3.5 in python it is 4 ||| second st[int(len(lst)/2) - 1] = 7 / 2 = 3.5 - 1 is 2.5 that in python is 3 and total would be 7 its done with this okay next

len(lst) would be 7 / 2 is 3.5 and == is true but it does not true and way i get -7.0 ? this is confusing me

You get 7.0 because when you use a modulo `%` operator, it returns a float(or a decimal number).

``````>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> len(a)
7
>>> type(len(a))
<class 'int'>
>>> len(a) % 2
1
>>> type(len(a) % 2)
<class 'int'>
>>> len(a) % 2 == 0
False
``````

There should be no floats anywhere, everything here is done with integers (whole numbers)

For division, use `//`

``````>>> 7 // 2
3
``````

there should be no conversions to int using `int( ... )` because everything should already be and remain int

1 Like

If the length of the list `lst` is 7, the the code `if len(lst) % 2 == 0` would return false because 7 divided by 2 gives a remainder of one. (7/2 = 3 remainder 1). Therefore your code would run the `else` code. I hope this helps!