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:

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.

 

Python Star Pattern Programs (With Code and Output) 

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: 

1. Right Triangle Star Pattern 

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: 


** 
*** 
**** 
***** 
 

2. Inverted Right Triangle Star Pattern 

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: 

***** 
**** 
*** 
** 

 

3. Left Triangle Star Pattern 

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: 

   * 
   ** 
  *** 
**** 
***** 
 

4. Inverted Left Triangle Star Pattern 

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: 

***** 
**** 
  *** 
   ** 
    * 
 

5. Pyramid Star Pattern 

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

background

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

6. Inverted Pyramid Star Pattern 

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: 

********* 
******* 
  ***** 
   *** 
    * 
 

7. Diamond Star Pattern 

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: 

     * 
   *** 
  ***** 
******* 
********* 
******* 
  ***** 
   *** 
    * 
 

8. Hourglass Star Pattern 

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: 

********* 
******* 
  ***** 
   *** 
    * 
   *** 
  ***** 
******* 
********* 
 

9. Hollow Square Pattern 

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: 

* * * * * 

*       * 

*       * 

*       * 

* * * * *  

10. Right Pascal's Triangle 

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 (With Code and 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. 

1. Simple Number Triangle (Row Number) 

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  
 

2. Simple Number Triangle (Column Number) 

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  
 

3. Floyd's Triangle 

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  
 

4. Binary Number Triangle 

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  
 

5. Pascal's Triangle 

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  
 

6. Inverted Number Triangle 

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  
 

7. Palindrome Number Triangle 

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  
 

9. Number Diamond 

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  
 

10. Zero-One Pyramid 

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  
 

11. Inverted Sequential Number Triangle 

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  
 

12. Column-wise Number Triangle 

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  
 

13. Square of Numbers (Column-wise) 

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   
 

14. Alternating Number Rectangle 

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  
 

15. Pyramid of Palindromic Numbers 

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 
 

Character (Alphabet) Pattern Programs in Python (With Code and Output) 

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

Promise we won't spam!

1. Simple Alphabet Triangle (Row Character) 

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: 


BB 
CCC 
DDDD 
EEEEE 
 

2. Sequential Alphabet Triangle 

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: 


AB 
ABC 
ABCD 
ABCDE 
 

3. Continuous Alphabet Pattern 

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  
 

4. K-Shape Character Pattern 

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 

KK 
KKK 
KKKK 
KKKKK 
 

5. Alphabet Pyramid 

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 
 

6. Square of Alphabets 

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  
 

7. Diamond of Alphabets 

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  
 

8. Hollow Alphabet Square 

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  
 

9. Palindrome Alphabet Pyramid 

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 
 

10. Inverted Alphabet Triangle 

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 

 

Python Pattern Programs Using Recursion (With Code and Output) 

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. 

1. Right Triangle Using Recursion 

Python 
def pattern41(n, current_row=1): 
    if current_row > n: 
        return 
    print("*" * current_row) 
    pattern41(n, current_row + 1) 
 
pattern41(5) 
 

Output: 


** 
*** 
**** 
***** 
 

2. Inverted Right Triangle Using Recursion 

Python 
def pattern42(n): 
    if n == 0: 
        return 
    print("*" * n) 
    pattern42(n - 1) 
 
pattern42(5) 
 

Output: 

***** 
**** 
*** 
** 

 

3. Pyramid Using Recursion 

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: 

   * 
   *** 
  ***** 
******* 
********* 
 

4. Left Triangle Using Recursion 

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: 

   * 
   ** 
  *** 
**** 
***** 
 

5. Hollow Square Using Recursion 

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: 

*  *  *  * * 

*              * 

*               * 

*  *  *  *  * 

Why You Should Practice Pattern Programs in Python 

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. 

  • Builds Loop Proficiency: Patterns are all about nested loops, usually for or while loops. Working through them forces you to master how loops iterate, how nested loops interact, and how to control their flow to produce a specific visual output. 
  • Develops Logical Thinking: The core challenge of a pattern program is breaking down a visual shape into logical, programmable steps. This strengthens your problem-solving skills and teaches you to think like a programmer. 
  • Prepares for Technical Interviews: Questions involving a pattern program in python using for loop are very common in entry-level technical interviews. They are a quick way for interviewers to assess your grasp of fundamental programming concepts. 
  • Introduces Algorithmic Concepts: While simple on the surface, patterns can introduce you to more advanced ideas. For example, solving a pattern with a function that calls itself is a great introduction to recursion. 

Conclusion 

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!

Frequently Asked Questions (FAQs)

1. What is the most common mistake beginners make with pattern programs?

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. 

2. Can I create these patterns with while loops instead of for loops?

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. 

3. What is the end parameter in the print() function and why is it useful?

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. 

4. What is the purpose of the ord() and chr() functions for character patterns?

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. 

5. Is recursion more efficient than loops for printing 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. 

6. How can I get the input for the pattern size from the user?

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. 

7. What is a nested loop?

A nested loop is one loop inside another. In pattern programs: 

  • The outer loop runs once for each row. 
  • The inner loop runs multiple times inside each outer loop run, usually printing stars, numbers, or spaces. 

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. 

8. Can you create patterns with symbols other than stars?

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(). 

9. Why do some patterns need a separate loop for spaces?

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. 

10. How can I combine patterns?

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. 

11. What does the range() function do?

range() generates a sequence of numbers that you can loop through. Examples: 

  • range(5) → 0, 1, 2, 3, 4 
  • range(1, 6) → 1, 2, 3, 4, 5 
  • range(5, 0, -1) → 5, 4, 3, 2, 1 

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. 

12. How do you print a pattern in reverse order?

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. 

 

13. What is the difference between print() and print(end="")?

  • print("*") prints a star and moves to the next line. 
  • print("*", end="") prints a star but stays on the same line. 

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. 

14. Are there libraries in Python for creating patterns?

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. 

15. What is a "hollow" pattern?

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. 
 

16. How do I approach a complex pattern problem?

Don’t try to code it all at once. Break the problem into smaller pieces. Example: For a diamond: 

  1. First identify the top half (pyramid). 
  2. Then the bottom half (inverted pyramid). 

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. 

17. What is ASCII art?

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. 

18. How can I make my pattern code more reusable?

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. 

 

19. What is the role of indentation in these programs?

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. 

20. What is a good next step after mastering pattern programs?

Once you’re confident with patterns, move to solving problems with arrays, strings, and lists. For example: 

  • Reversing a string 
  • Finding the maximum in a list 
  • Searching and sorting problems 

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. 

Rohit Sharma

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

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree

17 Months

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in DS & AI

360° Career Support

Executive PG Program

12 Months