- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python β Product of squares in list
In the current world, the Python language is one of the most popular ones and it allows the user flexible accessibility and usability. The Python language has data structures namely the list, dictionary, tuple, and string. In this article, we are going to deal with the list data structure in which the type of data can be chosen among integer, float and string. When the lists are assigned some values, they cannot be changed. In lists, the elements are ordered in a sequence and are a one-dimensional array.
Product of Squares in list
The lists are composed of an element that allows the programmers to store the data of different objects. The simple syntax to hold the elements of different data types is given below,
list1 = [1, 2, 'Welcome', β45β]
The above list data structure holds the value 1 in the first place of the array, the second place holds value 2, the third place holds the string data type of value βwelcomeβ and finally the fourth element has a value equal to 45.
The square number is nothing but a number when it multiplies by itself. The other way to represent it is by raising the value to 2. The basic syntax for the square number is,
Squarenum = X**2
The input taken for the below approaches is [9, -4, 8, -1] and let it be assigned to a variable named list1. So, the mathematical calculation involved in finding the product of a square number is given below.
List1 = (9)2 * (-4)2 * (8)2 * (-1)2 = 81 * 16 * 64 * 1 = 82944
The given numbers are squared first and later all the squared numbers are multiplied to get the product of squares. The same output is printed but using the Python functionalities in a very easier and quicker manner without any manual mistakes.
Approach
Approach 1 β Using the iteration method
Approach 2 β Using the functools module
Approach 1: Python Program to Perform product of square in list using the iteration method
The iteration method is used to iterate through each element in the list by calculating the square value and later multiplying it with the outcome. The below code holds the time complexity as 0(n).
Algorithm
Step 1 β The input with a list of integer data objects is initialized with both positive and negative signs as [9, -4, 8, -1].
Step 2 β The outcome is initialized to 1 because the product needs to be multiplied by 1 to get the value
Step 3 β When the outcome is initialized to 0, then it returns a 0 value.
Step 4 β The for loop is used to iterate through the given list and then calculate the square of the number.
Step 5 β Then the outcome with a value of β1β is multiplied by the square number.
Step 6 β Finally taking the product of the squared numbers, the result is printed.
Example
#initializing the list with integer elements list1 = [9,-4,8,-1] #declaring the outcome variable as 1 to get the product value outcome = 1 #iterating through the list using for loop for num in list1: #the syntax to calculate the square number sq =num**2 #Later the square number is multiplied by the outcome outcome*=sq #Printing the final result print("The result after the multiplying the number is: ", outcome)
Output
The result after the multiplying the number is: 82944
Approach 2: Python Program to Perform product of squares in list using functools module
The lambda function is the one which can be used without defining the function. The time complexity of the below code is 0(n) and n represents the length of the list.
Algorithm
Step 1 β The functools library is imported to make use of the reduce method.
Step 2 β The list is initialized with four integer elements of varying symbols.
Step 3 β The outcome variable is declared to hold the result value.
Step 4 β The reduce function has two parameters a lambda function and a sublist holding the square value.
Step 5 β The result is printed.
Example
#reduce() function is imported from the functools module from functools import reduce #initialize the list data structure list1 = [9, -4, 8, -1] #reduce method with two parameters # first parameter is a lambda function # second parameter holds the iteration outcome = reduce(lambda a,b: a*b, [m**2 for m in list1]) #the print function will return the product of the squared number print("The result after the multiplying the number is: ", outcome)
Output
The result after the multiplying the number is: 82944
Conclusion
The mathematical steps for finding the product of the squared number in the given list are calculated with manual calculations is given and then provided with a simple code using the modules like functools and iteration methods. When working with the programming languages like Python, the user needs to grasp all the concepts behind lists and numerical calculations.