Number Pattern Programs in Python with Output
Print common number patterns for n rows in Python using nested loops.
- Time
O(n^2) for each pattern - Space
O(1) - Hints3
- Read4 min
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.
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.
An integer n, the number of rows.
The pattern, one row per line.
Examples
n = 3, increasing triangle1 / 1 2 / 1 2 3Row i prints 1 to i. A slash marks a new line here.
n = 3, Floyd's triangle1 / 2 3 / 4 5 6The 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
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 columnj, or a running counter?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
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 | |
|---|---|
| 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.
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
Solution
Reveal the full Python solution
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:
klives 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 - iindents first. - Decreasing:
range(n, n - i, -1)printsinumbers starting fromn. - Binary: for
i = 1, j = 1,(1 + 1 + 1) % 2is 1, and the value flips each timejgrows by one.
Practice without looking
- Rows
1,2 4,3 6 9,4 8 12 16(printi * j). - Rows
5 4 3 2 1,5 4 3 2, ...5. - Rows
1,1 2,1 2 3, then back down to1. - 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.
O(n^2) for each patternO(1)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
Pattern Programs in Java with Explanation
Hints · Dry run · SolutionSolve Similar problemStar Pattern Programs in Java
Hints · Dry run · SolutionSolve Similar problemPascal Triangle in Java
Hints · Dry run · SolutionSolve Similar problemDiamond Pattern in Java
Hints · Dry run · SolutionSolve Related learningLoops In Programming
TutorialLearn Related learningWhat Is Python Used For
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
