×

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

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Adding a key-value pair to dictionary in Python

Python dictionaries are collections of unsorted key-value pairs. This article will examine a method for adding new key-value teams to an existing dictionary. Dictionary in Python With the aid of curly brackets...

3 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Sentiment Analysis using NLTK

Introduction Data is being produced at an astounding rate and volume in the field of the internet and other digital services nowadays. Researchers, engineers, and data analysts often work with tabular...

7 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

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

Python program to find the area of the triangle

Python program to find the area of the triangle This article will discuss how to find the area of a triangle in Python with all three given sides. The area of...

2 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

Python sum() function

Python sum() function The sum() function in python sums start and the items of an iterable from left to right and returns the total.  Syntax sum(iterable[, start]) Parameter iterable: This parameter represents the sequence to sum. start: It is optional...

1 minute read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.