Python: How to Write Logic to Print Triangle

The below is the best example for nested loop in Python language. The specialty of this logic is you can create your output in the form of triangle.

 
nested loop in python

Logic in Python

max = 8
 for x in range(1, max + 1):
     for y in range(1, x + 1):
         print y,
         print 

Result

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8

 Also Read

Comments

Popular posts from this blog

Hyperledger Fabric: 20 Real Interview Questions

How to Fix Python Syntax Errors Quickly

7 AWS Interview Questions asked in Infosys, TCS