How to Multiply a Variable in Python?

Recently, we were working on a shopping cart application in Python, and we were required to do some multiplication. In this tutorial, I will show you how to multiply a variable in Python using different methods and examples.

To multiply variables in Python, you simply use the * operator. For example, if you have two variables, a = 5 and b = 3, you can multiply them by writing result = a * b. This will store the product of a and b in the variable result, which in this case would be 15.

Multiply a Variable in Python

Now, let me show you how to multiply a variable in Python using different methods and examples.

1. Basic Multiplication

The best way to multiply numbers in Python is by using the * operator. Here’s a simple example:

# Basic multiplication
result = 5 * 3
print(result)  # Output: 15

This example demonstrates how to multiply two integers in Python directly. I executed the Python code, and you can see the output in the screenshot below:

multiply a variable in python

Check out How to Increment a Variable in Python?

2. Multiply Variables

Often, you’ll need to multiply variables rather than hard-coded numbers. So, in those cases, we need to multiply variables in Python. Let’s look at an example of calculating the total price of items in a shopping cart.

# Multiplying variables
price_per_item = 20
quantity = 3
total_price = price_per_item * quantity
print(total_price)  # Output: 60

In this example, price_per_item and quantity are variables that store the price of a single item and the number of items, respectively. We multiply these variables to find the total price.

You can also see the exact output in the screenshot below:

multiply variables in Python

3. Using the math Module

While the * operator is sufficient for basic multiplication, Python’s math module offers additional functionality for more complex mathematical operations. However, for multiplication, the * operator is generally preferred. Here’s an example for this:

import math

# Using math.prod for multiplying elements in an iterable (Python 3.8+)
numbers = [2, 3, 4]
product = math.prod(numbers)
print(product)  # Output: 24

Check out How to Subtract from a Variable in Python?

4. Multiply Lists

Now, let me show you how to multiply lists in Python.

Multiplying elements in a list requires a different approach. You can use a loop or the functools.reduce function with operator.mul. Here’s how:

from functools import reduce
import operator

numbers = [2, 3, 4]
product = reduce(operator.mul, numbers, 1)
print(product)  # Output: 24

This method efficiently multiplies all elements in the list.

5. Multiply Matrices

Matrix multiplication is a bit more complex and typically requires the use of libraries such as NumPy. Here’s an example using NumPy:

import numpy as np

# Define two matrices
matrix_a = np.array([[1, 2], [3, 4]])
matrix_b = np.array([[5, 6], [7, 8]])

# Multiply matrices
result = np.dot(matrix_a, matrix_b)
print(result)
# Output:
# [[19 22]
#  [43 50]]

This example explains how to perform matrix multiplication using the np.dot function from the NumPy library.

Read Divide a Variable in Python

Practical Example: Multiply Variables in Python

Now, let me give you a real-time example of how to multiply variables in Python. Here, I will show you how to calculate the total cost.

Example: Calculate Total Cost

Suppose you’re developing a shopping application for a grocery store in New York. You need to calculate the total cost of apples and oranges in a customer’s cart.

# Prices in dollars
price_per_apple = 1.5
price_per_orange = 2.0

# Quantities
num_apples = 4
num_oranges = 3

# Total cost
total_cost = (price_per_apple * num_apples) + (price_per_orange * num_oranges)
print(total_cost)  # Output: 12.0

I executed the above Python code, and you can see the exact output in the screenshot below:

how to multiply a variable in python

Conclusion

In this tutorial, I explained how to multiply a variable in Python using different methods like basic multiplication, multiplying variables, using the math module, multiplying lists, and matrix multiplication with NumPy. I hope the practical example also helps you to know how to multiply variables in Python.

You may also like:

Leave a Comment