Python Output a Row of the Multiplication Table
We can write a Python function to output a row of the multiplication table. This function will accept a parametern, which represents the row number to output (between 1 and 9). The function will output the multiplication table content for that row.
Example
def print_multiplication_row(n):
for i in range(1, 10):
print(f"{n} * {i} = {n * i}")
for i in range(1, 10):
print(f"{n} * {i} = {n * i}")
Code explanation:
def print_multiplication_row(n):Defines a function namedprint_multiplication_row, which accepts a parametern, representing the row number to output.for i in range(1, 10):Usesforloop to iterate over numbers from 1 to 9.print(f"{n} * {i} = {n * i}")Uses f-string to format and output the multiplication table content,n * icomputes the product.
Output result:
Suppose we callprint_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 = 27Other Extensions
Python3 Examples