Totalling & counting · Somatório e contagem
Totalling: a running total
- Totalling adds up a series of numbers.
- Start a
totalat 0, then add each value to it. - The pattern
total = total + valueis called an accumulator.
Somatório: total acumulativo
- Somatório soma uma série de números.
- Inicie um
totalem 0, depois adicione cada valor a ele. - O padrão
total = total + valueé chamado de acumulador.
nums = [10, 20, 30]
total = 0
for n in nums:
total = total + n
print(total) # 60
Building a running total · Construindo um total acumulado
Totalling — start at 0, then add each value as the loop visits it. · Somatório — comece em 0, depois some cada valor conforme o laço o visita.
Counting: a running count
- Counting tallies how many values meet a rule.
- Start a
countat 0, then add 1 each time the rule is true.
Contagem: contador acumulativo
- Contagem contabiliza quantos valores atendem a uma regra.
- Inicie um
countem 0, depois adicione 1 cada vez que a regra for verdadeira.
nums = [4, -2, 7, -5]
count = 0
for n in nums:
if n > 0:
count = count + 1
print(count) # 2
Average = total ÷ count
- An average is the total divided by how many values there are.
len(nums)gives the number of items in a list.
Média = total ÷ contagem
- Uma média é o total dividido pela quantidade de valores.
len(nums)retorna a quantidade de itens em uma lista.
nums = [2, 4, 6]
average = sum(nums) / len(nums)
print(average) # 4.0
In the exam: standard methods
- Totalling and counting are named standard methods in the syllabus.
Na prova: métodos padrão
- Somatório e contagem são chamados métodos padrão no currículo.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + Number[I]
NEXT I
Common mistakes
- Set the total to
0before the loop, then add to it inside. - A counter starts at
0and goes up by1each time. - Do not reset the total inside the loop.
Erros comuns
- Defina o total como
0antes do laço, depois adicione dentro dele. - Um contador começa em
0e aumenta em1cada vez. - Não reinsicie o total dentro do laço.
Now you try
- Use a loop with a running total or count.
- Press Check answer to test your code.
Agora você tenta
- Use um laço com total ou contagem acumulativa.
- Clique em Check answer para testar seu código.
The list nums is set for you. Use a loop to add up every number and store the result in total (the answer is 18). · A lista nums está definida para você. Use um laço para somar todos os números e armazene o resultado em total (a resposta é 18).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Count how many numbers in nums are greater than 0. Store the count in count (the answer is 2). · Conte quantos números em nums são maiores que 0. Armazene a contagem em count (a resposta é 2).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Work out the average of the numbers in nums (the total divided by how many there are) and store it in average (the answer is 4.0). · Calcule a média dos números em nums (o total dividido pela quantidade) e armazene-a em average (a resposta é 4.0).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.