Reduce() seems to be a Python method that performs the folding or compression of the mathematical approach. When you’re about to implement a method over an iterable and limit it to a singular cumulative value, reduce() comes in handy. Python’s reduce() method is famous among functional programmers, although Python has many more to offer. You’ll learn how to use reduce() functions and how to implement them efficiently in this article.
Installation of Python
Before we indulge in the learning of reducing methods in python via the Ubuntu Linux system, we need to have python installed and configured on our Linux system. So to start the learning, we should log in first from the login screen of the Ubuntu system. After the login, launch your command-line shell by activity area or using the shortcut “Ctrl+Alt+T” key. You have to check whether your system has python installed already or not. And, for this purpose, try out the query below. You can see it is now showing the latest version. For that, we have to check its version that has already been installed through the below command.
Now, you need to update the apt package that has already been mounted on your Linux system to update python again. For updation of the apt package, use the stated-below query in the shell.
After the updation of the apt package, you need to download the additional packages via the ppa package throughout the below query needed for additional python updates.
Tap on the Enter key to continue the installation.
After downloading additional packages and updates, we will mount the new version of the Python repository in the Ubuntu System. Hence, we have been trying the beneath query in the shell as follows:
Tap “Y” to carry on the procedure of setting up, or else knock out the key “n” to end it. Now the updated version of Python has been sufficiently configured on your system. Now, another package should be installed and configured on your system to use python on it. This package is named “pip.” This is necessary to be installed before the usage of python. As the pip package is not installed, we will be installing it via curl command. Therefore, install the curl package first as:
After the installation of curl, download the package of pip via:
You can have a look at the list of all the repositories beginning with “get.”
Using the sudo command with the keyword “python3.9”, we will extract the “py” file below. Please wait for a while until it gets completed.
The last line of the output shows that the latest pip package is installed and mounted on Ubuntu 20.04 System.
You can have a look at the latest installed version of the package “pip” as follows:
Reduce Example 01:
Let’s have a first example of reduce function to see its working. First of all, open the terminal and create a new .py file named “one.py” using the touch query as follows:
Now go to the File Explorer and direct towards the home folder. Open the newly created one.py file and write the script shown below in it. This script contains the function of reducing in it. Firstly, you need to import the reduce method from the library functools to use this function in our code using the “from” keyword. After that, we have declared a method “sum” to calculate a sum of two variables, “a” and “b,” and return this sum to a calling statement. After that, we have initialized a list of integers, and a print statement has been used to apply the reduce function. We have passed the list to a function “sum,” and the “sum” has been passed to the reduce method. You can see we have applied the sum function to the list “l.” So this will sum up the values of the list and return them to the print statement to be shown. Save the file.
Let’s execute the saved file by using the “python3” keyword command while using the name of a file “one.py” along with it and tap on the “Enter” button. The terminal’s output shows the sum of all the integer values of a list as 24.
Reduce Example 02:
Let’s take another simple yet lengthy example of reduce method in python to understand it more. Open the same file “one.py” from the home directory and update the code with the below script. Now, this script contains library functools that have been importing the reduce method first. After that, we have defined a function factorial with 2 parameters, x and y, to calculate the factorial of integers provided in some time. The print statement is printing the integer variables and return the calculated factorial or multiplication of both the variables to the calling statement. Then we have defined a 5 element list “l.” The next two print statements have been used to print string values and the sum of the list elements, consequently using the lambda function within the reduce function to avoid complications. The next two print statements are defined to check the largest value from the list using lambda using the reducing method. The last two print statements have been used to print and calculate the factorial by using list members.
We have found the sum, the largest value, and the factorial of the list values in a sequence without any complexity by executing the file.
Conclusion:
Hopefully! You will find this article helpful and easy to implement on your side. Now you can easily use python reduce function by implementing different examples of your own choice.
from Linux Hint https://ift.tt/3cYA4kP
0 Comments