How to Multiply in Python?

Multiplication is one of the most fundamental operations in programming, and Python makes it incredibly easy and intuitive. I’ve found that mastering multiplication opens the door to countless programming possibilities, from basic math to complex data manipulation.

In this guide, I’ll walk you through how to multiply numbers in Python, explore different methods, and even cover multiplying sequences like lists and strings. Whether you’re just starting with Python or brushing up on your skills, this tutorial will make multiplication clear and practical.

Multiply Numbers in Python: The Basics

Multiplying numbers in Python is easy. You use the * operator, which works for integers, floats, and even complex numbers.

Here’s a simple example:

# Multiply two integers
result = 7 * 5
print("7 multiplied by 5 is:", result)

Output:

7 multiplied by 5 is: 35

I executed the above example code and added the screenshot below.

Multiply in Python

The * operator multiplies the two values and returns the product. You can multiply floats just as easily:

# Multiply two floating-point numbers
result = 3.5 * 2.0
print("3.5 multiplied by 2.0 is:", result)

Output:

3.5 multiplied by 2.0 is: 7.0

This is the most common way to multiply in Python and works perfectly for all numeric types.

Multiply Multiple Numbers Together

Sometimes, you want to multiply more than two numbers at once. You can chain the * operator:

result = 2 * 3 * 4
print("2 multiplied by 3 and then by 4 is:", result)

Output:

2 multiplied by 3 and then by 4 is: 24

I executed the above example code and added the screenshot below.

How to Multiply in Python

Alternatively, if you have a list of numbers and want to multiply all of them, you can use a loop or Python’s math.prod() function (available from Python 3.8 onwards):

import math

numbers = [2, 3, 4]
result = math.prod(numbers)
print("Product of the list is:", result)

Output:

Product of the list is: 24

Using math.prod() is efficient and clean, especially when dealing with larger datasets.

Multiply Variables in Python

In real-world Python scripts, you often multiply variables rather than hardcoded numbers. Here’s how you do it:

price_per_item = 15.99
quantity = 4

total_cost = price_per_item * quantity
print("Total cost for 4 items is: $", total_cost)

Output:

Total cost for 4 items is: $ 63.96

I executed the above example code and added the screenshot below.

How to Multiply Python

This example mimics a common scenario in US retail or e-commerce, where you calculate the total price by multiplying the unit price by the quantity.

Multiply Strings and Lists: Repetition Using the * Operator

Python’s * operator is versatile. Beyond numbers, you can multiply strings and lists to repeat them.

Multiply Strings

word = "USA "
result = word * 3
print(result)

Output:

USA USA USA 

This repeats the string three times. It’s handy when you want to create repeated patterns or formats.

Multiply Lists

numbers = [1, 2, 3]
result = numbers * 2
print(result)

Output:

[1, 2, 3, 1, 2, 3]

I executed the above example code and added the screenshot below.

How can I Multiply in Python

This repeats the entire list twice. It’s useful in scenarios like duplicating data or creating test datasets.

Use Functions to Multiply in Python

To keep your code clean and reusable, I recommend wrapping multiplication logic inside functions. Here’s a simple function to multiply two numbers:

def multiply(a, b):
    return a * b

# Example usage
result = multiply(12, 4)
print("12 multiplied by 4 is:", result)

Output:

12 multiplied by 4 is: 48

You can extend this function to multiply any number of arguments using Python’s *args:

def multiply_all(*args):
    product = 1
    for num in args:
        product *= num
    return product

result = multiply_all(2, 3, 5)
print("Product of 2, 3, and 5 is:", result)

Output:

Product of 2, 3, and 5 is: 30

This approach is flexible and powerful for dynamic inputs.

Multiply Matrices and Arrays in Python

When working with data science or engineering tasks in the USA or worldwide, multiplying matrices or arrays is a common operation. Python’s numpy library simplifies this:

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Matrix multiplication
result = np.dot(A, B)
print("Matrix multiplication result:\n", result)

Output:

Matrix multiplication result:
 [[19 22]
 [43 50]]

If you’re working with large datasets or scientific computing, learning how to multiply arrays efficiently is essential.

Common Mistakes to Avoid When Multiplying in Python

  • Confusing * with **: The * operator multiplies, while ** is used for exponentiation (power). For example, 2 ** 3 equals 8, not 6.
  • Multiplying incompatible types: You can’t multiply a string by a float. For instance, “USA” * 2.5 will cause an error.
  • Forgetting to import modules: To use math.prod() or numpy, you must import their modules first.

Multiplication in Python is simple yet versatile. Whether you’re multiplying numbers, repeating strings or lists, or working with complex data structures like matrices, Python’s * operator and built-in functions make it easy.

In my experience, mastering multiplication early on helps you write cleaner, more efficient Python programs. Experiment with the examples above, and soon you’ll multiply your Python skills exponentially!

If you want to dive deeper, exploring libraries like numpy and practicing with real-world datasets will take your multiplication knowledge to the next level.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.