Skip to content

Module 4 · Operators — topic 4 of 16

Multiplication

The * operator for multiplying numbers and repeating strings, lists and tuples, with practical uses in totals, scaling, percentages and building repeated patterns.

Beginner 10 min read·Lesson 47 of 306 in Python Programming

Learning objectives

  • Multiply numbers of mixed types
  • Repeat sequences with *
  • Avoid the shared-reference trap with nested list repetition

Definition

The * operator for multiplying numbers and repeating strings, lists and tuples, with practical uses in totals, scaling, percentages and building repeated patterns.

Introduction

The * operator multiplies numbers. With a sequence and an integer it repeats the sequence: "ab" * 3 is "ababab" and [0] * 4 is [0, 0, 0, 0].

Why it matters

Price times quantity, area, scaling a recipe and applying a percentage all use multiplication. Sequence repetition is a quick way to create separators, starting lists and simple patterns.

Detailed explanation

int * int gives an int; any float operand gives a float.

Percentages are usually computed as value * percent / 100 or value * 0.18.

Sequence repetition requires an integer: "-" * 2.5 raises TypeError. Multiplying by 0 or a negative number gives an empty sequence.

[0] * 5 is a convenient way to create a list of five zeros.

Beware nested lists: [[0] * 3] * 2 creates two references to the same inner list, so changing one row changes both. Build grids with a comprehension instead: [[0] * 3 for _ in range(2)].

math.prod(iterable) multiplies all values together, the multiplication counterpart of sum().

Multiplying by a decimal factor is the cleanest way to apply percentages and conversions. A 25% increase is value * 1.25, a 25% discount is value * 0.75, and converting kilometres to metres is km * 1000. Storing such factors in named constants, such as KM_TO_M = 1000, makes formulas self-explaining.

Repetition also works with tuples: (0, 1) * 3 is (0, 1, 0, 1, 0, 1). Like strings, the original tuple is untouched because repetition always builds a new sequence.

Syntax

Python
product = a * b
line = "=" * 20
zeros = [0] * size
  • Two numbers are multiplied.
  • A sequence times an int repeats it.
  • Repetition builds a new sequence.

Code examples

Multiplying numbers

Python
print(12 * 8)
print(2.5 * 4)
print(1500 * 18 / 100)
Output
96
10.0
270.0

Repeating sequences

Python
print("=" * 12)
print([0] * 5)
print("ha" * 3)
Output
============
[0, 0, 0, 0, 0]
hahaha

More examples

Scaling a recipe and the nested-list trap

Python
import math
recipe = {"rice (g)": 200, "water (ml)": 400, "salt (g)": 5}
servings_factor = 3
for item, amount in recipe.items():
print(f"{item}: {amount * servings_factor}")
print("product:", math.prod([2, 3, 4]))
grid = [[0] * 3] * 2
grid[0][0] = 9
print("shared rows:", grid)
grid = [[0] * 3 for _ in range(2)]
grid[0][0] = 9
print("separate rows:", grid)
Output
rice (g): 600
water (ml): 1200
salt (g): 15
product: 24
shared rows: [[9, 0, 0], [9, 0, 0]]
separate rows: [[9, 0, 0], [0, 0, 0]]

Step-by-step explanation

  • Each recipe amount is multiplied by the servings factor of 3.
  • math.prod multiplies 2 × 3 × 4.
  • [[0] * 3] * 2 repeats a reference to one inner list, so both rows change together.
  • The comprehension creates a new inner list for each row, so only the first row changes.

Expected output

Running “Scaling a recipe and the nested-list trap” prints exactly:

TextOutput
rice (g): 600
water (ml): 1200
salt (g): 15
product: 24
shared rows: [[9, 0, 0], [9, 0, 0]]
separate rows: [[9, 0, 0], [0, 0, 0]]

Practical applications

  • Shopping carts multiply unit price by quantity for each line.
  • Image processing multiplies pixel values to adjust brightness.
  • Text reports use "-" * width for separators.

Best practices

  • Use math.prod() to multiply many values.
  • Create nested lists with comprehensions.
  • Write percentages as value * percent / 100 for clarity.

Common mistakes

Repeating with a float

Avoid

"*" * 2.0

Prefer

"*" * 2

Repetition needs an int; a float raises TypeError.

Building grids with nested *

Avoid

board = [[" "] * 3] * 3

Prefer

board = [[" "] * 3 for _ in range(3)]

The first form shares one row three times.

Key takeaways

  • * multiplies numbers and repeats sequences.
  • Repetition needs an integer count.
  • Nested repetition shares inner lists; use comprehensions for grids.

Try it yourself

  1. 1
    Print a 5-line triangle of * characters where line n has n stars.
  2. 2
    Compute the total cost of 7 items at Rs 149 each plus 5% tax.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic