Multiplication Table

Tags: tiny, beginner, math

This program generates a multiplication table from 0 × 0 to 12 × 12. While simple, it provides a useful demonstration of nested loops.

multiplication_table.py
 1"""Multiplication Table, by Al Sweigart al@inventwithpython.com
 2Print a multiplication table.
 3This code is available at https://nostarch.com/big-book-small-python-programming
 4Tags: tiny, beginner, math"""
 5
 6print('Multiplication Table, by Al Sweigart al@inventwithpython.com')
 7
 8# Print the horizontal number labels:
 9print('  |  0   1   2   3   4   5   6   7   8   9  10  11  12')
10print('--+---------------------------------------------------')
11
12# Display each row of products:
13for number1 in range(0, 13):
14
15    # Print the vertical numbers labels:
16    print(str(number1).rjust(2), end='')
17
18    # Print a separating bar:
19    print('|', end='')
20
21    for number2 in range(0, 13):
22        # Print the product followed by a space:
23        print(str(number1 * number2).rjust(3), end=' ')
24
25    print()  # Finish the row by printing a newline.

https://inventwithpython.com/bigbookpython/project49.html