In this program, we will take a user input as a list and print the square of all numbers In the Python list with the help of the below-given steps.
Table of Contents
Method 1: Using for loop.
Square of all numbers in list:
Steps to solve the program
- Take a list as input.
- Using for loop and **n print the square of each element from the list.
list1 = [3, 5, 7, 1, 8]
for val in list1:
print(val, ":", val**2)
Output :
3 : 9
5 : 25
7 : 49
1 : 1
8 : 64
Method 2 : Using while loop.
Square of all numbers in list:
Steps to solve the program
- Take a list as input.
- Using while loop and necessary condition print the square of each element from the list.
#input list
list1 =[3,5,7,1,8]
#Creating count variable
count = 0
while count < len(list1):
print(list1[count],":",list1[count]**2)
count += 1
Output :
3 : 9
5 : 25
7 : 49
1 : 1
8 : 64
Related Articles
Python program to combine two lists.
Python program to calculate the sum of all elements from a list.
Python program to find a product of all elements from a given list.
Python program to find the minimum and maximum elements from the list.
Python program to differentiate even and odd elements from the given list.
Python program to remove all duplicate elements from the list.