Back to Problems
## Scalar Multiplication of a Matrix (easy)

#### Example

## Scalar Multiplication of a Matrix

When a matrix \(A\) is multiplied by a scalar \(k\), the operation is defined as multiplying each element of \(A\) by \(k\).
Given a matrix \(A\):
\[
A = \begin{pmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{pmatrix}
\]
And a scalar \(k\), the result of the scalar multiplication \(kA\) is:
\[
kA = \begin{pmatrix}
ka_{11} & ka_{12} \\
ka_{21} & ka_{22}
\end{pmatrix}
\]
This operation scales the matrix by \(k\) without changing its dimension or the relative proportion of its elements.

Write a Python function that multiplies a matrix by a scalar and returns the result.

Example: input: matrix = [[1, 2], [3, 4]], scalar = 2 output: [[2, 4], [6, 8]] reasoning: Each element of the matrix is multiplied by the scalar.

def scalar_multiply(matrix: list[list[int|float]], scalar: int|float) -> list[list[int|float]]: return [[element * scalar for element in row] for row in matrix]

Output will be shown here.

Solution copied to clipboard!