How to play a list that skips the numbers according to the number typed

Asked

Viewed 52 times

1

Preferably something that explains the answer below (without the spaces)

0

1

4

9

16

  • 1

    This corresponds to the n² series, easy to do in python with the operator ** exponentiating

2 answers

4


You can use list comprehension for this question, much more idiomatic:

[ i**2 for i in range(5) ]

2

If you want something that generates this result that you showed in the question, just do it:

i = 0
lista=[]

while i < 5:
   lista.append((i ** 2)) 
   i += 1

Output:

[0, 1, 4, 9, 16]

and so on.

  • But and to finish at 16?

  • @Lucassouza I made the change in the reply, look there

Browser other questions tagged

You are not signed in. Login or sign up in order to post.