One liner that outputs multiplication tables in Python

One liner

$ python -c "print '\n'.join([''.join(['%3d'%(i*j) for i in range(1,10)]) for j in range(1,10)])"

Commentary (for myself)

$ python -c" " is a way to run python programs directly from the command line

[] Is a comprehension in Python. It seems that you can easily write a loop. When you run print [i * j for i in range (1,10) for j in range (1,10)],

[1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9, ...Omission...36, 45, 54, 63, 72, 81]

The calculation result of multiplication table is displayed in full.

In order to break this line appropriately, first use the join function to make the calculation result into one character string for each stage.

print [''.join(['%3d'%(i*j) for i in range(1,10)]) for j in range(1,10)]

['  1  2  3  4  5  6  7  8  9', '  2  4  6  8 10 12 14 16 18', '  3  6  9 12 15 18 21 24 27', '  4  8 12 16 20 24 28 32 36', '  5 10 15 20 25 30 35 40 45', '  6 12 18 24 30 36 42 48 54', '  7 14 21 28 35 42 49 56 63', '  8 16 24 32 40 48 56 64 72', '  9 18 27 36 45 54 63 72 81']

After that, in order to break this line by step, insert it with the join function and it's done.

print '\n'.join([''.join(['%3d'%(i*j) for i in range(1,10)]) for j in range(1,10)])

Output result

  1  2  3  4  5  6  7  8  9
  2  4  6  8 10 12 14 16 18
  3  6  9 12 15 18 21 24 27
  4  8 12 16 20 24 28 32 36
  5 10 15 20 25 30 35 40 45
  6 12 18 24 30 36 42 48 54
  7 14 21 28 35 42 49 56 63
  8 16 24 32 40 48 56 64 72
  9 18 27 36 45 54 63 72 81

Recommended Posts

One liner that outputs multiplication tables in Python
One liner that outputs 1000000 digits of pi in Python
One liner in Python
One liner webServer (with CGI) in python
python Zundko One Liner
One liner to 100% CPU1 core usage in Python
The one that displays the progress bar in Python
Fizzbuzz in Python (in one line)
curl -I python one liner
DMD in Python one dimension
Matrix multiplication in python numpy
Create code that outputs "A and pretending B" in python
Make python segfault in one line
Handle multiple python versions in one jupyter
CGI server (1) python edition in one line
How to display multiplication table in python
I tried to develop a Formatter that outputs Python logs in JSON
Decompose command arguments in one line in Python
[Python] Invert bool value in one line
I wrote Project Euler 1 in one liner.
Draw contour lines that appear in textbooks (Python)
A memo that I wrote a quicksort in Python
Try implementing two stacks in one array in Python
Building an environment that uses Python in Eclipse
A program that removes duplicate statements in Python
Static type checking that starts loosely in Python
One liner that lists the colors of matplotlib
Python code that removes contiguous spaces into one
Make a rock-paper-scissors game in one line (python)
Testing methods that return random values in Python
Multiple graphs are displayed in one window (python)
One-liner that outputs 10000 digits of pi with Python
Sample program that outputs syslog with Python logging
Formulas that appear in Doing Math with Python
Sample to put Python Kivy in one file
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python