Python function to create and print a list where the values are square of numbers between 1 and 20

Method 1:-

def printValues():
    l = list()
    for i in range(1,21):
        l.append(i**2)
    print(l)
 
printValues()
output:-
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
>>> 

Method 2:-

sqrt = [elem**2 for elem in range(1,20)] 
print(sqrt) 

output:-
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]
>>> 
Method 3:-
sqr_list = [] 
for i in range(1,20): 
  sqr_list.append(i*i) 
 
print(*sqr_list) 

output:-
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361
>>> 
tags:-

write a python function to create and print a list where the values are square of numbers


Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post