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

Hyperledger Fabric: 20 Real Interview Questions

7 AWS Interview Questions asked in Infosys, TCS

How to Fix Python Syntax Errors Quickly

Python 'getsizeof' Command the Real Purpose

Blue Prism complete tutorials download now

Vi Editor to Quit use Esc and Colon

Python Dictionary Vs List With Examples

AWS Vs Azure Load Balancers Top Insights

How to Decode TLV Quickly

Tokenization: Vault based Vs Vault-less