Python- multiplication / time table

Hi, here is a time table code for a given value. The tricky part is the 2nd print() function. It starts a new line.

my_int = 5
for i in range(1,my_int+1):
    for j in range(1,my_int+1):
        print(i*j,end= "\t")
    print()
#The output is shown below
1	2	3	4	5	
2	4	6	8	10	
3	6	9	12	15	
4	8	12	16	20	
5	10	15	20	25	

You can add some formatting as shown below
my_int = 5
for i in range(1,my_int+1):
    for j in range(1,my_int+1):
        print("{:5}".format(i*j),end= "\t")
    print()
#The output is shown below
    1	    2	    3	    4	    5	
    2	    4	    6	    8	   10	
    3	    6	    9	   12	   15	
    4	    8	   12	   16	   20	
    5	   10	   15	   20	   25	

Leave a Reply

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