← Back to How to's
Edit on Github

How to multiply in python

Written by:

Javier Leyva Seiglie

How to multiply in python

Short answer:

1result = 3 * 2 2print(result) # 6

Multiply numbers with the * operator:

The most common way to multiply 2 (or more) numbers would be using the operator (*) this is the syntax to use it:

syntax: num1*num2

Complete example:

1def multiply (num1, num2): 2 return num1*num2 3 4print(multiply(3, 2)) # Output: 6

But how could we achieve this without using this operator?

Multiply numbers without the * operator:

Let´s say that you dont want to use the (*) operator. Here´s the way to do it:

1def multWithout(num1, num2): 2 result = 0 3 for x in range(num2): 4 result +=num1 5 return result 6 7print(multWithout(2,10)) # Output: 20

We created a function that receives 2 numbers, we declared a variable result and start is as "0" to store our values and we´ll loop as many times as num2. For each itaration result will be updated adding num1 to the stored ammount.

Multiply as a power operation alternative

The process would be pretty much alike, but the result variable would start as 1, since we will be multiplying instead of adding and as we all know, multiplying with "0" is not a very good idea.

1def powerMult(num1, num2): 2 result = 1 3 for x in range(num2): 4 result *= num1 5 return result 6 7print(mult2(3, 3)) # 27

Multiply strings

Multiplying strings will follow the same structure, here´s an example:

1def multString (str, num): 2 return str*num 3 4print(multString("How to multiply in python ", 2)) # Output: How to multiply in python How to multiply in python

The function multString recevies 2 elements, the string we want to multiply and the second element will be the ammount of times it will repeat (multiply).

Multiply lists of numbers with * operator

The way to multiply a list using the * operator will ask to loop through the given array and multiply each element storing the value on a result variable to return it.

1arr = [1, 2, 3, 4, 5] 2 3def multiplyList(list): 4 5 # Multiplying one by one 6 result = 1 7 for x in list: 8 result = result * x 9 return result 10 11print (multiplyList(arr)) # Output: 120

Multiply lists of numbers with numpy

Numpy is a library widely used by many programmers to handle complex mathematics operations. Here´s how to use it to multiply lists:

1import numpy 2arr = [1, 2, 3, 4, 5] 3 4def multNumpy(arr): 5 return numpy.prod(arr) 6 7print(multNumpy(arr)) 8#Output: 120

First we need to import numpy to be able to use it, and then with numpy.prod() we pass the element we want to multiply (in this case, our array of numbers)

Multiply lists of numbers with reduce

Lambda is one of the most used methods in the python library, here´s how to use it along with reduce to multiply lists

1from functools import reduce 2arr = [1, 2, 3, 4, 5] 3 4def MultListLambaReduce(arr): 5 return reduce((lambda x, y: x * y), arr) 6 7print(MultListLambaReduce(arr)) 8#Output: 120

Multiply lists of numbers with math.prod()

Using the math library you can multiply a list of numbers, here´s an example:

1import math 2arr = [1, 2, 3, 4, 5] 3 4def multProd(arr): 5 return math.prod(arr) 6 7print(multProd(arr)) # Output: 120

We covered different ways to multiply in python numbers, strings and lists (arrays of numbers) with different methos, from the most simple and usual one, as simple as using the (*) operator, to more complex ones using libraries like numpy or math.

Hope you enjoyed your reading and keep on the Geek side.