×

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to view different patterns.

In this section, we have created a different kind of pattern using numbers. Let’s see a few different examples:

Steps to the print pattern in Python

The following steps are used to print pattern in Python:

  • Decide the number of rows and columns

Any pattern has a standard form, which includes the number of rows and columns. To print any pattern, we must use two loops or nested loops. The number of rows is determined by the outer loop, while the inner loop determines the column required to print the pattern.

  • Iterate rows and columns

Write an outer loop to iterate the rows using for loop and the range() function. The inner loop for handling the number of columns will be written next. The values of the outer loop determine the iteration of the internal loop.

  • Print number

To show the number of a pattern, use the print() function in each iteration of the nested for loop.

  • Add a new line after each iteration of the outer loop

After each iteration of the outer loop, use the print() function to add a new line so that the pattern displays properly.

Example 1: Number pattern

#Program to print the pattern using the number  
#Declare number of rows    
 rows = int(input("Enter number of rows:"))    
 #Outer loop will print number of rows   
 for m in range(0, rows+1): 
     #Inner loop will print value of m    
     for n in range(0, m):           
         print(m,end=" ") #Print number     
     print("\r")  

Output

Number pattern in Python

Explanation: In this program, we have printed the number according to the row value. A first-row print single value of 1, second-row print two value of 2, and so on. In this program, we have taken two loops. The outer for loop will iterates from 0 to rows + 1 and print the same number of rows as a number of iterations. The inner loop will print the value of m.

Example 2: Half-pyramid number pattern

 #Program to print half pyramid using the number 
 #Declare number of rows   
 rows = int(input("Enter number of rows:"))    
 #Outer loop will print number of rows  
 for m in range(1, rows+1):      
     #Inner loop will print number of column    
     for n in range(1, m+1):      
         print(n,end=" ")  
     print("\r")     

Output

Python Program Half-pyramid number pattern

Explanation: In this program, we have printed the column value in the inner for loop.

Example 3: An Inverted Pyramid Pattern with Numbers

 #Declare number of rows      
 rows = int(input("Enter number of rows:"))    
 x = 0    
 #Reverse for loop for this pattern   
 for i in range(rows, 0, -1):    
     x += 1     
     for j in range(1, i + 1):     
         print(x, end=" ")     
     print('\r')     

Output

Python Program: An Inverted Pyramid Pattern with Numbers

Explanation: In the above code, we have used a reversed for loop that iterates from the number of rows to 0 will decrease by 1.

Example 4: An Inverted pyramid with the same number

 #Declare number of rows  
 rows = int(input("Enter number of rows:"))     
 x = rows       
 #Reverse for loop for this pattern      
 for i in range(rows, 0, -1):     
     for j in range(0, i):   
         print(x, end=" ")   
     print('\r')      

Output

Python Program: An Inverted pyramid with the same number

Example 5: Display number in decreasing order

 #Declare number of rows      
 rows = int(input("Enter number of rows:"))   
 #Reverse for loop for this pattern    
 for i in range(rows, 0, -1):        
     #Assign value of i to x   
     x = i       
     for j in range(0, i):      
         print(x, end=" ")  
     print('\r')     

Output

Python Program: Display number in decreasing order

Example 6: Inverted half pyramid with the same number

 #Declare number of rows 
 rows = int(input("Enter number of rows:"))       
 for i in range(rows, 0, -1):                  
     for j in range(0, i+1):        
         print(j, end=" ")    
     print('\r')      

Output

Python Program: Inverted half pyramid with the same number

Example 7: Reverse number pattern

 #Declare number of rows 
 rows = int(input("Enter number of rows:"))     
 for i in range(1, rows+1):  
     for j in range(i, 0, -1):     
         print(j, end=" ")    
     print('\r')      

Output

Python Programs: Reverse number pattern

Example 8: Display 1 to 10 number in a pattern

 currentNumber = 1     
 stop = 2     
 rows = 3  # Rows we want in your pattern    
 for i in range(rows):             
     for column in range(1, stop):         
         print(currentNumber, end="")   
         currentNumber += 1       
     print("")         
     stop += 2      

Output

Python Programs: Display 1 to 10 number in a pattern

Type 2:

 currentNumber = 1  
 stop = 2 
 rows = 4  # Rows we want in your pattern     
 for i in range(rows):        
     for column in range(1, stop):      
         print(currentNumber, end=' ')  
         currentNumber += 1          
     print("")       
     stop += 1          

Output

Display 1 to 10 number in a pattern

Example 9: Reverse number pattern

 #Declare number of rows   
 rows = int(input("Enter the number of rows:"))     
 for i in range(0, rows + 1):  
     for j in range(rows - i, 0, -1):               
         print(j, end=" ")  
     print("")  

Output

Python Programs: Reverse number pattern

Example 10: Reverse number from 10-1

 start = 1      
 stop = 2    
 currentNumber = stop    
 for i in range(2, 6):           
     for j in range(start, stop):     
         currentNumber -= 1         
         print(currentNumber, end=" ")          
     print("")             
     start = stop      
     stop += i        
     currentNumber = stop        

Output

Python Program: Reverse number from 10-1

Example 11: Print alternate number

 #Declare number of rows    
 rows = int(input("Enter the number of rows:"))   
 count = 1                                  
 while count <= rows:               
     j = 1         
     while j <= count:        
         print((count * 2 - 1), end=" ")       
         j = j + 1     
     count = count + 1        
     print()       

Output

Python Programs: Print alternate number

Example 12: Square pattern with a number

 #Declare number of rows    
 rows = int(input("Enter the number of rows:"))    
 for i in range(1, rows + 1):     
     for j in range(1, rows + 1):       
         if j <= i:      
             print(i, end=' ')          
         else:                     
             print(j, end=' ')     
     print()   

Output

Python Programs: Square pattern with a number

Example 13: Right-angled triangle pattern with numbers

 #Declare number of rows
 rows = int(input("Enter the number of rows:")) 
 for i in range(1, rows+1):   
     num = 1 
     for j in range(rows, 0, -1): 
         if j > i:      
             print(" ", end=' ') 
else:      
             print(num, end="") 
 num += 1   
print("")  

Output

Python Programs: Right-angled triangle pattern with numbers

Related Topics

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Wikipedia module in Python

Wikipedia module in Python: The internet has become an essential part of all of our life which is the largest source of all the information. Therefore, it becomes very important...

7 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.