Write a Python Function to Create And Print a List Where The Values Are Square of Numbers

Given the list  of numbers as an input to the Python  function,  your task  used to create a new list out of the older list by squaring all the numbers present in the older list.  the new list should store the squares of all the numbers of the older list.

Let's use the list comprehension technique for this problem.

 

 

Write a Python Function to Create And Print a List Where The Values Are Square of Numbers

 

 


Write a Python Function to Create And Print a List Where The Values Are Square of Numbers

Let's define a function gets_squares() and pass the  input argument to this function as a list containing the numbers.  Using the list comprehension  the method iterates all the numbers present in the list and multiplies the number by the number itself; this will be the square of the number and also a new list will be created automatically.

Now using the for loop, iterate all the items present in the list of squares and use the print function call to print all the items that are the numbers present in the list as shown below.

def get_squares(data):
   squares = [x * x for x in data]
   print("The square of numbers are ")
   for item in squares:
       print(item)





In the main program take a variable and to store the list length,  ask user to enter the list size for length using the input function call and then convert it into an integer using int() type conversion

Take an empty list of numbers set it to empty using the for loop to iterate all the the range from 0 to n(n is list length).  keep asking the user to enter the list  of numbers until and using the input function call convert it into an integer and append to the list of numbers.  using the inbuilt function append().



n = int(input("Enter List Size : "))
numbers = []
for _ in range(n):
   temp = int(input("Enter Numbers in a List : "))
   numbers.append(temp)





Once the for loop is completed we have the list of numbers as an input to the Python program and now call the above function to get the squares of the list of numbers. Function will also print the squares of numbers,  you can also modify it to return the list of square instead just using Return function call.
 
get_squares(numbers)





Python Code
==================

def get_squares(data):
   squares = [x * x for x in data]
   print("The square of numbers are ")
   for item in squares:
       print(item)


n = int(input("Enter List Size : "))
numbers = []
for _ in range(n):
   temp = int(input("Enter Numbers in a List : "))
   numbers.append(temp)


get_squares(numbers)





OUTPUT
=====================

>python main.py
Enter List Size : 5
Enter Numbers in a List : 1
Enter Numbers in a List : 2
Enter Numbers in a List : 3
Enter Numbers in a List : 4
Enter Numbers in a List : 5

The square of numbers are
1
4
9
16
25

 

 

 

OUTPUT 2
=====================
>python main.py
Enter List Size : 12
Enter Numbers in a List : 11
Enter Numbers in a List : 34
Enter Numbers in a List : 43
Enter Numbers in a List : 89
Enter Numbers in a List : 100
Enter Numbers in a List : 23
Enter Numbers in a List : 88
Enter Numbers in a List : 56
Enter Numbers in a List : 51
Enter Numbers in a List : 58
Enter Numbers in a List : 83
Enter Numbers in a List : 91
The square of numbers are
121
1156
1849
7921
10000
529
7744
3136
2601
3364
6889
8281





Conclusion
==========================

Execute the above program by providing different list length and random values as an input to the program.  comment down below if you have any suggestions are queries related to the about program






Post a Comment

0 Comments