DEV Community

Cover image for Mater Logic With Number Pattern in Python - 1
datatoinfinity
datatoinfinity

Posted on

Mater Logic With Number Pattern in Python - 1

Imagine looking at a simple series of numbers on a screen—and suddenly realizing you can predict the next move, decipher hidden logic, and even create your own mesmerizing patterns with just a few lines of code. That’s the secret magic behind number patterns in Python, and today, we’ll not only unravel how they work, but give you the tools to build your own “number masterpieces” using the power of simple logic!

Number Pattern 1.

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

Explanation:

  1. Simple explanation for this we just print column.
  2. for i in range(1,row+1) controls the number of row.
  3. for j in range(1,i+1) controls the number of column.

Dry Run Table

Row (i) Inner Loop (j in range(i)) Output
1 0 1
2 0, 1 1 2
3 0, 1, 2 1 2 3
4 0, 1, 2, 3 1 2 3 4
5 0, 1, 2, 3, 4 1 2 3 4 5

Other way around;

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

Just print row, print(i,end=" ")

Number Pattern 2.

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
row=5
num=0
for i in range(1,row+1):
    for j in range(i):
        num=num+1
        print(num,end=" ")
    print()

Explanation:

  1. row=5 number of rows and column.
  2. num=0 will be incremented for every printed number.
  3. for i in range(1,row+1) print number of rows.
  4. for j in range(i) print number of column.
  5. num=num+1 advances the sequence and prints each number, spacing them on the same line.

Dry Run Table

Row (i) Inner Loop (j in range(i)) num=0, num=num+1
1 0 1
2 0, 1 2 3
3 0, 1, 2 4 5 6
4 0, 1, 2, 3 7 8 9 10
5 0, 1, 2, 3, 4 11 12 13 14 15

Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-1
Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-2
Build Your Logic from Scratch: Python Pattern Problems Explained. Star Pattern-3

Top comments (0)