Lists (1-D arrays) · Listas (arrays 1-D)
A list holds many values
- A list keeps many values in one variable.
- Write the items inside square brackets:
[10, 20, 30]. - The items can be numbers, strings, or a mix.
Uma lista guarda muitos valores
- Uma lista guarda muitos valores em uma única variável.
- Escreva os itens entre colchetes:
[10, 20, 30]. - Os itens podem ser números, strings ou uma mistura.
Get and change items
- Use an index, just like a string:
nums[0]is the first item. nums[-1]is the last item.- You can also change an item:
nums[0] = 99.
Obter e alterar itens
- Use um índice, assim como em uma string:
nums[0]é o primeiro item. nums[-1]é o último item.- Você também pode alterar um item:
nums[0] = 99.
fruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
Add items and count them
nums.append(x)addsxto the end of the list.len(nums)tells you how many items there are.- A list can grow as your program runs.
Adicionar itens e contá-los
nums.append(x)adicionaxao final da lista.len(nums)diz quantos itens existem.- Uma lista pode crescer conforme seu programa é executado.
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits))
print(fruits)
Loop over a list
for item in nums:visits each item in turn.- The loop variable holds one item per pass.
- This is the easy way to process every value.
Iterar sobre uma lista
for item in nums:visita cada item em sequência.- A variável de loop armazena um item por passagem.
- Esta é a forma fácil de processar cada valor.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In Cambridge pseudocode
- A list is the exam's 1-D array. Note: exam arrays often start at index
1, but Python lists start at0.
Em pseudocódigo do Cambridge
- Uma lista é o array 1-D da prova. Nota: arrays da prova geralmente começam no índice
1, mas listas do Python começam em0.
DECLARE Scores : ARRAY[1:3] OF INTEGER
Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
FOR I ← 1 TO 3
OUTPUT Scores[I]
NEXT I
Common mistakes
- The first item is
a[0]and the last isa[-1];a[len(a)]is out of range. b = adoes not copy the list — both names share it. Usea.copy()to make a real copy.appendadds one item to the end.
Erros comuns
- O primeiro item é
a[0]e o último éa[-1];a[len(a)]está fora do intervalo. b = anão copia a lista — ambos os nomes a compartilham. Usea.copy()para fazer uma cópia real.appendadiciona um item ao final.
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
Agora você tenta
- Construa ou itere sobre uma lista em cada tarefa.
- Clique em Check answer para testar seu código.
A list changes in place · Uma lista muda no local
A list is ordered and indexed; append/change/pop all modify it. · Uma lista é ordenada e indexada; append/change/pop modificam tudo ela.
Create a list colors holding "red", "green", "blue". Then append "yellow" so the list has four items. · Crie uma lista colors contendo "red", "green", "blue". Depois append "yellow" para que a lista tenha quatro itens.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Loop over the list nums and add up its values. Store the total in total (the answer is 66). · Passe pelo laço sobre a lista nums e some seus valores. Armazene o total em total (a resposta é 66).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Find the largest value in nums with a loop. Start largest at the first item, then loop through nums and keep the bigger value. Store the answer in largest (it is 23). · Encontre o valor maior em nums com um laço. Inicie largest no primeiro item, depois passe pelo laço em nums e mantenha o valor maior. Armazene a resposta em largest (é 23).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Build a new list backwards with the items of nums in reverse order. Use a loop, and do not change nums itself. · Crie uma lista nova backwards com os itens de nums em ordem reversa. Use um loop e não modifique nums.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.