Doubt about lists, I can't undertand. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Doubt about lists, I can't undertand.

I'm stuck on the explanation about lists, seems little confused to me. Here the example: number = 3 things = ["string", 0, [1, 2, number], 4.56] print(things[1]) print(things[2]) print(things[2][2]) sorry my bad english

15th Nov 2016, 9:05 PM
Lonecoder Phantom Nightlies
Lonecoder Phantom Nightlies - avatar
4 Answers
+ 1
From your name, I assume you're Brazilian. If you're not, tell me and I write it in English later. XD Seguinte, a saída é 0, [ 1, 2, 3 ] 3 porque: o primeiro print(things[1]) refere-se ao segundo elemento da lista (note que o primeiro elemento é things[0], que guarda "string") então, print(things[1]) vai lhe mostrar 0; o segundo print(things[2]) refere-se ao terceiro elemento da lista, que é outra lista( aqui ele vai te mostrar toda a lista guardada no terceiro elemento da lista things, ou seja [ 1, 2, 3 ]); o terceiro print(things[2][2]) refere-se ao terceiro elemento da lista guardada no terceiro elemento da lista things. Esse terceiro elemento é a variável number, que à qual, no início do código, foi atribuído o valor 3. Lembrando que o acesso aos elementos da lista começa pelo índice 0 things[0] = 1º elemento. things[1] = 2º elemento. assim por diante. Sacou?
15th Nov 2016, 11:52 PM
Vinicius Matté Gregory
Vinicius Matté Gregory - avatar
0
my doubt is about output, why and how the output is 0,[1,2,3],3?
15th Nov 2016, 9:23 PM
Lonecoder Phantom Nightlies
Lonecoder Phantom Nightlies - avatar
0
Obrigado, era essa parte que não entendia (things[2][2]) o "segundo" [2] era o terceiro elemento dentro do "primeiro" [2]. Agora faz sentido.
16th Nov 2016, 12:40 AM
Lonecoder Phantom Nightlies
Lonecoder Phantom Nightlies - avatar
0
cronoik don't worry about this, you can speak english if you want
16th Nov 2016, 1:30 AM
Lonecoder Phantom Nightlies
Lonecoder Phantom Nightlies - avatar