Printing in list form


#1

numbers = [7, 9, 12, 54, 99]
for number in numbers:
print(number ** 2)

Above code print result in up down order but I want to print result in list form? Is it possible?


#2

yes, its possible. I could tell you how to achieve this, but then we waste a valuable learning opportunity. How do you think this could be achieved? Not in code, just try to describe it.


#3

numbers = [7, 9, 12, 54, 99]
for number in numbers:
print(numbers[number] **2)
I tried this but it show IndexError: list index out of range


#4

numbers = [7, 9, 12, 54, 99]
def numb(number):
for numbers in number:
return numbers ** 2
print (numb(numbers))
tried this as well ? :frowning:


#5

Why did you try those things? Why did you think they would work?


#6

Variable goes through each numbers inside list. So, I think printing (numbers[number] ** 2) will print numbers list with index variable squared.


#7

why do you want the index squared? This won’t work because number might have an higher value then that there are indexes, so you might get an index error

if you want indexes, use range() with length of list as argument


#8
for number in numbers:

#9

numbers = [7, 9, 12, 54, 99]
word= []
def lists_view(number):
for numb in number:
word.append(numb ** 2)
return word
Finally I did it :slight_smile: Sir.