R
R
Richard Kamsky2019-02-05 22:44:51
Python
Richard Kamsky, 2019-02-05 22:44:51

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

The book explains it this way:
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]

Help me figure this out. But for me it's kind of dark.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2019-02-05
@Richard_Kamsky

I'll explain everything to you now.
If you have read to a place that you poorly understand, flip the book back to the page where you understood everything strongly. And read again from this page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question