Write a function s(n) that will return a list of the first n squares.

def s(n): ans=[] #This variable stores the list we will return. for i in range(n): #This is a for loop that will iterate n times. ans.append((i+1)**2) #This adds the (i+1)th square to the list, because range() is 0-indexed. return ans

BM
Answered by Brodie M. Python tutor

1080 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

h


Write a simple Python program that asks a user to input their age and name and then return the text: "Hi my name is *NAME*, and I am *AGE* years old."


What is the difference between a for loop and a while loop.


Ask the user for a number and output the smallest divisor, bigger than one, for the inputted number. Output "Prime" if the number is a prime number.


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences