Python Output a Certain Row of the Multiplication Table
We can write a Python function to output a certain row of the multiplication table. This function will accept a parameter nrepresenting the row number to output (between 1 and 9). The function will output that row's multiplication table content.
Example
def
print_multiplication_row
(
n
)
:
for
i
in
range
(
1
,
10
)
:
(
f
"{n} * {i} = {n * i}"
)
Code analysis:
def print_multiplication_row(n):defines a class namedprint_multiplication_rowfunction, which takes a parameternrepresenting the row number to output.for i in range(1, 10):UseforLoop through the numbers from 1 to 9.print(f"{n} * {i} = {n * i}")Use an f-string to format and output the multiplication table content,n * icomputing the product.
Output result: Suppose we call print_multiplication_row(3), the output will be:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
Python3 Examples