Python Tutorial

Introduction Python Features Python Applications System requirements for Python Python Installation Python Data Types Python IDE Python Keywords Python Operators Python Comments Python Pass Statement

Python Conditional Statements

Python if Statement Python elif Statement Python Switch Case

Python Loops

Python for loop Python while loop Python Break Statement Python Continue Statement Python Goto Statement

Python Arrays

Python Array Python Matrix

Python Strings

Python Strings Python Regex

Python Built-in Data Structure

Python Lists Python Tuples Python Dictionary Python Sets

Python Functions

Python Function Python min() function Python max() function Python User-define Functions Python Built-in Functions Anonymous/Lambda Function in Python

Python File Handling

Python File Handling Python Read CSV Python Write CSV Python Read Excel Python Write Excel Python Read Text File Python Write Text File Read JSON File in Python

Python Exception Handling

Python Exception Handling Python Errors and exceptions Python Assert

Python OOPs Concept

OOPs Concepts in Python Inheritance in Python Polymorphism in Python Python Encapsulation Python Constructor

Python Iterators

Iterators in Python Yield Statement In Python

Python Generators

Python Generator

Python Decorators

Python Decorator

Python Functions and Methods

Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods

Python Modules

Python Modules Python Datetime Module Python Calendar Module  

Python MySQL

Python MySQL Python MySQL Update Operation Python MySQL Delete Operation

Python MongoDB

Python MongoDB

Python Data Structure Implementation

Python Stack Python Queue Python Hash Table Python Graph

Python Advance Topics

Speech Recognition in Python Face Recognition in Python Python Rest API Python Command Line Arguments Python JSON Python Virtual Environment Type Casting in Python Collections in python Python Enumerate Python Debugger Python DefaultDict

Misc

Python PPTX Python Pickle Python Seaborn Python Coroutine Python EOL Python Infinity Python math.cos and math.acos function Python Project Ideas Based On Django Reverse a String in Python Reverse a Number in Python Python Word Tokenizer Python Trigonometric Functions Python try catch exception GUI Calculator in Python Implementing geometric shapes into the game in python Installing Packages in Python Python Try Except Python Sending Email Socket Programming in Python Python CGI Programming Python Data Structures Python abstract class Python Compiler Python K-Means Clustering List Comprehension in Python3 NSE Tools In Python Operator Module In Python Palindrome In Python Permutations in Python Pillow Python introduction and setup Python Functionalities of Pillow Module Python Argmin Python whois Python JSON Schema Python lock Return Statement In Python Reverse a sentence In Python tell() function in Python Why learn Python? Write Dictionary to CSV in Python Write a String in Python Binary Search Visualization using Pygame in Python Latest Project Ideas using Python 2022 Closest Pair of Points in Python ComboBox in Python Python vs R Python Ternary Operators Self in Python Python vs Java Python Modulo Python Packages Python Syntax Python Uses Python Logical Operators Python Multiprocessing Python History Difference between Input() and raw_input() functions in Python Conditional Statements in python Confusion Matrix Visualization Python Python Algorithms Python Modules List Difference between Python 2 and Python 3 Is Python Case Sensitive Method Overloading in Python Python Arithmetic Operators Design patterns in python Assignment Operators in Python Is Python Object Oriented Programming language Division in Python Python exit commands Continue And Pass Statements In Python Colors In Python Convert String Into Int In Python Convert String To Binary In Python Convert Uppercase To Lowercase In Python Convert XML To JSON In Python Converting Set To List In Python Covariance In Python CSV Module In Python Decision Tree In Python Difference Between Yield And Return In Python Dynamic Typing In Python Abstract design pattern in python Builder design pattern in python

How to

How to convert integer to float in Python How to reverse a string in Python How to take input in Python How to install Python in Windows How to install Python in Ubuntu How to install PIP in Python How to call a function in Python How to download Python How to comment multiple lines in Python How to create a file in Python How to create a list in Python How to declare array in Python How to clear screen in Python How to convert string to list in Python How to take multiple inputs in Python How to write a program in Python How to compare two strings in Python How to create a dictionary in Python How to create an array in Python How to update Python How to compare two lists in Python How to concatenate two strings in Python How to print pattern in Python How to check data type in python How to slice a list in python How to implement classifiers in Python How To Print Colored Text in Python How to develop a game in python How to print in same line in python How to create a class in python How to find square root in python How to import numy in python How to import pandas in python How to uninstall python How to upgrade PIP in python How to append a string in python How to open a file in python

Sorting

Python Sort List Sort Dictionary in Python Python sort() function Python Bubble Sort

Programs

Factorial Program in Python Prime Number Program in Python Fibonacci Series Program in Python Leap Year Program in Python Palindrome Program in Python Check Palindrome In Python Calculator Program in Python Armstrong Number Program in Python Python Program to add two numbers Anagram Program in Python Even Odd Program in Python GCD Program in Python Python Exit Program Python Program to check Leap Year Operator Overloading in Python Pointers in Python Python Not Equal Operator Raise Exception in Python Salary of Python Developers in India What is a Script in Python Singleton design pattern in python

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



ADVERTISEMENT
ADVERTISEMENT