Printing Patterns in Python5 Jan 2025 | 17 min read Pattern questions are very common in programming. These questions help in getting familiar with the flow of the programs and also help in understanding how to write programs to get the desired output. Patterns are printed using loops. We generally use nested loops to print a pattern. The outer loop defines the number of rows of the pattern. At the same time, the inner loop defines the pattern we need. We put conditions in the inner loop. In this tutorial, we will see programs to print different kinds of patterns. There can be more than one way to print a pattern, so there is no need to follow one strict approach. Simple Pyramid PatternApproach - 1Code Output: * * * * * * * * * * * * * * * Time Complexity: The time complexity of this approach is non-linear. We have used nested loops to print the pattern. The outer loop will iterate n times; hence, the time complexity will be O(n). However, the inner loop will iterate from 0 to n times, which makes the worst time complexity of the inner loop O(n). The total time complexity of this program is O(n2). Space Complexity: The space complexity of this approach is O(1). We have not used any auxiliary space to print the pattern. Approach - 2We can print the same pattern using a list, which simplifies the code. Code Output: * * * * * * * * * * * * * * * Time Complexity: The time complexity of this approach is linear because we have used a single loop to print the pattern. Therefore, using a list is better than using nested loops. The join function has a time complexity of O(n); however, when taken in total, the complexity will be O(n) + O(n). The average time complexity of this approach is O(n). Space Complexity: The space complexity of this approach is linear because we have used a list to store the pattern. Therefore, the space complexity of this approach is O(n) Approach - 3We will use the recursion method to print the pattern. Code Output: * * * * * * * * * * * * * * * Approach - 4This time, we will use the while loop to print the pattern. Code Output: * * * * * * * * * * * * * * * Time Complexity: The time complexity of this approach is O(n ^ 2). We have used nested loops to print the pattern. Space Complexity: The space complexity of this approach is O(1) since we have not used any extra space to store the pattern. Rotating the PatternNow, we will flip the pattern and design the programs to print the pattern. Approach - 1Code Output: * * * * * * * * * * * * * * * Approach - 2Now, we will see a better approach to printing the previous pattern In this pattern, we must print spaces (number_from_base - row_number) and then the stars row_number times. This time, we will use the while loop to print the pattern. We will see that we can print the pattern using a single while loop and hence don't require nested loops. This will decrease the time complexity from non-linear to linear time complexity. Below is the Python code for the implementation of this approach Code Output: * * * * * * * * * * * * * * * Approach - 3We will write the previous program using the for loop. Code Output: * * * * * * * * * * * * * * * Printing TriangleApproach - 1Code Output: * * * * * * * * * * * * * * * Number PatternNow, we will see how to print the pattern using numbers. Approach - 1Code Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Approach - 2In this pattern, we do not have to start the pattern with 1 in each row. Code Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Approach - 3In this pattern, we will print a triangle using the numbers. Code Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Printing AlphabetsPattern - 1We will print patterns using alphabets. We will update the ASCII code of the letters to print alphabet patterns. The reason to use ASCII code is to increment the characters. Code Output: A B B C C C D D D D E E E E E Pattern - 2Instead of starting with an A in each row, this program will print the alphabet continuously. Compared with the previous algorithm, the change will be in the increment of the character ASCII code. In the previous code, we incremented the code value after the completion t=of the inner loop, i.e., in the outer loop. This algorithm will increment the code value for every row. In this algorithm, we will increment the code value in the inner loop, hence moving to the next character for each column of each row. Code Output: A B C D E F G H I J K L M N O Pattern - 3In this pattern, we will print a triangle of characters. Code Output: A A B A B C A B C D A B C D E Complex PatternsPattern - 1This program is divided into four parts. Code Output: ![]() Pattern - 2Code Output: ![]() Pattern - 3Code Output: ![]() Pattern - 4Code Output: \*******/ *\*****/* **\***/** ***\*/*** ****/**** ***/*\*** **/***\** */*****\* /*******\ Pattern - 5Code Output: 4 3 2 1 0 3 2 1 0 2 1 0 1 0 0 Pattern - 6Code Output: 1 7 2 12 8 3 16 13 9 4 19 17 14 10 5 21 20 18 15 11 6 Print G PatternWe shall discover the procedures if we attempt to analyze this image using a (row, column) matrix, where the circles denote the locations of the stars in the pattern G. In this case, the procedures are carried out column-by-column. Therefore, we set the first if condition for the first line of stars, which states that all rows from 1 to (n-1) will receive the stars and that rows with 0 and (n-1) will not. Similarly, we want stars in the second, third, and fourth columns at row = 0 and row = (n-1). The remaining steps are self-explanatory and are clear from the diagram's row and column placement. Below is the implementation of this approach in Python. Code Output: ![]() |
? Summing the values in a Python dictionary is a common task that can be approached in multiple ways depending on the context and requirements. A dictionary in Python is a collection of key-value pairs where each key is unique and maps to a value. Often,...
16 min read
Introduction Data systems and algorithms are the essential constructing blocks of pc technology and programming. They are crucial for efficient problem-solving, software development, and building strong programs. Python, regarded for its simplicity and flexibility, is a popular language choice for each novice and skilled programmers. If...
6 min read
Find the Greater Frequency Element In this tutorial, we will learn to write the Python program to find the greater frequency element. We will solve this problem using the various approaches. Let's understand the problem statement. We are given an array, and required to determine...
12 min read
? Introduction Python's Unicode (UTF-eight) studying and writing capabilities cope with textual content encoded in a format that helps a huge variety of languages and characters. A popular Unicode encoding widespread that works with lots of devices and structures is UTF-8. Using Python's open() technique and the...
4 min read
Python is a lot more efficient in coping with time and its complexity, and it may forecast time-series statistics, get actual-time records, or every other related hassle with time. The ctime module is a vital module in Python to deal with time-associated troubles. This article will...
3 min read
. Introduction: In the world of web development and API interactions, sending HTTP POST requests is a fundamental skill. Python, being a versatile programming language, provides the requests library that simplifies the process of making HTTP requests. In this article, we'll delve into the details of performing...
4 min read
Pandas is a powerful and open-source Python library that is used for manipulating data and is useful in performing data analysis tasks; pandas provide data structures and functions that are very helpful in performing data analysis tasks. Pandas is built on top of the NumPy...
6 min read
? If you're working with data, machine learning, or scientific computing on a Windows machine, installing NumPy is a crucial step. In this comprehensive guide, we'll walk you through the process of installing NumPy on Windows, covering various methods, and troubleshooting tips. Why NumPy? Before we dive into...
3 min read
Python is among the most used and most famous programming languages in the world. It was created by Guido van Rossum and first released in 1991. Python is free to use and open-source. Python has a simple syntax that is well-organized and makes it easy...
10 min read
? Understanding Histograms Histograms are graphical portrayals of the dissemination of mathematical information. They give a visual rundown of the recurrence or thickness of information values inside unambiguous stretches, frequently alluded to as "containers." Histograms are generally utilized in information examination and representation to investigate the fundamental...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India