Last Updated on May 12, 2023 by Prepbytes
Python is a highly legible, interactive, high-level, object-oriented, and interpreted scripting language. Python uses English phrases rather than punctuation and has fewer syntactic structures than other computer languages. You may learn how to print the patterns seen below in Python by following this tutorial.
You may have a firm grasp of Python loops by printing various patterns. You may make a variety of designs after reading this tutorial.
Steps to Print Pattern in Python
Use the below steps to print pattern in Python
-
Decide the number of rows and columns
Using a standard format, which includes the number of rows and columns, any design may be printed. We need to employ two loops, also known as nested loops, to print any pattern. While the outer loop supplies the number of rows, the inner loop tells us the column needed to print the pattern. Get the user’s input on how many rows to allow before deciding the size of a pattern by using the input() method. -
Iterate rows
Then, use a for loop and the range() function to build an outer loop to iterate the number of rows. -
Iterate columns
Next, write the inner loop or nested loop to control the number of columns. The inner loop’s iteration is influenced by the outcomes of the outer loop iteration. -
Print star or number
Use the print() function in each iteration of the nested for loop to display the symbol or number of a pattern (like a star (asterisk *) or number). -
Add a new line after each iteration of the outer loop
For the pattern to show properly, add a new line using the print() method after each iteration of the outer loop.
Algorithm and Explanation of Pattern Program in Python
Pattern program in python
Pattern 1
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Python Program for Pattern
rows = 6 for i in range(rows): for j in range(i): print(i, end=' ') print('')
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Explanation:
In this number pattern, the first row had a single digit, the second row contained the next two digits, the third row contained the following three numbers, and so on until the desired number of rows was reached.
Pattern 2
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
rows = 5 b = 0 for i in range(rows, 0, -1): b += 1 for j in range(1, i + 1): print(b, end=' ') print('\r')
Output
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
Pattern 3(Pascal triangle pattern)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
def print_pascal_triangle(size): for i in range(0, size): for j in range(0, i + 1): print(decide_number(i, j), end=" ") print() def decide_number(n, k): num = 1 if k > n - k: k = n - k for i in range(0, k): num = num * (n - i) num = num // (i + 1) return num rows = 7 print_pascal_triangle(rows)
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Pattern 4(Multiplication table pattern)
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
rows = 8 for i in range(1, rows + 1): for j in range(1, i + 1): square = i * j print(i * j, end=' ') print()
Output
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
Pattern 5(Downward star pyramid pattern)
* * * * * *
* * * * *
* * * *
* * *
* *
*
rows = 5 k = 2 * rows - 2 for i in range(rows, -1, -1): for j in range(k, 0, -1): print(end=" ") k = k + 1 for j in range(0, i + 1): print("*", end=" ") print("")
Output
* * * * * *
* * * * *
* * * *
* * *
* *
*
Conclusion:
Python pattern printing requires knowledge of loops and practical application of programming theory. To comprehend Python and its use in industries like data science, it may be necessary to consult more in-depth ideas and sources. One such program that will help you learn Python from A to Z and its applications and implementation in data science are PrepBytes.
FAQs related to Pattern Programs in Python
Q1. What is a pattern program?
Ans. Pattern programs are nothing more than patterns made up of letters, numbers, or symbols in a specific order. The use of loop conditions makes it simple to solve these sorts of pattern programs. 30 of the C, C++, and Java pattern programs with the most frequently asked questions are shown in the following paragraphs.
Q2. What is the use of pattern programming?
Ans. The easiest way to explore how looping structures operate in general-purpose programming languages is by using pattern programs. It aids novices in understanding how each iteration of a loop operates.
Q3. What are the three types of patterns?
Ans. There are three types of patterns:
- Shape Pattern
- Number Pattern
- Letter Pattern
Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Check Palindrome Number
Python Program to Print the Fibonacci Series
Python Loop Program
Anagram Program in Python
Fizzbuzz Program in Python
String Programs in Python
List Program in Python
Prime Number Program in Python