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

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

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

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

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

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

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

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

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

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

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

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

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

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
