4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to multiply in python

Learn how to multiply in Python using various methods, from the simple asterisk operator to advanced libraries like NumPy. Master your coding skills today!
5 min readLast updated

Short answer:

python
result = 3 * 2
print(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:

python
def multiply (num1, num2):
	return num1*num2
 
print(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 don't want to use the (*) operator. Here´s the way to do it:

python
def multWithout(num1, num2):
    result = 0
    for x in range(num2):
        result +=num1
    return result
 
print(multWithout(2,10)) # Output: 20

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

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.

python
def powerMult(num1, num2):
    result = 1
    for x in range(num2):
        result *= num1 
    return result
 
print(powerMult(3, 3)) # 27

Multiply strings

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

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

The function multString receives 2 elements, the string we want to multiply and the second element will be the amount 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.

python
arr = [1, 2, 3, 4, 5]
 
def multiplyList(list):
 
    # Multiplying one by one
    result = 1
    for x in list:
        result = result * x
    return result
 
print (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:

python
import numpy
arr = [1, 2, 3, 4, 5]
 
def multNumpy(arr):
    return numpy.prod(arr)
 
print(multNumpy(arr))
#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 a Python keyword to create small anonymous functions, here´s how to use it along with reduce to multiply lists

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

Multiply lists of numbers with math.prod()

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

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

We covered different ways to multiply in python numbers, strings and lists (arrays of numbers) with different methods, 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.

Multiplying numbers, strings and lists in Python becomes second nature once you apply it in real applications. A software development bootcamp lets you practice operators like these in guided projects with mentors.

Frequently asked questions

How do you multiply each element of a list by a number in Python?

Use a list comprehension: [x * 3 for x in numbers] returns a new list with every element multiplied by 3. Writing numbers * 3 does something different, because the * operator on a list repeats it, so [1, 2] * 3 gives [1, 2, 1, 2, 1, 2]. With NumPy, numpy.array(numbers) * 3 multiplies element by element.

What does ** mean in Python?

** is the exponent operator: 3 ** 3 returns 27, the same result as a loop that multiplies 3 by itself three times. The built-in pow(3, 3) is equivalent and also accepts a third argument for modular arithmetic. Exponents can be negative or fractional, so 2 ** -1 returns 0.5 and 9 ** 0.5 returns 3.0.

Can you multiply a float by an integer in Python?

Yes. Python converts the integer to a float automatically, so 2 * 1.5 returns 3.0. Multiplying two integers gives an int, and as soon as one operand is a float the result is a float. The exception is strings and lists: they can only be repeated by an integer, so "ab" * 1.5 raises a TypeError.

Why does Python repeat a number instead of multiplying it?

The value is a string, not a number. The input() function always returns text, so input() * 2 turns "5" into "55" instead of 10. Convert the value first with int() or float(), as in int(input("Number: ")) * 2. The same happens with numbers read from files or command-line arguments, which also arrive as strings.