×

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

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Is Python Object Oriented Programming language

In this tutorial, we will see whether python also belongs to an object-oriented programming language like several others. We will also see the advantages of using OOPs. Further, we will...

4 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 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.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.