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

How to multiply in python

By 4Geeks

Multiply numbers with *, square and raise to powers with **, multiply whole lists or each element, and avoid the common errors.

6 min readLast updated
Multiply in Python icon

How do you multiply in Python?

Short answer: use the * operator. 7 * 3 returns 21. To square a number or raise it to a power, use **: 5 ** 2 returns 25. To multiply all the numbers of a list, use math.prod().

python
print(7 * 3)                       # 21
print(5 ** 2)                      # 25
 
import math
print(math.prod([1, 2, 3, 4, 5]))  # 120

* multiplies, ** raises to a power, math.prod() multiplies a whole list and a list comprehension multiplies each element.

Every way to multiply in Python

What you wantCodeResult
Multiply two numbers7 * 321
Square a number5 ** 2 or pow(5, 2)25
Raise to any power2 ** 101024
Multiply all the numbers in a listmath.prod([1, 2, 3, 4, 5])120
Multiply each element of a list[n * 3 for n in nums][3, 6, 9]
Multiply NumPy arrays element by elementnp.array([1, 2, 3]) * 3[3 6 9]
Multiply matricesa @ bMatrix product
Repeat a string"ab" * 3"ababab"

Multiply numbers with the * operator

python
price = 19
quantity = 3
total = price * quantity
print(total)  # 57

The type of the result depends on the numbers you multiply:

OperationResultType
7 * 321int
2.5 * 410.0float
2 * 1.53.0float

Two integers give an integer. As soon as one of them is a float, the result is a float. Floats can show tiny rounding errors, such as 0.1 * 3 returning 0.30000000000000004. Use round(0.1 * 3, 2) to show 0.3, or the decimal module when you work with money.

To multiply and update a variable at the same time, use *=:

python
score = 10
score *= 2
print(score)  # 20
Career · AI Engineering

You can already do the math in Python, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Square a number in Python

To square a number, multiply it by itself or raise it to the power of 2. All of these return the square of 5:

python
import math
import numpy as np
 
print(5 ** 2)          # 25    exponent operator, the most common
print(pow(5, 2))       # 25    built-in function
print(5 * 5)           # 25    the number times itself
print(math.pow(5, 2))  # 25.0  always returns a float
print(np.power([1, 2, 3], 2))  # [1 4 9]  every item of an array
MethodBest for
x ** 2Everyday code, it is the most readable
pow(x, 2)When the exponent comes from a variable, or you need pow(x, y, mod)
math.pow(x, 2)When you want the result as a float
np.power(arr, 2)Squaring every item of a NumPy array at once

Be careful with negative numbers: -3 ** 2 returns -9, because Python applies ** before the minus sign. Write (-3) ** 2 to get 9. The same operator works for any power: 2 ** 10 is 1024, 2 ** -1 is 0.5 and 9 ** 0.5 is the square root, 3.0.

Multiply all the numbers in a list

math.prod() (Python 3.8 or newer) multiplies every number of a list:

python
import math
 
nums = [1, 2, 3, 4, 5]
print(math.prod(nums))  # 120

If you can't use it, a loop does the same. Start the result at 1, not 0, or everything will be zero:

python
result = 1
for n in nums:
    result *= n
print(result)  # 120

With NumPy, use np.prod(nums). With functools, use reduce(lambda x, y: x * y, nums).

Multiply each element of a list by a number

python
nums = [1, 2, 3]
 
print([n * 3 for n in nums])  # [3, 6, 9]
print(nums * 3)               # [1, 2, 3, 1, 2, 3, 1, 2, 3]

nums * 3 does not multiply the numbers: it repeats the list three times. To multiply each element, use a list comprehension, or a NumPy array:

python
import numpy as np
 
arr = np.array([1, 2, 3])
print(arr * 3)    # [3 6 9]
print(arr * arr)  # [1 4 9]   element by element

For matrix multiplication with NumPy, use @ (or np.dot()), not *.

Multiply strings and lists

The * operator repeats strings and lists when you multiply them by an integer:

python
print("ha" * 3)  # hahaha
print("-" * 20)  # --------------------
print([0] * 5)   # [0, 0, 0, 0, 0]

To create a grid, don't write [[0] * 3] * 3: the three rows are the same list, so changing one changes all of them. Use [[0] * 3 for _ in range(3)] instead.

Use AI to check your calculations

An assistant like ChatGPT, Claude or GitHub Copilot can explain an unexpected result if you give it your code and what you expected. These three prompts are ready to copy.

1. Explain a result

In Python, [paste your code] returns [the result] but I expected [what you expected].
Explain why and show me how to fix it.

2. Multiply data

I have this Python list: [paste the list]. Show me how to multiply [all the numbers / each element by X]
with plain Python and with NumPy, and explain which is faster for large lists.

3. Fix a TypeError

I get this error in Python: [paste the error]. This is my code: [paste the code].
Explain what types are being multiplied and how to convert them.

Run every answer yourself and check the result.

Learn with a mentor

From Python operators to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems when multiplying in Python

What you seeWhy it happensFix
"5" * 2 gives 55The value is a string, for example from input()Convert it: int(value) * 2
TypeError: can't multiply sequence by non-int of type 'float'You multiplied a string or list by a floatUse an integer, or convert the string with float()
[1, 2] * 2 gives [1, 2, 1, 2]* repeats listsUse [n * 2 for n in nums] or NumPy
0.1 * 3 gives 0.30000000000000004Floats are stored in binaryUse round() or the decimal module
-3 ** 2 gives -9** runs before the minus signWrite (-3) ** 2
The product of a list is 0The loop started with result = 0Start with result = 1, or use math.prod()
All the rows of a grid change together[[0] * 3] * 3 repeats the same listUse [[0] * 3 for _ in range(3)]
Free download

Multiplying in Python in a 2-page cheat sheet

The * and ** operators, squaring numbers, multiplying lists with math.prod() and NumPy, result types, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to multiply in Python cheat sheet

2 pages · PDF · 4Geeks

  • The right code for numbers, powers, lists and arrays
  • Why some results surprise you and how to fix them
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about multiplying in Python

Want to go from Python basics to building AI apps, with real projects and mentors?

See the AI Engineering program