Javatpoint Logo
Javatpoint Logo

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 i Value of j prints Explanation
i = 0 j = - - Does not print anything
i = 1 j = 0 1 Prints 1 in the first line, goes to the next line
i = 2 j = 0, 1 2 2 Prints 2 for 2 times (notice that we are printing the value of i, not j)
i = 3 j = 0, 1, 2 3 3 3 Prints 3 for 3 times
i = 4 j = 0, 1, 2, 3 4 4 4 4 Prints 4 for 4 times
i = 5 j = 0, 1, 2, 3, 4 5 5 5 5 5 Prints 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 i Value of j Value of num prints
i = 5 j = 1, 2, 3, 4, 5 1 1 1 1 1 1
i = 4 j = 1, 2, 3, 4 2 2 2 2 2
i = 3 j = 1, 2, 3 3 3 3 3
i = 2 j = 1, 2 4 4 4
i = 1 j = 1 5 5

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 i Value of j prints
1 1 1
2 1, 2 1 2
3 1, 2, 3 1 2 3
4 1, 2, 3, 4 1 2 3 4
5 1, 2, 3, 4, 5 1 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 i Value of j Value of num prints
5 0, 1, 2, 3, 4 5 5 5 5 5 5
4 0, 1, 2, 3 4 4 4 4 4
3 0, 1, 2 3 3 3 3
2 0, 1 2 2 2
1 0 1 1

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 num Value of i Value of j prints
5 5 0, 1, 2, 3, 4 5 5 5 5 5
4 0, 1, 2, 3 5 5 5 5
3 0, 1, 2 5 5 5
2 0, 1 5 5
1 0 5

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 i Value of j prints
5 1 2 3 4 5 1 2 3 4 5
4 1 2 3 4 1 2 3 4
3 1 2 3 1 2 3
2 1 2 1 2
1 1 1

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 i Value of j prints
1 1 1
2 2 1 2 1
3 3 2 1 3 2 1
4 4 3 2 1 4 3 2 1
5 5 4 3 2 1 5 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 j Values of num: num, num +=1 Condition (j > i) prints
1 5, 4, 3, 2, 1 1 5, 4, 3, 2 spaces
1 1
2 5, 4, 3, 2, 1 1, 2 5, 4, 3 spaces
2, 1 1 2
3 5, 4, 3, 2, 1 1, 2, 3 5, 4 Spaces
3, 2, 1 1 2 3
4 5, 4, 3, 2, 1 1, 2, 3, 4 5 Spaces
4, 3, 2, 1 1 2 3 4
5 5, 4, 3, 2, 1 1, 2, 3, 4, 5 - No spaces
5, 4, 3, 2, 1 1 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 i Value of j prints
0 0 0
1 0, 1 0 1
2 0, 1, 2 0 2 4
3 0, 1, 2, 3 0 3 6 9
4 0, 1, 2, 3, 4 0 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 i Value of j prints
1 1 1
2 1, 2 3 3
3 1, 2, 3 5 5 5
4 1, 2, 3, 4 7 7 7 7
5 1, 2, 3, 4, 5 9 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…







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA