0
0
PythonProgramBeginner · 2 min read

Python Program to Print Number Pattern

You can print a number pattern in Python using nested for loops; for example, for i in range(1, n+1): print(*range(1, i+1)) prints numbers from 1 up to the current line number.
📋

Examples

Inputn = 3
Output1 1 2 1 2 3
Inputn = 5
Output1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Inputn = 1
Output1
🧠

How to Think About It

To print a number pattern, think of each line as printing numbers starting from 1 up to the line number. Use an outer loop to go through each line from 1 to n, and an inner loop to print numbers from 1 to the current line number.
📐

Algorithm

1
Get the input number n for the number of lines.
2
For each line i from 1 to n:
3
Print numbers from 1 to i separated by spaces.
4
Move to the next line after printing each line.
💻

Code

python
n = 5
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end=' ')
    print()
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
🔍

Dry Run

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

1

Start outer loop with i=1

Print numbers from 1 to 1: prints '1 '

2

Move to next line

Print newline

3

Outer loop i=2

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

4

Move to next line

Print newline

5

Outer loop i=3

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

6

Move to next line

Print newline and end

iPrinted Numbers
11
21 2
31 2 3
💡

Why This Works

Step 1: Outer loop controls lines

The outer for loop runs from 1 to n, deciding how many lines to print.

Step 2: Inner loop prints numbers

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

Step 3: Print newline after each line

After printing each line's numbers, print() moves the cursor to the next line.

🔄

Alternative Approaches

Using string join and range
python
n = 5
for i in range(1, n + 1):
    print(' '.join(str(x) for x in range(1, i + 1)))
This method uses string joining for cleaner code and avoids nested print calls.
Using list comprehension inside print
python
n = 5
for i in range(1, n + 1):
    print(*[j for j in range(1, i + 1)])
This uses unpacking with <code>*</code> to print numbers separated by spaces without extra loops.

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?

All approaches have similar time complexity; using string join or unpacking can make code cleaner but does not improve time complexity.

ApproachTimeSpaceBest For
Nested loops with printO(n^2)O(1)Simple and clear for beginners
String join with rangeO(n^2)O(1)Cleaner code, easier to read
List comprehension with unpackingO(n^2)O(1)Concise and pythonic
💡
Use nested loops where the outer loop controls lines and the inner loop prints numbers for each line.
⚠️
Beginners often forget to print a newline after each line, causing all numbers to print on one line.