Skip to content

A comprehensive guide to understanding and creating different pattern-printing programs in Python. Perfect for beginners who want to strengthen their logic-building and looping concepts.

Notifications You must be signed in to change notification settings

CODEMASTER-ABDULLAH-92/Pattern-Printing-In-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pattern Printing in Python

A comprehensive guide to understanding and creating different pattern-printing programs in Python. Perfect for beginners who want to strengthen their logic-building and looping concepts.


πŸ“Œ Table of Contents

S.No. Pattern Name Problem Link Solution Link
1 Square Star Pattern Problem Solution
2 Left Triangle Pattern Problem Solution
3 Right Triangle Pattern Problem Solution
4 Number Triangle Pattern Problem Solution
5 Same Number Triangle Pattern Problem Solution
6 Inverted Number Pattern Problem Solution
7 Number Sequence Pattern Problem Solution
8 Pyramid Pattern Problem Solution
9 Inverted Pyramid Problem Solution
10 Diamond Pattern Problem Solution
11 Number Pyramid with Dashes Problem Solution
12 Reverse Number Pyramid Problem Solution
13 Hollow Square Pattern Problem Solution
14 Hollow Rectangle Pattern Problem Solution
15 Hollow Diamond Pattern Problem Solution
16 Butterfly Pattern Problem Solution
17 Number Pattern with Dashes Problem Solution
18 Complex Number Pattern Problem Solution
19 Inverted Triangle Pattern Problem Solution
20 Combined Triangle Pattern Problem Solution

πŸ“š What You Will Learn

  • Basics of loops for pattern printing
  • Star (*) patterns
  • Number patterns
  • Alphabet patterns
  • Pyramid patterns
  • Hollow patterns
  • Advanced patterns
  • Common mistakes and how to fix them
  • Practice exercises

⭐ Basic Patterns

1. Square Star Pattern

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

Solution:

rows = 5
cols = 5
for i in range(rows):
    for j in range(cols):
        print("*", end="")
    print()

2. Left Triangle Pattern

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

Solution:

rows = 5
for i in range(rows):
    for j in range(i + 1):
        print("*", end="")
    print()

3. Right Triangle Pattern

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

Solution:

rows = 5
for i in range(rows):
    print(" " * (rows - i - 1) + "*" * (i + 1))

πŸ”’ Number Patterns

4. Number Triangle Pattern

1
12
123
1234
12345

Solution:

rows = 5
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end="")
    print()

5. Same Number Triangle Pattern

1
22
333
4444
55555

Solution:

for i in range(5):
    for j in range(i + 1):
        print(i + 1, end="")
    print()

6. Inverted Number Pattern

12345
1234
123
12
1

Solution:

for i in range(5):
    for j in range(1, 6 - i):
        print(j, end="")
    print()

7. Number Sequence Pattern

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Solution:

num = 0
for i in range(1, 6):
    for j in range(i):
        num += 1
        print(f"{num} ", end="")
    print()

πŸ”Ί Pyramid Patterns

8. Pyramid Pattern

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

Solution:

rows = 5
for i in range(rows):
    print(" " * (rows - i - 1) + "*" * (2 * i + 1))

9. Inverted Pyramid

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

Solution:

rows = 5
for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * (2 * i - 1))

10. Diamond Pattern

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

Solution:

# Upper part
for i in range(5):
    for j in range(5 - i):
        print(" ", end="")
    for k in range(i + 1):
        print("*", end="")
    for m in range(i):
        print("*", end="")
    print()

# Lower part
for i in range(4):
    for j in range(i + 2):
        print(" ", end="")
    for k in range(4 - i):
        print("*", end="")
    for m in range(3 - i):
        print("*", end="")
    print()

11. Number Pyramid with Dashes

----1
---12
--123
-1234
12345

Solution:

for k in range(1, 6):
    for m in range(5 - k):
        print("-", end="")
    for n in range(1, k + 1):
        print(n, end="")
    print()

12. Reverse Number Pyramid

----1
---21
--321
-4321
54321

Solution:

for k in range(1, 6):
    for m in range(5 - k):
        print("-", end="")
    for n in range(k, 0, -1):
        print(n, end="")
    print()

πŸ•³οΈ Hollow Patterns

13. Hollow Square Pattern

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

Solution:

rows = 5
cols = 5
for i in range(rows):
    for j in range(cols):
        if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

14. Hollow Rectangle Pattern

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

Solution:

for i in range(1, 5):
    for j in range(1, 5):
        if 2 <= i <= 3 and 2 <= j <= 3:
            print(" ", end="")
        else:
            print("*", end="")
    print()

15. Hollow Diamond Pattern

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

Solution:

# Upper Part
for i in range(5):
    for j in range(5 - i):
        print("*", end="")
    for k in range(i):
        print(" ", end="")
    for m in range(i):
        print(" ", end="")
    for n in range(5 - i):
        print("*", end="")
    print("")

# Lower Part
for i in range(1, 6):
    for j in range(i):
        print("*", end="")
    for k in range(5 - i):
        print(" ", end="")
    for m in range(5 - i):
        print(" ", end="")
    for n in range(i):
        print("*", end="")
    print("")

πŸ”₯ Advanced Patterns

16. Butterfly Pattern

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

Solution:

# Upper Part
for i in range(1, 6):
    for j in range(i):
        print("*", end="")
    for k in range(5 - i):
        print(" ", end="")
    for m in range(5 - i):
        print(" ", end="")
    for n in range(i):
        print("*", end="")
    print()

# Lower Part
for i in range(4):
    for j in range(4 - i):
        print("*", end="")
    for k in range(i + 1):
        print(" ", end="")
    for m in range(i + 1):
        print(" ", end="")
    for n in range(4 - i):
        print("*", end="")
    print()

17. Number Pattern with Dashes

1------1
12----21
123--321
12344321

Solution:

for i in range(1, 5):
    for j in range(1, i + 1):
        print(j, end="")
    print("-" * (4 * 2 - i * 2), end="")
    for k in range(i, 0, -1):
        print(k, end="")
    print()

18. Complex Number Pattern

4 4 4 4 4 4 4 
4 3 3 3 3 3 4 
4 3 2 2 2 3 4 
4 3 2 1 2 3 4 
4 3 2 2 2 3 4 
4 3 3 3 3 3 4 
4 4 4 4 4 4 4 

Solution:

for i in range(1, 8):
    for j in range(1, 8):
        if (i == 2 and 2 <= j <= 6) or (i == 6 and 2 <= j <= 6) or (3 <= i <= 5 and j == 2) or (3 <= i <= 5 and j == 6):
            print("3 ", end="")
        elif (j == 3 and 3 <= i <= 5) or (j == 5 and 3 <= i <= 5) or (i == 3 and 3 <= j <= 5) or (i == 5 and 3 <= j <= 5):
            print("2 ", end="")
        elif i == 4 and j == 4:
            print("1 ", end="")
        else:
            print("4 ", end="")
    print()

19. Inverted Triangle Pattern

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

Solution:

for i in range(5):
    for j in range(5 - i):
        print("*", end="")
    print()

20. Combined Triangle Pattern

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

Solution:

for i in range(5):
    for j in range(i + 1):
        print("*", end="")
    print()
    
for i in range(5):
    for j in range(4 - i):
        print("*", end="")
    print()

❗ Common Mistakes Students Make

1. Forgetting end=""

This prints each symbol on a new line.

2. Using wrong indentation

Python is indentation-sensitive β€” wrong spaces break your program.

3. Misunderstanding ranges

Examples:

  • range(5) β†’ 0 to 4
  • range(1,5) β†’ 1 to 4
  • range(5,0,-1) β†’ 5 to 1

4. Incorrect loop boundaries

Always test edge cases for your loops.


πŸ“ Practice Exercises

Try to code these yourself:

  1. Hollow pyramid
  2. Pascal's triangle
  3. Hourglass pattern
  4. Floyd's triangle
  5. Spiral pattern
  6. Zigzag pattern
  7. Heart pattern
  8. Arrow patterns

🎯 Tips for Success

  1. Start simple: Begin with basic patterns before moving to complex ones
  2. Understand the logic: Don't just copy code - understand the pattern structure
  3. Use variables: Make your code flexible by using variables for rows/columns
  4. Debug step by step: Print intermediate values to understand what's happening
  5. Practice regularly: Pattern printing improves with consistent practice

πŸ“š Resources

Happy Coding! πŸš€

About

A comprehensive guide to understanding and creating different pattern-printing programs in Python. Perfect for beginners who want to strengthen their logic-building and looping concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages