Top 40 Pattern Programs in Python to Master Loops and Recursion
By Rohit Sharma
Updated on Sep 24, 2025 | 38 min read | 49.43K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Sep 24, 2025 | 38 min read | 49.43K+ views
Share:
Table of Contents
Are you learning Python and want to build a strong foundation in loops and logic? A great way to practice these core skills is by solving pattern programs in python. These exercises are a rite of passage for new programmers, helping to translate logical thinking into functional code. They are an excellent way to get comfortable with control flow and nested structures.
In this blog, you will find a massive collection of 40 Pattern Programs in Python. We have organized them into categories like star, number, and character patterns, and even show you how to solve some using recursion. Each example includes the logic, the complete code, and the final output to help you learn effectively.
Build the future with code! Explore our range of Software Engineering Courses and kickstart your journey to becoming a tech expert.
Popular Data Science Programs
Star pattern programs in Python are the classic starting point. They help you understand the structure of rows and columns using a simple, single character.
Pave the path for a rewarding tech career with some of our top programs:
The outer loop controls the rows, and the inner loop prints stars equal to the current row number.
Python
def pattern1(n):
for i in range(1, n + 1):
print("*" * i)
pattern1(5)
Output:
*
**
***
****
*****
The outer loop counts down from the total number of rows.
Python
def pattern2(n):
for i in range(n, 0, -1):
print("*" * i)
pattern2(5)
Output:
*****
****
***
**
*
This pattern requires an extra loop to print leading spaces.
Python
def pattern3(n):
for i in range(1, n + 1):
print(" " * (n - i) + "*" * i)
pattern3(5)
Output:
*
**
***
****
*****
This combines a countdown loop for stars with an increasing loop for spaces.
Python
def pattern4(n):
for i in range(n, 0, -1):
print(" " * (n - i) + "*" * i)
pattern4(5)
Output:
*****
****
***
**
*
This pattern combines spaces and an increasing number of stars centered in each row.
Python
def pattern5(n):
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern5(5)
Output:
*
***
*****
*******
*********
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
The reverse of the pyramid pattern, with loops counting downwards.
Python
def pattern6(n):
for i in range(n - 1, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern6(5)
Output:
*********
*******
*****
***
*
A diamond is created by printing a pyramid followed by an inverted pyramid.
Python
def pattern7(n):
# Upper part
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
# Lower part
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern7(5)
Output:
*
***
*****
*******
*********
*******
*****
***
*
An hourglass is an inverted pyramid followed by a regular pyramid.
Python
def pattern8(n):
# Upper part
for i in range(n - 1, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
# Lower part
for i in range(1, n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
pattern8(5)
Output:
*********
*******
*****
***
*
***
*****
*******
*********
This uses conditional logic to print stars only for the border.
Python
def pattern9(n):
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
pattern9(5)
Output:
* * * * *
* *
* *
* *
* * * * *
A right triangle and an inverted right triangle joined together.
Python
def pattern11(n):
for i in range(1, n + 1):
print("*" * i)
for i in range(n - 1, 0, -1):
print("*" * i)
pattern11(5)
Output:
*
**
***
****
*****
****
***
**
*
Number pattern programs in python replace the stars with numbers, often based on the row or column index, to create more complex logical challenges.
Prints the row number repeatedly in each row.
Python
def pattern16(n):
for i in range(1, n + 1):
for j in range(i):
print(i, end=" ")
print()
pattern16(5)
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Prints numbers sequentially from 1 up to the current column number.
Python
def pattern17(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
pattern17(5)
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
This pattern prints consecutive numbers in a right triangle shape.
Python
def pattern18(n):
num = 1
for i in range(1, n + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
pattern18(5)
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Prints a pattern of alternating 0s and 1s based on column position.
Python
def pattern19(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j % 2, end=" ")
print()
pattern19(5)
Output:
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
A famous mathematical pattern where each number is the sum of the two numbers directly above it.
Python
def pattern20(n):
for i in range(n):
print(" " * (n - i - 1), end="")
num = 1
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1)
print()
pattern20(5)
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Prints descending numbers in an inverted triangle.
Python
def pattern21(n):
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()
pattern21(5)
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Creates a pyramid where each row is a palindrome.
Python
def pattern22(n):
for i in range(1, n + 1):
print(" " * (n - i), end="")
# Ascending part
for j in range(1, i + 1):
print(j, end="")
# Descending part
for j in range(i - 1, 0, -1):
print(j, end="")
print()
pattern22(5)
Output:
1
121
12321
1234321
123454321
8. Square of Numbers
Prints a square where each row contains the row number.
Python
def pattern23(n):
for i in range(1, n + 1):
for j in range(n):
print(i, end=" ")
print()
pattern23(5)
Output:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
A diamond shape made of numbers, increasing to a center row.
Python
def pattern24(n):
# Upper part
for i in range(1, n + 1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print(j, end=" ")
print()
# Lower part
for i in range(n - 1, 0, -1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print(j, end=" ")
print()
pattern24(5)
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
A pyramid pattern made of alternating zeros and ones.
Python
def pattern25(n):
for i in range(1, n + 1):
for j in range(1, i + 1):
if (i + j) % 2 == 0:
print(1, end=" ")
else:
print(0, end=" ")
print()
pattern25(5)
Output:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
The reverse of the sequential number triangle, counting down rows.
Python
def pattern26(n):
for i in range(n, 0, -1):
for j in range(i, 0, -1):
print(j, end=" ")
print()
pattern26(5)
Output:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
The numbers increase down the columns instead of across the rows.
Python
def pattern27(n):
for i in range(1, n + 1):
val = i
for j in range(1, i + 1):
print(val, end=" ")
val += n - j
print()
pattern27(5)
Output:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
A square of numbers that increments down the columns.
Python
def pattern28(n):
for i in range(1, n + 1):
for j in range(n):
print(i + j * n, end=" ")
print()
pattern28(5)
Output:
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
A rectangle of numbers that alternates its order in each row.
Python
def pattern29(n):
for i in range(1, n + 1):
if i % 2 != 0:
for j in range(1, n + 1):
print(j, end=" ")
else:
for j in range(n, 0, -1):
print(j, end=" ")
print()
pattern29(5)
Output:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
A pyramid where each row is a number that reads the same forwards and backwards.
Python
def pattern30(n):
for i in range(1, n + 1):
print(" " * (n - i), end="")
print(str(11 ** (i - 1)).replace("0", " "))
pattern30(5)
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
These patterns use alphabets, which is a great way to practice working with ASCII values using Python's ord() and chr() functions.
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
Prints the character corresponding to the row number repeatedly.
Python
def pattern31(n):
for i in range(n):
char = chr(ord('A') + i)
print(char * (i + 1))
pattern31(5)
Output:
A
BB
CCC
DDDD
EEEEE
Prints alphabets in sequence up to the current row.
Python
def pattern32(n):
for i in range(n):
row_string = ""
for j in range(i + 1):
row_string += chr(ord('A') + j)
print(row_string)
pattern32(5)
Output:
A
AB
ABC
ABCD
ABCDE
Prints alphabets continuously across all rows and columns.
Python
def pattern33(n):
char_code = ord('A')
for i in range(n):
for j in range(i + 1):
print(chr(char_code), end=" ")
char_code += 1
print()
pattern33(5)
Output:
A
B C
D E F
G H I J
K L M N O
Forms the shape of the letter 'K' using any character.
Python
def pattern34(n, char='K'):
for i in range(n, 0, -1):
print(char * i)
for i in range(2, n + 1):
print(char * i)
pattern34(5, 'K')
Output:
KKKKK
KKKK
KKK
KK
K
KK
KKK
KKKK
KKKKK
A pyramid shape made of alphabets.
Python
def pattern35(n):
for i in range(n):
print(" " * (n - i - 1), end="")
char = chr(ord('A') + i)
print(char * (2 * i + 1))
pattern35(5)
Output:
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
Prints a square where each row contains the same character.
Python
def pattern36(n):
for i in range(n):
char = chr(ord('A') + i)
print((char + " ") * n)
pattern36(5)
Output:
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E
A diamond shape where each row contains the corresponding alphabet character.
Python
def pattern37(n):
# Upper part
for i in range(n):
print(" " * (n - i - 1), end="")
char = chr(ord('A') + i)
print((char + " ") * (i + 1))
# Lower part
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1), end="")
char = chr(ord('A') + i)
print((char + " ") * (i + 1))
pattern37(5)
Output:
A
B B
C C C
D D D D
E E E E E
D D D D
C C C
B B
A
Forms a hollow square using alphabet characters.
Python
def pattern38(n):
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print(chr(ord('A') + j), end=" ")
else:
print(" ", end=" ")
print()
pattern38(5)
Output:
A B C D E
A E
A E
A E
A B C D E
A pyramid where each row is a palindromic sequence of characters.
Python
def pattern39(n):
for i in range(n):
print(" " * (n - i - 1), end="")
# Ascending
for j in range(i + 1):
print(chr(ord('A') + j), end="")
# Descending
for j in range(i - 1, -1, -1):
print(chr(ord('A') + j), end="")
print()
pattern39(5)
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
An inverted triangle with sequential alphabets.
Python
def pattern40(n):
for i in range(n, 0, -1):
row_string = ""
for j in range(i):
row_string += chr(ord('A') + j)
print(row_string)
pattern40(5)
Output:
ABCDE
ABCD
ABC
AB
A
Solving a pattern program in python can also be done with recursion. This is a more advanced approach where a function calls itself to solve a problem.
Python
def pattern41(n, current_row=1):
if current_row > n:
return
print("*" * current_row)
pattern41(n, current_row + 1)
pattern41(5)
Output:
*
**
***
****
*****
Python
def pattern42(n):
if n == 0:
return
print("*" * n)
pattern42(n - 1)
pattern42(5)
Output:
*****
****
***
**
*
Python
def pattern43(n, current_row=0):
if current_row == n:
return
print(" " * (n - current_row - 1) + "*" * (2 * current_row + 1))
pattern43(n, current_row + 1)
pattern43(5)
Output:
*
***
*****
*******
*********
Python
def pattern44(n, current_row=1):
if current_row > n:
return
print(" " * (n - current_row) + "*" * current_row)
pattern44(n, current_row + 1)
pattern44(5)
Output:
*
**
***
****
*****
Python
def pattern45_helper(n, i, j):
if j == n:
print()
return
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
pattern45_helper(n, i, j + 1)
def pattern45(n, i=0):
if i == n:
return
pattern45_helper(n, i, 0)
pattern45(n, i + 1)
pattern45(5)
Output:
* * * * *
* *
* *
* * * * *
After we get to the code, it is helpful to understand why these exercises are so valuable. Solving a pattern program in python is more than just a simple coding challenge.
You have now seen a large collection of 40 different examples of a pattern program in python, from simple stars to complex numbers and recursive solutions. These exercises are a fantastic tool for any aspiring Python developer. They build a strong, intuitive understanding of loops, conditional logic, and problem-solving.
The key to mastering loops and logic is consistent practice. Choose a pattern, try to solve it yourself, and then compare your solution to the ones here. This process of trial, error, and learning will make you a more confident and capable programmer.
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
The most common mistake is mixing up the logic of the outer and inner loops. The outer loop usually controls the number of rows, and the inner loop handles what happens within each row, like printing stars, numbers, or spaces. Beginners often swap their roles or forget to reset the inner loop logic for each row. A simple way to avoid this is to always ask: “What repeats per row?” (inner loop) and “How many rows are needed?” (outer loop). Once you separate those, the logic becomes clear.
Yes. Any pattern program in Python using for loop can also be written with while loops. The difference is that with while, you need to initialize a counter before the loop and increment it manually inside the loop. For example:
i = 0
while i < 5:
print("*" * (i+1))
i += 1
This produces a right triangle. While loops give you more control, but for loops are simpler for patterns since they work naturally with the range() function.
Normally, print() moves the cursor to the next line after printing. For patterns, we often want to keep printing on the same line. That’s where end="" comes in. It changes what happens after printing. For example:
print("*", end=" ")
keeps printing stars on the same line with a space in between. Without this, every star would be on a new line, which breaks the pattern shape.
ord() converts a character into its ASCII value (e.g., ord("A") = 65).
chr() converts an ASCII value back into a character (e.g., chr(65) = "A").
When you need alphabet patterns like A-B-C triangles or sequences, you can add numbers to ASCII values and then convert them back to letters. Example:
for i in range(5):
print(chr(65 + i))
This prints letters A to E. These two functions are the backbone of alphabet-based patterns.
No. Loops are more efficient for patterns. Recursion calls the same function repeatedly, which takes more memory because each call is stored in the stack. But recursion is still useful for learning. For example, a recursive triangle pattern shows how a bigger problem can be broken into smaller ones. If efficiency is your goal, stick with loops, but if understanding recursion is the goal, try both.
You can use the input() function to let the user decide the number of rows or size. For example:
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
print("*" * i)
This makes your code flexible. Instead of hardcoding sizes, you can print different shapes based on user input. Always remember to convert input() into int() because it returns a string by default.
A nested loop is one loop inside another. In pattern programs:
For example:
for i in range(5):
for j in range(i+1):
print("*", end="")
print()
The result is a right triangle. Understanding nested loops is the first step to solving any pattern program in Python.
Yes. Instead of "*", you can use any character, symbol, or even emoji (if allowed). For example:
for i in range(5):
print("#" * (i+1))
This prints a triangle with #. You can also mix symbols, like alternating * and @ to form unique designs. It’s all about what character you put inside print().
Patterns like pyramids, diamonds, or left-aligned triangles need alignment. To create that alignment, you print spaces before printing stars or numbers. Example:
for i in range(5):
print(" " * (5-i-1) + "*" * (i+1))
Here, " " * (5-i-1) prints spaces before stars, which shifts them to the right, creating a pyramid shape. Without this spacing logic, your pyramid would just look like a right triangle.
You can combine smaller patterns to form bigger shapes. For example, a diamond pattern is a pyramid followed by an inverted pyramid. By calling the functions one after another, you get a complete diamond. Combination is about recognizing that big patterns are made from smaller ones stacked together.
range() generates a sequence of numbers that you can loop through. Examples:
This is powerful in patterns because you decide how many rows to loop, whether to count up or down, and how many steps to skip.
To print an inverted or reverse pattern, you adjust the range(). For example:
for i in range(5, 0, -1):
print("*" * i)
This prints 5 stars first, then 4, down to 1. By decreasing instead of increasing, you flip the direction of the pattern.
This difference is what allows us to print multiple symbols in a row before moving to the next line. Without end="", patterns would break line by line.
Not really, because patterns are meant as practice exercises for loops, logic, and problem-solving. Libraries exist for graphics (like Turtle, Pygame, or Matplotlib), but for text-based shapes, writing your own code is better. That’s the whole learning experience—understanding how loops and conditions can draw shapes.
A hollow pattern prints only the borders of the shape while leaving the inside empty. Example:
*****
* *
* *
*****
To create this, you use conditions: print stars at the border (first row, last row, first column, last column), and print spaces inside. This combines if/else with nested loops, which makes it slightly trickier but very rewarding to code.
Don’t try to code it all at once. Break the problem into smaller pieces. Example: For a diamond:
Once you have each part working, join them. Also, sketch the pattern on paper first to see the rows, spaces, and symbols. This makes it much easier to translate into code.
ASCII art is creating designs and images using characters from the ASCII table (letters, numbers, and symbols). Pattern programs are a simple form of ASCII art. With creativity, you can go beyond triangles and squares to draw letters, logos, or even faces. It’s a fun way to see coding and art mix together.
Wrap your logic in functions. Example:
def right_triangle(n):
for i in range(1, n+1):
print("*" * i)
right_triangle(5)
right_triangle(8)
Now you can call the function with different values to generate different sizes. You can also pass arguments for the character, making your function usable for multiple shapes.
Indentation defines code blocks in Python. Without proper indentation, loops and conditions won’t work correctly, and Python will throw errors. For nested loops, the inner loop must be indented under the outer loop, and the print statements inside them must be indented again. Beginners often mess this up, which changes the logic or crashes the program.
Once you’re confident with patterns, move to solving problems with arrays, strings, and lists. For example:
These problems require the same skills: nested loops, conditions, and logical thinking. Patterns teach you how loops work. Arrays and algorithms teach you how to solve real-world problems with them.
834 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources