gbZDB gbZDB - 4 months ago 7
Python Question

Print a number table in a simple format

I am stuck trying to print out a table in Python which would look like this (first number stands for amount of numbers, second for amount of columns):

>>> print_table(13,4)
0 1 2 3
4 5 6 7
8 9 10 11
12 13


Does anyone know a way to achieve this?

Answer

This is slightly more difficult than it sounds initially.

def numbers(n, r):
    print('\n'.join(' '.join(map(str, range(r*i, min(r*(i + 1), n + 1)))) for i in range(n//r + 1)))

numbers(13, 4)

#>>> 0 1 2 3
4 5 6 7
8 9 10 11
12 13
Comments