In this practice, we will see how to write 10 different number pattern programs in Python using for loop. To understand this example, you should know the very basics of python. I mean core python. If you want to learn python easily, you can consider this course, Core Python Course.

### Video Explanation to Print Number Pattern Program in Python

The below video explains a simple Pattern Program in Python as the video focuses more on the approach. If you understand one pattern program, you can understand all the remaining programs very easily. Before you move to the next pattern, don’t forget to watch the video.

## Python Program to Print Number Patterns

We have covered different types of number patterns programs in tutorials lest start the programs.

### Pattern 1: Simplest Number Pattern in Python

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

This pattern prints the number in 4 different lines. With each new line, a number is incremented. We can implement this pattern in two different ways, one using a single loop and the other using two loops. Let’s see the code for both methods.

#### Method 1: Simplest Number Pattern in Python using for Loop

Method one is a bit tricky and uses the string multiplication feature of python. In python, multiplying strings repeats the strings. So, if you write “A” * 5, “A” will be printed 5 times.

#python code to print the simplest number pattern in Python for r in range (1,5): print(str(r) * r)

#### Method 2: Simplest Number Pattern in Python using Two for Loops

Code to print the number pattern in Python using two loops. This is the most traditional method, and it is a must to understand this method to write other number pattern programs in Python. Here we will write one loop to travel along row and column. The print statement is significant.

First, “**print**” with the end character as space ensures that the number is printed in the same line when the inner loop in Python is running. The second print statement gives the next line after all the numbers in a single line are printed.

#python code to print the simplest number pattern in Python Method 2 using two loops for r in range(1,5): for c in range(1,r+1): print(r, end=' ') print()

### Pattern 2: Inverted Pyramid of Numbers Pattern in Python

This example is the same as above to some extent, but it’s reversed. The first line below will print the maximum time, while the last line will have only 1 digit. It will look like this.

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

We can print the above pattern using a single-loop or two-loop. We will see and understand both methods to print number pattern programs in Python.

#### Method 1: Inverted Pyramid of number pattern program in python using for loop

Since we need to print 5 numbers in one line, the loop will start from 5 and go to 0. After that, we will use the string multiplication feature of Python. In the range function, the first parameter is the initial value of the variable “r”, 0 is the last value, and -1 is the step, i.e. decrement.

#python code to print Inverted Pyramid of Same Numbers in Python - 1 Loop for r in range (5,0,-1): print(str(r) * r)

#### Method 2: Inverted Pyramid of Same Numbers in Python using Two for Loop

In this method, we will use two for loops. The first loop will travel from 5 to 1. Since the loop starts from 5, the second loop will print numbers 5 times. But it will print the same number, i.e. variable “r.”

#python code to print Inverted Pyramid of Same Numbers in Python - 2 for Loop for r in range (5,0,-1): for c in range(1,r+1): print(r, end=' ') print()

### Pattern 3: Pyramid of Incremental Numbers Pattern in Python

In the last two examples, we printed number patterns with the same number in each line. But in this example, we will print the pyramid, but the number will keep increasing in the same line. It will start again with 1 in the next line.

#### Code to Print Number Pattern Program – Pyramid of Incremental Numbers

#python code to print Pyramid of Incremental Numbers in Each Line for r in range (6): for c in range(1,r+1): print(c, end=' ') print()

##### Output:

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

### Pattern 4: Inverted Pyramid of Incremental Numbers in Each Line – Python

Well, this is the same as example number 3, but in reverse order. So our first loop will start from 6 and go till 0 with the step in the loop as -1. The inner loop will print incremental numbers until the value of “r” is the counter of the first loop. But always remember to add the print statement after the inner loop since it ensures the next line after the inner loop.

**The code to print the above number pattern programs in python is this.**

#python code to print Inverted Pyramid of Incremental Numbers in Each Line for r in range (5,0,-1): for c in range(1,r+1): print(c, end=' ') print()

**Output:**

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

### Pattern 5: Pyramid of Decrementing Numbers in Each Line – Python

Code to print Pyramid of Decrementing Numbers in Each Line – Python

#python code to print Pyramid of Decrementing Numbers in Each Line - Python for r in range (6): for c in range(r,0,-1): print(c, end=' ') print()

**Output:**

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

### Pattern 6: Inverted Pyramid of Decrementing Numbers in Each Line – Python

Code to print an Inverted Pyramid of Decrementing Numbers in Each Line – Python

#python code to print Inverted Pyramid of Decrementing Numbers in Each Line - Python for r in range (5,0,-1): for c in range(r,0,-1): print(c, end=' ') print()

**Output:**

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

### Pattern 7: Pyramid of Incremental Numbers till the End up to 10 in Python

We have written number pattern programs in python that reinitialize the sequence after every new line. But now, we will write a program that will print a pyramid with a number counter increasing even if the line changes, exactly like the pattern below.

Here is the trick to declare a third variable called a “number”. This will increase by 1 every time a loop iteration is executed. Instead of the counter of the loop, we will print this third variable.

**Code to print Pyramid of Incremental Numbers till the End in Python**

#python code to print Pyramid of Incremental Numbers till the End in Python number = 1 for r in range (5): for c in range(1,r + 1): print(number, end=' ') number = number + 1 print()

**Output:**

```
1
2 3
4 5 6
7 8 9 10
```

### Pattern 8: Pyramid of Decrementing Numbers till the from 10 in Python

#python code to print Pyramid of Decrementing Numbers till the End in Python number = 10 for r in range (5): for c in range(1,r + 1): print(number, end=' ') number = number - 1 print()

**Output:**

```
10
9 8
7 6 5
4 3 2 1
```

### Patterns 9: Pyramid of Even Numbers Pattern in Python

Well, you can convert any pattern into odd and even. You can write any program with odd and actual numbers just by changing the Initialization, increment, or printing factor that step in Python. Let’s try the most straightforward pattern program to print even numbers in Python. In this example, we are not changing any part of the Initialization or increment but just the printed factor. We just multiplied the printing variable by 2. Code to print Pyramid of Even Numbers.

#python code to print Pyramid of Even Numbers for r in range (5): for c in range(1,r + 1): print(r * 2, end=' ') print()

**Output:**

```
2
4 4
6 6 6
8 8 8 8
```

### Patterns 10: Pyramid of Odd Numbers Pattern (Logic is totally Different)

The logic for the Even number was elementary. But this is not the case with Odd Numbers. We just can’t multiply to get the odd numbers as we did in the patterns for Even Numbers. Logic is a bit tricky over here.

So in the number pattern program of Python, we will first write the main loop with initializing values as 1 and step 2. So the counter “r” will have values 1,3,5,7, and so on. These are nothing but our odd numbers.

In the inner loop, the max range parameter is essential. The formula will be int(r/2))+2, where r is the row count of the first loop. And the “int” function is to convert any decimal value into an integer. Since division is involved, we need to take care of any possible decimal value.

Int (r/2))+2, So if the value of r is one, this function will return 2, and hence the inner loop will run 1 time. If the value of r is 3, this formula will give the value 3, and hence the inner loop will run 2 times. This process will go on until the main loop breaks. This is what the pattern will look like.

**Code to print Pyramid of Odd Numbers**

#python code to print Pyramid of Odd Numbers for r in range (1,11,2): for c in range(1,int(r/2) + 2): print(r, end=' ') print()

**Output:**

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

While writing any program to print number patterns, logic and approach is very important. Let’s assume a simple example and see how to approach the problem of writing any number pattern program in Python. For Explanation, we will assume this simple example of a Number Pattern.

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

### Explanation of the code

**Writing the Inner Loop**: Never approach the problem from the top but the base. Looking at the above pattern, we need to print 1 in the first line and two numbers, 1 and 2, in the second line. 3 Numbers 1,2,3 in the third line and so on. So Write a loop that prints 1 to n in a line.`for j in range(1,6):`

print(j , end = ' ')

We have added the end character as space so that it will print the number in the same line.**Write the Main Loop**: If you look at the pattern, we will need to print these numbers in five different lines. So we will include this loop from step 1, inside the main loop. And also, remembers after the inner loop has done its work for the line, we need the next link. For this, we will add a blank print statement link below the code.`for i in range (1,6):`

for j in range(1,5):

print(j , end = ' ')

print()

If you run this code, you will get this output.

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

Well, this is not right; to correct it implement the third step.**Make an inner loop depending on the Main Loop**: In the above step, the range of the inner loop is 5; hence it prints 5 numbers in each line. We need to change the range to i + 1, i.e. the counter of the main loop. When the value of i is a will, the inner loop will run 1 time, and when the value of i is 2, the inner loop will run two times and so on.

Here is the complete code to print number pattern`for i in range (1,6):`

for j in range(1,i+1):

print(j , end = ' ')

print()

## Tips and Tricks for Optimizing Number Pattern Programs

Optimizing number pattern programs is crucial for improving efficiency and performance, here are some tips and tricks for optimizing number pattern programs in Python:

- Choose the Right Algorithm:

Selecting the appropriate algorithm for generating a specific number pattern is the first step towards optimization. Consider the characteristics of the pattern and choose an algorithm with the lowest time complexity for optimal performance. - Reduce Time Complexity:

Analyze the time complexity of your algorithm and look for opportunities to reduce it. For example, if you’re using nested loops to generate a pattern, consider whether you can achieve the same result with a single loop or a more efficient algorithm. - Minimize Memory Usage:

Be mindful of memory usage, especially when working with large datasets or recursive algorithms. Avoid unnecessary variable declarations or data structures that consume memory. Use generators or iterators instead of lists to save memory space. - Implement Memoization:

Memoization is a technique used to store the results of expensive function calls and reuse them when the same inputs occur again. Implement memoization in recursive algorithms to cache intermediate results and reduce redundant computations, improving both time complexity and memory usage. - Optimize Loop Structures:

Streamline loop structures by minimizing the number of iterations and eliminating unnecessary operations. Use slicing or built-in functions like`range()`

to generate sequences more efficiently. Additionally, consider using bitwise operators for arithmetic operations if applicable, as they can be more efficient than traditional arithmetic operations. - Write Clean and Readable Code:

Maintain readability and understandability by following best practices for code organization and documentation. Use meaningful variable names, add comments to explain complex logic or algorithms, and adhere to consistent coding style conventions. Clean and well-documented code not only improves maintainability but also makes it easier to identify potential areas for optimization.

Our blog post on ‘Number Patter Program in Python’’ able to provide valuable information about Python Programming Language. You can also visit our **Newtum** website for more information on various courses and blogs about PHP, C Programming for kids, Java, and more. Happy coding!