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.
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
- Two numbers are multiplied.
- A sequence times an int repeats it.
- Repetition builds a new sequence.
Code examples
Multiplying numbers
96 10.0 270.0
Repeating sequences
============ [0, 0, 0, 0, 0] hahaha
More examples
Scaling a recipe and the nested-list trap
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:
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
Prefer
Repetition needs an int; a float raises TypeError.
Building grids with nested *
Avoid
Prefer
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
- 1Print a 5-line triangle of * characters where line n has n stars.
- 2Compute 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?