Number pattern programs are a common topic in Python programming, especially for beginners looking to understand nested loops, logic, and problem-solving techniques. These patterns help you practice control structures like loops and understand how they can be used to print structured output.
In this article, we’ll cover 7 different number pattern programs, starting from basic to advanced, with simple Python code and easy-to-understand explanations.
Left Triangle Pattern
This is one of the most basic patterns where we print a left triangle of numbers.
Pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code:
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
Explanation:
- The outer loop controls the number of rows (
n
). - The inner loop prints numbers from
1
to the current row number (i
). - After each row,
print()
moves to the next line.
Inverted Left Triangle Pattern
This pattern prints an inverted pyramid of numbers, starting from 5
down to 1
.
Pattern:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Source Code:
n = 5
for i in range(n, 0, -1):
for j in range(i, 0, -1):
print(j, end=" ")
print()
Explanation:
- The outer loop runs from
n
to1
, decreasing by1
in each iteration. - The inner loop prints numbers from the current row’s starting value (
i
) down to1
. - After each row,
print()
moves to the next line.
Floyd’s Triangle
Floyd’s triangle is a right-angle triangle where the numbers are printed in a continuous sequence.
Pattern:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Source Code:
n = 5
num = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints numbers in a continuous sequence, starting from
1
.
Pyramid Number Pattern
Let’s display numbers in a centered triangular format.
Pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code:
def print_pyramid(rows):
for i in range(1, rows + 1):
# Print leading spaces
print(" " * (rows - i), end="")
# Print numbers in increasing order
for j in range(1, i + 1):
print(j, end=" ")
# Move to the next line
print()
# Example: Print a pyramid with 5 rows
print_pyramid(5)
Explanation:
- The outer loop controls the number of rows (
n
). - The first inner loop prints leading spaces to center the numbers.
- The second inner loop prints numbers starting from 1 to the current row number (
i
). - After printing each row,
print()
moves to the next line to form the pyramid shape.
Pascal’s Triangle
Pascal’s Triangle is a triangular array of numbers where each number is the sum of the two numbers directly above it. The first and last values in each row are always 1.
Pattern:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Source Code:
def print_pascals_triangle(rows):
for i in range(rows):
# Print leading spaces
print(" " * (rows - i), end=" ")
# Initialize the first element of each row as 1
num = 1
for j in range(i + 1):
print(num, end=" ")
# Update num to the next value in Pascal's Triangle
num = num * (i - j) // (j + 1)
# Move to the next line after each row
print()
# Example: Print Pascal's Triangle with 5 rows
print_pascals_triangle(5)
Explanation:
- The outer loop controls the number of rows (
n
). - The first inner loop prints leading spaces to center the numbers, making the triangle shape.
- The second inner loop:
- Starts with
num = 1
for every row (since the first number in Pascal’s Triangle is always 1). - Prints the current number and updates
num
using the formula for Pascal’s Triangle:num = num * (i - j) // (j + 1)
. - This formula computes the next number in the row based on the previous number, using combinations.
- Starts with
- After printing the numbers in each row,
print()
moves to the next line to continue the pattern.
Spiral Number Pattern in Python
A Spiral Number Pattern generates numbers in a spiral-like manner inside a 2D grid. The pattern starts from the outermost layer and proceeds inward, moving right, down, left, and up alternately, filling the grid with increasing numbers.
Pattern:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Source Code:
def spiral_pattern(n):
# Create an empty n x n grid initialized with zeroes
grid = [[0 for _ in range(n)] for _ in range(n)]
# Initialize the boundaries
top, left = 0, 0
bottom, right = n - 1, n - 1
# Starting number
num = 1
# Fill the grid in a spiral pattern
while top <= bottom and left <= right:
# Fill the top row (left to right)
for i in range(left, right + 1):
grid[top][i] = num
num += 1
top += 1
# Fill the right column (top to bottom)
for i in range(top, bottom + 1):
grid[i][right] = num
num += 1
right -= 1
# Fill the bottom row (right to left)
if top <= bottom:
for i in range(right, left - 1, -1):
grid[bottom][i] = num
num += 1
bottom -= 1
# Fill the left column (bottom to top)
if left <= right:
for i in range(bottom, top - 1, -1):
grid[i][left] = num
num += 1
left += 1
# Print the grid in a formatted manner
for row in grid:
print(' '.join(map(str, row)))
# Example usage
n = 4
spiral_pattern(n)
Explanation:
- Grid Initialization:
- A 2D grid of size
n x n
is created and initialized with0
.
- A 2D grid of size
- Boundary Setup:
- The variables
top
,bottom
,left
, andright
are initialized to track the boundaries of the spiral.
- The variables
- Starting Number:
- The variable
num
keeps track of the current number to be inserted into the grid, starting from 1.
- The variable
- Filling the Grid:
- The pattern is filled in layers, starting from the outermost layer and moving inward. The process alternates between four main directions:
- Left to Right (Top Row): Fill the top row from the left to the right boundary.
- Top to Bottom (Right Column): Fill the rightmost column from the top to the bottom boundary.
- Right to Left (Bottom Row): Fill the bottom row from the right to the left boundary.
- Bottom to Top (Left Column): Fill the leftmost column from the bottom to the top boundary.
- The pattern is filled in layers, starting from the outermost layer and moving inward. The process alternates between four main directions:
- Boundary Adjustment:
- After filling a row or column, the respective boundary is updated (e.g., move the
top
boundary down after filling the top row).
- After filling a row or column, the respective boundary is updated (e.g., move the
- Condition Checks:
- Each time before filling a row or column, conditions like
top <= bottom
andleft <= right
are checked to ensure the process remains within bounds.
- Each time before filling a row or column, conditions like
- Print the Result:
- Finally, the 2D grid is printed row by row to display the spiral pattern.
Reverse Number Triangle
This pattern forms a triangle where the numbers decrease from 5
in each row.
Pattern:
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
Source Code:
n = 5
for i in range(n, 0, -1):
for j in range(n, i - 1, -1):
print(i, end=" ")
print()
Explanation:
- The outer loop runs from
n
to1
. - The inner loop prints the row number multiple times to form the triangle.
Conclusion
Number pattern programs are a great way to sharpen your programming skills, especially for mastering loops and nested loops in Python. These examples range from simple triangles to more complex patterns like spirals and Pascal’s triangle, offering a broad scope of learning opportunities for beginners and intermediate programmers.
By practicing these patterns, you’ll improve your understanding of Python’s control flow and how to format output in structured patterns. Happy coding!
👉 For more Python pattern programs click here