More
Vired Library
Python is extremely user-friendly and enables users to become good at programming by simplifying the coding process. Users can easily access different Python libraries with modules containing program codes that can structure a framework. That's what makes Python extremely popular among programmers.
Python encodes programs in different formats and shapes to develop recognized patterns. These patterns comprise different code combinations to help programmers improve their skills. In this article, we will specifically learn about pattern program in python.
The different benefits of using the pattern program in Python are as follows:
Enroll: Data Science and AI Course
You will come across various types of patterns program in Python. Keep scrolling to learn about some of them.
Let’s see the explanation of Pattern Program in Python with example:
Example:
# This is the example of print simple pyramid pattern n = int(input("Enter the number of rows")) # outer loop to handle number of rows for i in range(0, n): # inner loop to handle number of columns # values is changing according to outer loop for j in range(0, i + 1): # printing stars print("* ", end="") # ending line after each row print() Outcome: * * * * * * * * * * * * * * *
Explanation:
The above code involved initializing the n variable. It helped enter the number of rows for printing the pattern in Python. The value of n was chosen as 5, and the outer loop ranged between 0 to 4.
The outer loop influences the interaction of the inner for loop. The inner loop remains responsible for printing the number of columns.
In the first iteration, i equals 0, and it gets increased by 1. Therefore, it becomes 0+1. So the inner loop will be iterated for the first time to print one star (*).
In the second iteration, the value of i will be 1. If it is increased by 1, it will become 1+1. Therefore, the inner loop will be iterated twice, and two stars will get printed.
The final argument won't jump into another line. It will continue printing the star until the loop becomes valid.
The last print statement will ensure each line after a row ends.
Let’s see the explanation of Pattern Program in Python with example:
Code: # This is the example of print simple reversed right angle pyramid pattern rows = int(input("Enter the number of rows:")) k = 2 * rows - 2 # It is used for number of spaces for i in range(0, rows): for j in range(0, k): print(end=" ") k = k - 2 # decrement k value after each iteration for j in range(0, i + 1): print("* ", end="") # printing star print("") Outcome: * * * * * * * * * * * * * * *
Let’s see the explanation of Pattern Program in Python with example:
Code: rows = int(input("Enter the number of rows: 5")) k = 0 # Reversed loop for downward inverted pattern for i in range(rows, 0, -1): # Increment in k after each iteration k += 1 for j in range(1, i + 1): print(k, end=' ') print() Outcome: Enter the number of rows: 5 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
Explanation:
The reversed loop is useful for inverted pyramid pattern printing in Python. In this type of pattern program in Python, the number will reduce after every iteration.
Find out: Top 10 Python Libraries You Must Know In 2023
Let’s see the explanation of Pattern Program in Python with example:
Code: rows = int(input("Enter the number of rows: ")) k = rows - 2 # This is used to print the downward pyramid 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() # This is used to print the upward pyramid k = 2 * rows - 2 for i in range(0 , rows+1): for j in range(0 , k): print(end="") k = k - 1 for j in range(0, i + 1): print("* ", end="") print() Outcome: Enter the number of rows: 5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Explanation:
Hourglass pattern printing in Python is primarily based on logic and proper usage of loops.
Understand: Tuple in Python: Function with Example
You can use different pattern programs in Python for printing numbers. Some popular ones are as follows:
Code: rows = int(input("Enter the number of rows: ")) # Outer loop will print number of rows for i in range(rows+1): # Inner loop will print the value of i after each iteration for j in range(i): print(i, end=" ") # print number # line after each row to display pattern correctly print(" ") Outcome: Enter the number of rows: 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Explanation:
The above code revolved around printing numbers according to row value. The first row contains only one number. The second row contains two numbers, the third row contains three numbers, and so on.
Code: current_Number = 1 stop = 2 rows = 3 # Number of rows to print numbers for i in range(rows): for j in range(1, stop): print(current_Number, end=' ') current_Number += 1 print("") stop += 2 Outcome: 1 2 3 4 5 6 7 8 9
Code: rows = int(input("Enter the number of rows: ")) i = 1 # outer file loop to print number of rows while i <= rows: j = 1 # Check the column after each iteration while j <= i: # print odd values print((i * 2 - 1), end=" ") j = j + 1 i = i + 1 print() Outcome: Enter the number of rows: 5 1 3 3 5 5 5 7 7 7 7 9 9 9 9 9
Learn: What Is a List in Python: Functions with Examples
Every letter comes with a different ASCII value. You will have to define every character to get it printed on the screen. Find out how to print a right-angled pattern with characters:
Code: print("The character pattern ") asciiValue = 65 #ASCII value of A for i in range(0, 5): for j in range(0, i + 1): # It will convert the ASCII value to the character alphabate = chr(asciiValue) print(alphabate, end=' ') asciiValue += 1 print() Outcome: The character pattern A B C D E F G H I J K L M N O
Explanation:
The ASCII value of A is assigned an integer value of 65. Next, a loop was defined to print five rows. The char() function was used to convert the ASCII value into the character. It helped print the characters while increasing the asciiValue after every iteration.
Recruiters might ask you about a pattern program in Python to test your knowledge of the programming language. Therefore, it's crucial for you to possess adequate knowledge about the different patterns program in Python. You can begin with simple pattern printing in Python before moving ahead to the difficult ones.
Blogs from other domain
Carefully gathered content to add value to and expand your knowledge horizons