Reputation: 123
The below for loop doesn't have x**2 in its body which is generally Tab indented in the next line then how this program is able to generate output as given below:
>>> squares = [x**2 for x in range(10)]
Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
if so then how should i read this for loop?
Upvotes: 0
Views: 43
Reputation: 828
for x in range(10)
simply means counting from 0-9.
the list comprehension [x**2 for x in range(10)]
then takes every value and squares it, and saves it in the list.
Look at this link: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
Upvotes: 2