Python Glossary Русский

all Python 3Arithmetic & operatorsterm

* multiplication

arithmetic

The binary multiplication operator. For strings and lists it means repetition.

Syntax

a * b

The classic trap is nesting: [[0] * 3] * 3 does not copy the inner list, it stores the very same one three times, so writing into one row changes every row. Build matrices with a list comprehension instead. A zero or negative factor yields an empty sequence rather than an error.

Examples

print(6 * 7)  # → 42
print("ab" * 3)  # → ababab
print([0] * 5)  # → [0, 0, 0, 0, 0]
print(2.5 * 4)  # → 10.0
print(3 * 3 * 3)  # → 27 (куб без оператора **)
matrix = [[0] * 3 for _ in range(3)]  # 3x3 нулевая матрица
print(matrix[0])  # → [0, 0, 0]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy