My teacher gave me an exercise during the computing lesson in week 2:
Write a Python program to create and print a list where the values are first N square of numbers. You are REQUIRED to implement a function named
printSquare()to take n where n is an positive integer.
I'mm trying to writing a function called printSquare().
for example, the expected output of printSquare(5) is [1, 4, 9, 16, 25].
def getList(num):
list=[]
for i in range(int(num)):
list.append(i)
return list
def printSquare(num):
wholeList = list(getList(num))
wholeList.pop(0)
wholeList.append(num)
tmp=[]
for i in wholeList:
x = wholeList[i]**2
tmp.append(x)
return tmp
printSquare(5)
I'm struggling in the following part, I don't know why the tmp.append(x) doesn't work.
for i in wholeList:
x = wholeList[i]**2
tmp.append(x)
return tmp
The second question is that is there any faster way to write this code.