0
0
PythonProgramBeginner · 2 min read

Python Program to Print Number Triangle Pattern

You can print a number triangle pattern in Python using nested loops like for i in range(1, n+1): print(' '.join(str(x) for x in range(1, i+1))) where n is the number of rows.
📋

Examples

Input3
Output1 1 2 1 2 3
Input5
Output1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Input1
Output1
🧠

How to Think About It

To print a number triangle pattern, think of printing rows from 1 to n. For each row, print numbers starting from 1 up to the current row number. Use one loop to go through each row and another loop inside it to print the numbers for that row.
📐

Algorithm

1
Get the number of rows (n) as input.
2
For each row from 1 to n:
3
Print numbers starting from 1 up to the current row number separated by spaces.
4
Move to the next line after printing each row.
💻

Code

python
n = int(input('Enter number of rows: '))
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()
Output
Enter number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
🔍

Dry Run

Let's trace the program for input n=3 to see how it prints the number triangle.

1

Input

User inputs n = 3

2

First row (i=1)

Print numbers from 1 to 1: prints '1 '

3

Second row (i=2)

Print numbers from 1 to 2: prints '1 2 '

4

Third row (i=3)

Print numbers from 1 to 3: prints '1 2 3 '

Row (i)Numbers Printed
11
21 2
31 2 3
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, controlling how many rows to print.

Step 2: Inner loop prints numbers

The inner for loop prints numbers from 1 up to the current row number i.

Step 3: Print moves to next line

After printing each row, print() moves the cursor to the next line for the next row.

🔄

Alternative Approaches

Using list comprehension and join
python
n = int(input('Enter number of rows: '))
for i in range(1, n + 1):
    print(' '.join(str(x) for x in range(1, i + 1)))
This method is concise and uses string joining to print each row in one line.
Using while loops
python
n = int(input('Enter number of rows: '))
i = 1
while i <= n:
    j = 1
    while j <= i:
        print(j, end=' ')
        j += 1
    print()
    i += 1
This uses while loops instead of for loops, which some beginners find easier to understand.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops: the outer loop runs n times and the inner loop runs up to n times in the last iteration, resulting in O(n^2) time.

Space Complexity

The program uses constant extra space, only variables for counters, so space complexity is O(1).

Which Approach is Fastest?

Both for-loop and while-loop approaches have the same time and space complexity; the list comprehension method is more concise but similar in performance.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clear and easy to understand
List comprehension with joinO(n^2)O(1)Concise and readable
Nested while loopsO(n^2)O(1)Beginners learning loop basics
💡
Use nested loops where the outer loop controls rows and the inner loop prints numbers for each row.
⚠️
Forgetting to move to the next line after each row causes all numbers to print on one line.