What do the extra brackets do? | Sololearn: Learn to code for FREE!

+5

# What do the extra brackets do?

What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) Why does it print 8?

2/22/2020 12:52:30 AM

Oksana

+10

list[4] = 5 list[list[4]] = list[5] = 8

+6

Solve it from the inside out. list[4] is the element at index position 4, which is 5. So list[list[4]] is the same as list[5] list[5] is the element at index position 5, which is 8. ðŸ˜Š

+5

list[4] accesses the fifth element of list, which is 5. list[list[4]] hence accesses list[5], which is the sixth element of list, 8.

+5

Three answers for the price of one ðŸ˜„

+4

list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) 1. list's index start from 0 then 1 then 2... 2. ' list[4] ' will calculated first list[4] = 5 3. then list[ list[4] ] = list[5] and, list[5] = 8 thats why it prints 8

+2

Good question,,,, list[0] means the first value of the array. So, list[4] is the 5th value of the array means 5; So, list[list[4]]->list[5] means the 6th value of the array means 8; So, the answer is 8;

+1

list[3] is the element at index position 3, the quarter position, because in programming you start with the 0. The first element would be list[0] and list[1] would be the second element. exmple: list = [h, e, l, l, o] print(list[list[4]]) output: the O of the list

0

I hate questions like this... like a trick question or something? is there a real world example where you might use this other than to just try to confuse someone looking at the code? ðŸ˜³

0

Thank you everyone!!! I get it now!

-1