I need some advice on how to write a Python program where it gives you a list of the first n perfect squares in list format. The output should look like this:
How many squares?: 5 [1, 4, 9, 16, 25]
This is what I have so far:
n = int(raw_input("How many squares? "))
Now for the next part I need to create a list of the first n squares. Any suggestions on how? Thank you for your time and advice.
code:
where n is user input for number of squares he wants.
If
n=5
then the output looks like:Use a list comprehension:
or
Or with map and a lambda function
if you want it starting at 1
Somebody mentioned generators - this is how you use them in this case:
This is something you can be helped with. For the other part, post your algorithm.
Use range to generate a list:
Use list comprehension to get list of x^2
A more elegant answer is provided by Novikov