Python Tables for Multiplication and Addition - output

This post is written for readers with different levels of experience with Python programming and also with Mathematics. Depending on your experience and interest, you will get different things from reading it. It contains a Python program for printing and neatly formatting coloured multiplication and addition tables, and includes coding DNA for achieving the following with Python programming:

  • Print text with coloured backgrounds and foregrounds in a console using Python
  • Create neatly formatted tables with just a few lines of code
  • Create multiplication and addition tables for “normal” arithmetic
  • Create and explore multiplication and addition tables for “finite arithmetics” formed by using the modulo operator
  • Pass operators as arguments to functions
  • Align and specify width of formatted strings

I’ll start by just sharing some code and then we’ll discuss what is does, how it does it and how to use it for your own purposes. You can access the code on repl.it or at the bottom of this post where I will provide the full listing.

Python program to display Multiplication and Addition tables – Online version

On one level, this is a tool to explore basic multiplication and addition tables in Python in colour, which could be useful in a teaching (or self-teaching) context. For a Python programmer though, it uses some very useful tools which you may want to incorporate into our own projects.

Coloured Text in the Console with The Python Colorama Module

To use this module you will need to intsall with pip instal colorama. You can find the module along with documentation here: Colorama. Below is a basic example of its usage.

This gives the following output in PowerShell:

Python coloured text in console

Some colors are different on PowerShell vs CMD, and also will appear different on different operating systems and environments. You may need to pick and choose from the available colours in order to maintain readability. The full list of available colours is:

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET + some lighter versions made like LIGHTMAGENTA_EX from the basic colors just listed.

There are several more demos available on the github page for the project.

Printing Neatly Formatted Tables with the Python Tabulate Module

The Python Tabulate module module allows you to print neatly formatted tables without having to worry about manually positioning your data to make it clear and easily readable. It’s a pretty neat module and well worth experimenting with whenever you have tabular data you want to display in a console from a Python program.

Passing Operators as function arguments

This is not something you can usually do without the help of an imported module. The operator module is our friend here. You can read about the available methods etc. here. In our program, we import just the names of the operators we need: add, mul, and pow, but many more are available.

Exploring Finite Arithmetics with Python Cayley Tables

This is usually a fairly advanced topic in Mathematics from a branch called Abstract Algebra, but actually anyone interested in modular arithmetic can use this tool to explore how the modulo operator works. The program create what are called Cayley Tables (Basically just multiplication/addition tables but for sets of elements other than just the set of all integers). The colour functionality provided by colorama makes it easy to see patterns in tables created with different values and operators. This can be a fascinating investigation for those whom enjoy this kind of thing. The full program is provided below.

Python Cayley Tables Program with Colour Display and Formatted Output

Python programming modulo table

So there you have it – lots going on in this article. Hopefully there will be something in there you can make use. Let me know in the comments if you found it helpful. Happy computing!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python