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:
num_bananas = 3
banana_price = 0.50
print(num_bananas*banana_price)
This program prints out the total cost of your banana purchase:
1.5
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:
banana_price = 0.50
inflation = 1.03
banana_price *= inflation
print(banana_price)
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:
0.515
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:
numbers = [1,2,3,4]
print(list(map(lambda x: 2*x, numbers)))
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:
[2, 4, 6, 8]
Note that the semantics of simply multiplying the list by 2 is different. While syntactically correct, it yields a different answer. Thus the program:
numbers = [1,2,3,4]
print(numbers*2)
prints:
[1, 2, 3, 4, 1, 2, 3, 4]
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.
from functools import reduce
def product(x,y):
return(x*y)
numbers = [1,2,3,4]
print(reduce(product,numbers))
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:
24
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:
import math as m
numbers = [1,2,3,4]
print(m.prod(numbers))
The output is again:
24
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:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.multiply(a, b)
print(result)
The output is:
[ 4 10 18]
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.