Write a Python program to create and print a list where the values are first N square of numbers

Viewed 65

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.

5 Answers

If you look at the error message it states that IndexError: list index out of range meaning that the index isn't in the range of your list. What you could do is try to use the range function over the length of the list: for i in range(1, len(wholeList)), this ensures that the index is always within the range.

when you use for..in you are directly getting elements, not indexes. So to solve the problem write

for i in wholeList:
    x = i**2
    tmp.append(x)
return tmp

Also, you can write this way more clearly with list comprehensions (and it is a nice tool to have)

def get_list(n):
    return [i**2 for i in range(1, n+1)]

The i in the for loop is the value, not the index. You should either write x=i**2, or in the for loop you should specify it differently:

for i, value in enumerate(wholeList): 
    x = value**2
    tmp.append(x)

The tmp list now includes the square values of the wholeList. Also, you are not printing anything somewhere.

There are couple of mistakes and lack of understanding in the code. Let me try and explain mistakes and lack of understanding i found.

you are using range(num) which will give you a list from 0 to num-1. Hence you do wholeList.pop(0) to remove 0 from the list and wholeList.append(num) to then add the last num in.

Instead of that, you can do

range(1,int(num)+1) This will return you a list from 1 to number, solving both the issue, and not needing to pop and add extra number to list

secondly

for i in wholeList:
        x = wholeList[i]**2

That is not how looping over list works for i in wholeList:

Will return you the element of the list, not its index. Here you are assuming i is the index of elements in list. So wholeList[i]**2 is where your code fails, since you try to access elements at index position that does not exist.

If you want to get index position you have to do

for index, value in enumerate(wholeList):

This will return you the index, and the value associated with the index in the list.

I hope, all of that helped you understand things better. Here is a code that works

def getList(num):
    list=[]
    for i in range(1,int(num)+1):
        list.append(i)
    return list



def printSquare(num):
    wholeList = getList(num)
    tmp=[]
    for i in wholeList:
        tmp.append(i**2)
    return tmp

printSquare(5)

You have made this far more complicated than it really is. You can (should) use a list comprehension thus:

def printSquare(n):
    print([x*x for x in range(1, n+1)])

printSquare(5)

Output:

[1, 4, 9, 16, 25]
Related