Answer the question
In order to leave comments, you need to log in
How to make a list of squares?
Good afternoon. I am learning python from a book. I don't understand how this code works.
squares = [] #1
for value in range(1,11): #2
square = value**2 #3
squares.append(square) #4
print(squares) #5
First, an empty list called squares is created at point 1. At point 2, you tell Python to iterate over all values from 1 to 10 using the range() function.
In the loop, the current value is raised to the second power, and the result is stored
in the variable square at point 3. At point 4, each new value of square is appended to the squares list. Finally, after the loop ends, the list of squares
is output at point 5:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question