To have the first multiples of 3, just create a list with the result of 3 * 1
, 3 * 2
, 3 * 3
, etc., until 3 * N
.
Then it is enough that the range
go from 1 to N, and you enter the result of the multiplication of 3 by each element of the range
:
n = 5 # quantidade de múltiplos
multiplos = []
for i in range(1, n + 1):
multiplos.append(3 * i)
print(multiplos)
Remembering that in a range
the final value is not included, so I put n + 1
, so that he can go to the umpteenth multiple.
Another option is to use one comprehensilist on, much more succinct and pythonic:
n = 5 # quantidade de múltiplos
multiplos = [ 3 * i for i in range(1, n + 1) ]
print(multiplos)
Or, using map
to map each value of the range
for your triple:
n = 5 # quantidade de múltiplos
multiplos = list(map(lambda i: i * 3, range(1, n + 1)))
print(multiplos)
Finally, you can also generate the range
with the multiples themselves. Just start at 3 (which is the first multiple), end at 3 * N
(the umpteenth multiple), and skip 3 by 3 (to always get the next multiple). Then just get the list directly from it:
n = 5 # quantidade de múltiplos
multiplos = list(range(3, (n * 3) + 1, 3))
# ^ ^^^^^^^^^^ ^
# | | |
# | | \__ passo (pular de 3 em 3)
# | |
# valor inicial __/ \__ valor final
print(multiplos)
It wouldn’t just be something like
lista = [3*1, 3*2, 3*3, 3*4, 3*5]
?– Woss