Number patterns in Python

Coding is fun, isn't it? We can have more fun from it when we use our creativity. Printing natural numbers until a specified number is not that fun, but what if we can create a visual pyramid of numbers? Sounds fun?

This article consists of logics to create 10 different number patterns that we can play with using python programming. Have fun!

1. A simple semi pyramid of numbers with the given number of rows

The output should look like this:

Please enter how many rows you need: 5

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

Program:

Understanding:

We took the number of rows as the input from the user. Let us assume the user gave the number of rows: 5. in the first for loop, we are iterating the numbers to be printed from 1 to the rows (5). In the second loop, we are iterating the rows. Print () is used to shift to the next row after one row.

Mechanism Table:

Value of iValue of jprintsExplanation
i = 0j = --Does not print anything
i = 1j = 01Prints 1 in the first line, goes to the next line
i = 2j = 0, 12 2Prints 2 for 2 times (notice that we are printing the value of i, not j)
i = 3j = 0, 1, 23 3 3Prints 3 for 3 times
i = 4j = 0, 1, 2, 34 4 4 4Prints 4 for 4 times
i = 5j = 0, 1, 2, 3, 45 5 5 5 5Prints 5 for 5 times (i gets 5 because we gave range (rows+1))

2. Inverted Simple semi- pyramid with the given number of rows

The output should look like this:

Please enter how many rows you need: 5

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

Program:

Understanding:

We need the rows to be in inverse order. So, we give -1 as the step parameter in range (). We used num variable for the numbers to be in the right ascending order amid the reverse for loop - it is for the numbers iteration.

Value of iValue of jValue of numprints
i = 5j = 1, 2, 3, 4, 511 1 1 1 1
i = 4j = 1, 2, 3, 422 2 2 2
i = 3j = 1, 2, 333 3 3
i = 2j = 1, 244 4
i = 1j = 155

3. Semi pyramid with ascending numbers

The output should look like this:

Please enter how many rows you need: 5

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

Program:

Understanding:

Here, i refer to the row iterations and j to the column iterations.

Value of iValue of jprints
111
21, 21 2
31, 2, 31 2 3
41, 2, 3, 41 2 3 4
51, 2, 3, 4, 51 2 3 4 5

Note that we are printing the value of j.

4. Inverted simple semi pyramid of Descending numbers

The output should look like this:

Please enter how many rows you need: 5

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

Program:

Understanding:

We took the number of rows as input from the user. We iterated i in reverse from rows to 0 as we need the pyramid to be inverted. Then, we used a normal for loop to iterate through the positions in the row. During the iteration from 0 to i, the value of i will keep changing, but we need the same value throughout the row; hence we stored i in num and printed it.

Mechanism table:

Value of iValue of jValue of numprints
50, 1, 2, 3, 455 5 5 5 5
40, 1, 2, 344 4 4 4
30, 1, 2 333 3
20, 122 2
1011

5. Inverted semi-pyramid of the same number

The output should look like this:

Please enter how many rows you need: 5

5 5 5 5 5
5 5 5 5
5 5 5
5 5
5

Program:

Understanding:

We need to print the same number, the number of rows the user inputs in the whole semi-pyramid. So, we stored the number of rows in num and then printed it every time.

Mechanism table:

The value of numValue of iValue of jprints
550, 1, 2, 3, 45 5 5 5 5
40, 1, 2, 35 5 5 5
30, 1, 25 5 5
20, 15 5
105

6. Inverted semi-pyramid with numbers pattern

The output should look like this:

Please enter how many rows you need: 5

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

Program:

Understanding:

We need the numbers to be in ascending order in every row, and the pyramid has to be inverted, for which we used the inverted for loop, and in the inner for loop, we iterated natural numbers from 1 to i+1 to print the numbers.

Mechanism Table:

Value of iValue of jprints
51 2 3 4 51 2 3 4 5
41 2 3 41 2 3 4
31 2 31 2 3
21 21 2
111

Note that here, we printed the value of j in the inner loop.

7. Semi-pyramid of numbers in reverse order

Please enter how many rows you need: 5

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

Program:

Understanding:

We need the numbers to be in descending order. Hence, we used the reverse for loop as the inner loop. We iterated i from 1 to the number of rows + 1 for the positions in the row, and the inner loop is to print the numbers.

Mechanism Table:

Value of iValue of jprints
111
22 12 1
33 2 13 2 1
44 3 2 14 3 2 1
55 4 3 2 15 4 3 2 1

8. Mirrored semi pyramid with ascending numbers

Output:

Please enter how many rows you need: 5

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

Program:

Understanding:

We need to understand where to print spaces and where to print the numbers. We increment the num values to get the ascending order. We iterated i for the rows from 1 to rows + 1. In every row, we need all the places. Hence irrespective of the row number, we iterated j in reverse order. Inside the loop, we check if the value of j is greater than i to check whether to print spaces or numbers.

Mechanism table:

Value of i

Value of jValues of num: num, num +=1Condition (j > i)prints
15, 4, 3, 2, 115, 4, 3, 2spaces
11
25, 4, 3, 2, 11, 25, 4, 3spaces
2, 11 2
35, 4, 3, 2, 11, 2, 35, 4Spaces
3, 2, 11 2 3
45, 4, 3, 2, 11, 2, 3, 45Spaces
4, 3, 2, 11 2 3 4
55, 4, 3, 2, 11, 2, 3, 4, 5-No spaces
5, 4, 3, 2, 11 2 3 4 5

9. Semi pyramid of horizontal tables

Output:

Please enter how many rows you need: 5

0
0 1
0 2 4
0 3 6 9
0 4 8 12 16

Program:

Understanding:

In the first for loop, we iterated i - the multiplication table we wanted to print. It is the same as printing a normal simple pyramid. The only change is to print the value of multiplication i*j. In the inner loop, we iterated i - numbers from 0 to i.

Mechanism table:

Value of iValue of jprints
000
10, 10 1
20, 1, 20 2 4
30, 1, 2, 30 3 6 9
40, 1, 2, 3, 40 4 8 12 16

10. Semi pyramid of alternate numbers

Output:

Please enter how many rows you need: 5

1
3 3
5 5 5
7 7 7 7
9 9 9 9 9

Program:

Understanding:

Here, we used while loops. It is almost the same as a normal simple pyramid of numbers; the only change is we need to print alternate numbers for which we used - i * 2 - 1.

Mechanism Table:

Value of iValue of jprints
111
21, 23 3
31, 2, 35 5 5
41, 2, 3, 47 7 7 7
51, 2, 3, 4, 59 9 9 9 9

BONUS FUN:

Here is a special type of pyramid also asked in interviews. Understand the code with the knowledge gained up till now.

Output:

Please enter how many rows you need: 5

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

Program:

Enjoying the work you are doing makes the work more fun to do. If you think coding is complicated, it will stay like that. But, if you enjoy writing code, it will be your ps5.

Try to create more patterns from around you. Learn from the world.

Enjoy coding…






Latest Courses