1. Problem to get the square of all numbers In the python list

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
  1. Take a list as input.
  2. 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
  1. Take a list as input.
  2. 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

Add elements from list1 to list2

Leave a Comment