What is pattern program in python?

Python is a scripting language that is highly readable, interactive, high-level, object-oriented, and interpreted. Python has lesser syntactic structures than other programming languages, and it typically uses English terms instead of punctuation.

Main Features of Python

  • Beginner-friendly Language - Python is easy to learn, maintain, implement and read. It is interactive in nature.
  • Object-oriented - Python encapsulates code within objects by supporting the Object-Oriented programming approach or style or approach.
  • Industry-oriented - Python is extendable, portable, scalable, cross-platform friendly with a standard library, and has support for GUI applications and interactive mode.

18 Examples of Pattern Programs in Python

Pattern #1 - Number Pattern Semi-Pyramid

Pattern 

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

Code

n = 9

for n in range[1, n+1]:

    for m in range[1, n + 1]:

        print[m, end=’ ’‘]

    print[“”]

Output 

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5 

1 2 3 4 5 6 

1 2 3 4 5 6 7 

1 2 3 4 5 6 7 8 

1 2 3 4 5 6 7 8 9 

Pattern #2 - Printing the Same Digit Pattern Using Inverted Pyramid

Pattern 

7 7 7 7 7 7 7 

7 7 7 7 7 7

7 7 7 7 7 

7 7 7 7

7 7 7

7 7

7

Code

n = 7

digit = n

for k in range[n, 0, -1]:

    for m in range[0, k]:

        print[digit, end=' ']

    print["\r"]

Output

7 7 7 7 7 7 7 

7 7 7 7 7 7 

7 7 7 7 7 

7 7 7 7 

7 7 7 

7 7 

Pattern #3 - Printing Numbers Using Inverted Pyramids

Pyramid

1 1 1 1 1 1 1

2 2 2 2 2 2

3 3 3 3 3 

4 4 4 4 

5 5 5

6 6

7

Code

n = 7

m = 0

for k in range[n, 0, -1]:

    m += 1

    for n in range[1, k + 1]:

        print[m, end=' ']

    print['\r']

Output 

1 1 1 1 1 1 1 

2 2 2 2 2 2 

3 3 3 3 3 

4 4 4 4 

5 5 5 

6 6 

Pattern #4 - Printing Numbers in a Simple Triangle Pattern

Pyramid

1  

2 2  

3 3 3  

4 4 4 4  

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7

Code

n = 8

for digit in range[n]:

    for k in range[digit]:

        print[digit, end=" "]

    print[" "]

Output

1  

2 2  

3 3 3  

4 4 4 4  

5 5 5 5 5  

6 6 6 6 6 6  

7 7 7 7 7 7 7  

Pattern #5 - Printing Descending Numbers Using the Inverted Pyramid

Pyramid

7 7 7 7 7 7 7

6 6 6 6 6 6

5 5 5 5 5 

4 4 4 4 

3 3 3 

2 2 

1

Code

n = 7

for k in range[n, 0, -1]:

    digit = k

    for m in range[0, k]:

        print[digit, end=' ']

    print["\r"]

Output

7 7 7 7 7 7 7 

6 6 6 6 6 6 

5 5 5 5 5 

4 4 4 4 

3 3 3 

2 2 

Pattern #6 - Printing Natural Numbers < 17 Using a Pyramid

Pattern

2 3 4 

5 6 7 8 9 

10 11 12 13 14 15 16 

Code

curr = 1

end = 2

n = 4

for k in range[n]:

    for m in range[1, end]:

        print[curr, end=' ']

        curr += 1

    print[""]

    end += 2

Output

2 3 4 

5 6 7 8 9 

10 11 12 13 14 15 16 

Pattern #7 - Printing Numbers in a Reverse Semi-pyramid

Pattern

2 1 

3 2 1 

4 3 2 1 

5 4 3 2 1 

6 5 4 3 2 1 

7 6 5 4 3 2 1 

Code

n = 8

for r in range[1, n]:

    for m in range[r, 0, -1]:

        print[m, end=' ']

    print[""]

Output

2 1 

3 2 1 

4 3 2 1 

5 4 3 2 1 

6 5 4 3 2 1 

7 6 5 4 3 2 1 

Pattern #8 - Printing Digits From 22 in a Reverse Pattern

Pattern

4 3 

7 6 5 

11 10 9 8 

16 15 14 13 12 

22 21 20 19 18 17 

Code

beg = 1

end = 2

curr = end

for r in range[1, 8]:

    for c in range[beg, end]:

        curr -= 1

        print[curr, end=' ']

    print[""]

    beg = end

    end += r

    curr = end

Output

4 3 

7 6 5 

11 10 9 8 

16 15 14 13 12 

22 21 20 19 18 17 

Pattern #9 - Printing a Number Pattern Using an Inverted Semi-pyramid

Pattern

0 1 2 3 4 5 6 7 8 

0 1 2 3 4 5 6 7 

0 1 2 3 4 5 6 

0 1 2 3 4 5 

0 1 2 3 4 

0 1 2 3 

0 1 2 

0 1 

Code

n = 8

for k in range[n, 0, -1]:

    for m in range[0, k + 1]:

        print[m, end=' ']

    print["\r"]

Output

0 1 2 3 4 5 6 7 8 

0 1 2 3 4 5 6 7 

0 1 2 3 4 5 6 

0 1 2 3 4 5 

0 1 2 3 4 

0 1 2 3 

0 1 2 

0 1 

Pattern #10 - Printing a Number Pyramid in a Connected Pyramid

Pattern

1234567

 234567

  34567

   4567

    567

     67

      7

Code

n = 8

for k in range[0, n]:

    for m in range[n-1, k-1]:

        print[m, end=""]

    for j in range[k]:

        print[' ', end=""]

    for h in range[k + 1, n]:

        print[h, end=""]

    print['\n']

Output

1234567

 234567

  34567

   4567

    567

     67

      7

Pattern #11 - Printing a Horizontal Table Using a Pyramid

Pattern

0 1 

0 2 4 

0 3 6 9 

0 4 8 12 16 

0 5 10 15 20 25 

0 6 12 18 24 30 36 

0 7 14 21 28 35 42 49 

0 8 16 24 32 40 48 56 64 

0 9 18 27 36 45 54 63 72 81 

Code

n = 10

for k in range[0, n]:

    for m in range[0, k + 1]:

        print[k * m, end=' ']

    print[]

Output

0 1 

0 2 4 

0 3 6 9 

0 4 8 12 16 

0 5 10 15 20 25 

0 6 12 18 24 30 36 

0 7 14 21 28 35 42 49 

0 8 16 24 32 40 48 56 64 

0 9 18 27 36 45 54 63 72 81 

Pattern #12 - Printing a Right-Angled Triangle of Number Pyramid by Mirroring

Pattern

              1 

            1 2 

          1 2 3 

        1 2 3 4 

      1 2 3 4 5 

    1 2 3 4 5 6 

  1 2 3 4 5 6 7 

Code

n = 8

for r in range[1, n]:

    digit = 1

    for m in range[n, 0, -1]:

        if m > r:

            print[" ", end=' ']

        else:

            print[digit, end=' ']

            digit += 1

    print[""]

Output

              1 

            1 2 

          1 2 3 

        1 2 3 4 

      1 2 3 4 5 

    1 2 3 4 5 6 

  1 2 3 4 5 6 7 

Pattern #13 - Printing a Pattern of Unique Digits Using a Pyramid

Pattern

1 2 1 

1 2 3 2 1 

1 2 3 4 3 2 1 

1 2 3 4 5 4 3 2 1 

1 2 3 4 5 6 5 4 3 2 1 

1 2 3 4 5 6 7 6 5 4 3 2 1 

Code

n = 8

for k in range[1, n + 1]:

    for m in range[1, k-1]:

        print[m, end=" "]

    for m in range[k-1, 0, -1]:

        print[m, end=" "]

    print[]

Output

1 2 1 

1 2 3 2 1 

1 2 3 4 3 2 1 

1 2 3 4 5 4 3 2 1 

1 2 3 4 5 6 5 4 3 2 1 

1 2 3 4 5 6 7 6 5 4 3 2 1 

Pattern #14 - Printing a Pyramid Pattern With Even Numbers

Pattern

16 

16 14 

16 14 12 

16 14 12 10 

16 14 12 10 8 

16 14 12 10 8 6 

16 14 12 10 8 6 4 

16 14 12 10 8 6 4 2 

Code

n = 8

last_even = 2 * n

even_num = last_even

for k in range[1, n+1]:

    even_num = last_even

    for m in range[k]:

        print[even_num, end=' ']

        even_num -= 2

    print["\r"]

Output

16 

16 14 

16 14 12 

16 14 12 10 

16 14 12 10 8 

16 14 12 10 8 6 

16 14 12 10 8 6 4 

16 14 12 10 8 6 4 2 

Pattern #15 - Printing a Number Pyramid of Alternate Numbers

Pattern

3 3 

5 5 5 

7 7 7 7 

9 9 9 9 9 

11 11 11 11 11 11 

13 13 13 13 13 13 13 

Code

n = 7

k = 1

while k

Chủ Đề