How to use nested for loops in Python

2 Answers

0 votes
for i in range(1, 4):
    j = 1
    print("i = ", i)
    for j in range(1, 6):
        print("... j = ", j)


'''
run:

i =  1
... j =  1
... j =  2
... j =  3
... j =  4
... j =  5
i =  2
... j =  1
... j =  2
... j =  3
... j =  4
... j =  5
i =  3
... j =  1
... j =  2
... j =  3
... j =  4
... j =  5

'''

 



answered May 8, 2017 by avibootz
0 votes
for x in range(1, 5):
    for y in range(1, 5):
        print('%d * %d = %d' % (x, y, x * y))


'''
run:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16

'''

 



answered Oct 14, 2017 by avibootz

Related questions

1 answer 212 views
2 answers 187 views
1 answer 188 views
3 answers 255 views
...