Kodeclik Blog

How to multiply numbers in Python

Multiplication is such a core capability in any programming language and of course in Python, a very versatile language. It should come as no surprise that Python provides numerous ways to multiply numbers. In this blog post, we'll explore six different methods for multiplying numbers in Python, from basic arithmetic to advanced techniques, to help you become a proficient Pythonista!
There are actually multiple interpretations of multiplication and for each such interpretation many ways to accomplish the desired objective (see figure below).

Method 1: Use the * operator

The first method is of course to just use the asterisk symbol which means multiplication in numerous programming languages. Here is a simple Python code for this purpose:
This program prints out the total cost of your banana purchase:
Note that the '*' operator works not only for integers but also for floating-point numbers.

Method 2: Use the *= assignment operator

Python provides a shorthand notation for multiplying a variable by a constant and updating its value. This is particularly useful when you need to increment or scale a variable. Consider the following code:
Here we know that bananas cost 50 cents (0.50). Then there is an inflation of 3% which is represented by the inflation factor of 1.03. We then multiply banana_price by inflation and that becomes the new banana_price (this is the effect of the *= operator). The final value is:
Thus we can see that the banana price has increased by 3%.

Method 3: Multiply a list of numbers at once using map()

Assume you have a list of numbers and need to multiply each element of the list by the same quantity. We can use the map() function for this purpose:
Here we have a lambda function that merely multiplies its input by 2. We use map() to apply this function (first argument) to every element of a list (numbers). This returns a map object which is rendered in list notation using the list() constructor. Thus we obtain:
Note that the semantics of simply multiplying the list by 2 is different. While syntactically correct, it yields a different answer. Thus the program:
In other words, this operation does not multiply the individual elements of the list, but instead repeats the entire list a specified number of times.

Method 4: Use the reduce operation on a list

Python's reduce() function takes as input a function and applies it sequentially over an iterable, aggregating the result over the iterable into a single value. Here is how this might work.
In this example, we import the Python module functools to obtain access to the reduce function. The reduce function takes a function (here, product) and applies it to a list (here, numbers). At each step it uses the intermediate result to iteratively call the reduce function on the remaining elements of the list. The output is:

Method 5: Use math.prod()

For advanced mathematical operations, Python provides the math module. It offers various mathematical functions, including multiplication. Here's an example using the math.prod() function, which returns the product of all elements in an iterable:
The output is again:

Method 6: Use numpy.multiply()

If you're working with arrays or matrices, the NumPy library is a powerful tool. NumPy provides efficient multidimensional arrays and mathematical functions for numerical operations. Here's an example of multiplying two NumPy arrays:
The output is:
Wow - aren’t all these options amazing? Multiplication is a fundamental arithmetic operation in Python and thus it is no surprise that there are so many different ways, right from basic arithmetic operators to sophisticated libraries. Which of the above methods is your favorite?
If you liked this blogpost, learn how to find the average of a list in Python.
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.