Beginner Logical Thinking Python

Number Pattern Programs in Python with Output

Print common number patterns for n rows in Python using nested loops.

  • TimeO(n^2) for each pattern
  • SpaceO(1)
  • Hints3
  • Read4 min
Quick answer

Number pattern programs in Python use two loops. The outer loop runs once per row. The inner loop prints the values of that row with print(value, end=" ") so they stay on one line, and an empty print() ends the row. Print j when values grow across a row, i when the whole row repeats one value, and a counter that never resets for Floyd's triangle.

Nested for loopsrange() with start, stop and stepprint() with endRow and column logic
1

Problem statement

Given n, print each number pattern below using loops. Do not print whole rows as fixed strings.

This page covers the increasing triangle, repeating triangle, Floyd's triangle, the number pyramid and four more. The same patterns in Java are on pattern programs in Java.

Input

An integer n, the number of rows.

Output

The pattern, one row per line.

Examples

Example 1
Inputn = 3, increasing triangle
Output1 / 1 2 / 1 2 3

Row i prints 1 to i. A slash marks a new line here.

Example 2
Inputn = 3, Floyd's triangle
Output1 / 2 3 / 4 5 6

The count continues from one row to the next.

Constraints

  • 1 <= n <= 9 so each number is one digit and the shape stays aligned
  • Floyd's triangle reaches two digits from row 4
2

Think before you code

Reveal the questions to ask yourself first
  • How many rows? That is the outer loop.
  • In row i, how many numbers are printed, and how many spaces come first?
  • Does the number depend on the row i, the column j, or a running counter?
3

Hints

Open one at a time — try again after each.
1 Hint 1
print(x, end=" ") prints x followed by a space instead of a new line.
2 Hint 2
range(1, i + 1) gives 1 to i. range(i - 1, 0, -1) counts down from i - 1 to 1.
3 Hint 3
For leading spaces, " " * (n - i) repeats two spaces n - i times.
4

Approach

Reveal the step-by-step approach

Python pattern programs: the row table

Write what each row needs before writing code. For the number pyramid with n = 4:

Row i Leading spaces Going up Going down
1 3 1 none
2 2 1 2 1
3 1 1 2 3 2 1
4 0 1 2 3 4 3 2 1
Formula n - i 1 to i i - 1 to 1

Each column of the table becomes one part of the row.

What to print

What you see in a row Print
Numbers grow across the row j
Whole row is one number i
Numbers never restart a counter k outside both loops
0 and 1 alternate (i + j + 1) % 2

Each printed number is followed by a space, so it takes two characters. That is why the pyramid uses two spaces per indent step.

If loops are new to you, read loops in programming.

5

Dry run

Walk through the example step by step

Floyd's triangle, n = 3. k starts at 1 and is never reset.

i | j values | printed | k after the row
--+----------+---------+----------------
1 | 1        | 1       | 2
2 | 1 2      | 2 3     | 4
3 | 1 2 3    | 4 5 6   | 7
6

Solution

Reveal the full Python solution
solution.py
def increasing(n):
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(j, end=" ")
        print()


def repeating(n):
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(i, end=" ")
        print()


def floyd(n):
    k = 1
    for i in range(1, n + 1):
        for j in range(1, i + 1):
            print(k, end=" ")
            k += 1
        print()


def pyramid(n):
    for i in range(1, n + 1):
        print("  " * (n - i), end="")
        for j in range(1, i + 1):
            print(j, end=" ")
        for j in range(i - 1, 0, -1):
            print(j, end=" ")
        print()


increasing(4)
print("---")
repeating(4)
print("---")
floyd(4)
print("---")
pyramid(4)

Output:

1
1 2
1 2 3
1 2 3 4
---
1
2 2
3 3 3
4 4 4 4
---
1
2 3
4 5 6
7 8 9 10
---
      1
    1 2 1
  1 2 3 2 1
1 2 3 4 3 2 1
  • Number triangle in Python (increasing): values grow across the row, so print j.
  • Repeating: the whole row is the row number, so print i.
  • Floyd's triangle: k lives outside both loops, so row 2 starts where row 1 ended.
  • Pyramid pattern in Python: three parts per row: spaces, numbers going up, numbers going down. range(i - 1, 0, -1) stops before 0.

Four more number patterns

n = 4
# 5. Inverted triangle
for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
print("---")
# 6. Right-aligned triangle
for i in range(1, n + 1):
    print("  " * (n - i), end="")
    for j in range(1, i + 1):
        print(j, end=" ")
    print()
print("---")
# 7. Decreasing numbers
for i in range(1, n + 1):
    for j in range(n, n - i, -1):
        print(j, end=" ")
    print()
print("---")
# 8. Binary triangle
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print((i + j + 1) % 2, end=" ")
    print()
1 2 3 4
1 2 3
1 2
1
---
      1
    1 2
  1 2 3
1 2 3 4
---
4
4 3
4 3 2
4 3 2 1
---
1
0 1
1 0 1
0 1 0 1
  • Inverted: the outer loop counts down, so the rows get shorter.
  • Right-aligned: same as the increasing triangle, with n - i indents first.
  • Decreasing: range(n, n - i, -1) prints i numbers starting from n.
  • Binary: for i = 1, j = 1, (1 + 1 + 1) % 2 is 1, and the value flips each time j grows by one.

Practice without looking

  1. Rows 1, 2 4, 3 6 9, 4 8 12 16 (print i * j).
  2. Rows 5 4 3 2 1, 5 4 3 2, ... 5.
  3. Rows 1, 1 2, 1 2 3, then back down to 1.
  4. A Pascal triangle (the link has the Java version).

For star shapes, see star pattern programs. To learn Python with a mentor, see the Python full stack course.

TimeO(n^2) for each pattern
SpaceO(1)
7

Review before the interview

Common mistakes

  • Forgetting end=" ", so every number goes on its own line.
  • Calling print() inside the inner loop instead of after it.
  • Writing range(1, i), which stops at i - 1 and makes every row one number short.
  • Resetting k inside the outer loop in Floyd's triangle.
  • Using one space per indent when each number takes two characters.

Edge cases to test

  • n = 1 prints a single 1.
  • n = 0 prints nothing, because range(1, 1) is empty.
  • Numbers of two or more digits break the alignment. Use f"{value:3}" for a fixed width.
  • Each row ends with a trailing space. It does not show on screen, but some online judges compare it.

Interview follow-ups

  • How would you keep the pyramid aligned when numbers reach two digits?
  • Can you print Floyd's triangle using a formula instead of a counter?
  • How would you print these patterns using while loops?

Keep practising

Practising Python for interviews?

CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Chat with us