Understanding loops in Python
- Post by: Shubham Saurabh
- September 16, 2021
- No Comment
Patterns can be printed in python using simple for loops. The first outer loop is used to handle number of rows and the Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed.
By printing different patterns, you can build a solid understanding of loops in Python. After reading this article you can create various types of patterns.
Steps to Print Pattern in Python
Us the below steps to print pattern in Python
-
Decide the number of rows and columns
There is a typical structure to print any pattern, i.e., the number of rows and columns. We need to use two loops to print any pattern, i.e. nested loops.
The outer loop tells us the number of rows, and the inner loop tells us the column needed to print the pattern.
Accept the number of rows from a user using the
input()
function to decide the size of a pattern. -
Iterate rows
Next, write an outer loop to Iterate the number of rows using a for loop and
range()
function. -
Iterate columns
Next, write the inner loop or nested loop to handle the number of columns. The internal loop iteration depends on the values of the outer loop.
-
Print star or number
Use the
print()
function in each iteration of nestedfor
loop to display the symbol or number of a pattern (like a star (asterisk*
) or number). -
Add new line after each iteration of outer loop
Add a new line using the
print()
function after each iteration of the outer loop so that the pattern display appropriately
Some of the Patterns are shown for the better understanding.
Let’s start with the easiest of all.
Right Half-Pyramid Pattern Program
* * * * * * * * * * * * * * *
Code
# Function to demonstrate printing pattern def righthalf(n): # outer loop to handle number of rows # n in this case for i in range(0, n): # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # printing stars print("* ",end="") # ending line after each row print("\r") # Driver Code n = 5 righthalf(n)
Let’s look at another example.
Left Half-Pyramid Pattern Program
* * * * * * * * * * * * * * *
Code
# Function to demonstrate printing pattern def lefthalf(n): # number of spaces k = 2*n - 2 # outer loop to handle number of rows for i in range(0, n): # inner loop to handle number spaces # values changing acc. to requirement for j in range(0, k): print(end=" ") # decrementing k after each loop k = k - 2 # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # printing stars print("* ", end="") # ending line after each row print("\r") # Driver Code n = 5 lefthalf(n)
Now, combining both the patterns we can make full pyramid star pattern.
Full Pyramid Star Pattern
* * * * * * * * * * * * * * *
Code
# Function to demonstrate printing pattern triangle def pyramid(n): # number of spaces k = n - 1 # outer loop to handle number of rows for i in range(0, n): # inner loop to handle number spaces # values changing acc. to requirement for j in range(0, k): print(end=" ") # decrementing k after each loop k = k - 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i+1): # printing stars print("* ", end="") # ending line after each row print("\r") # Driver Code n = 5 pyramid(n)
Exercise problems
Problems sheet for practicing loops in python can be downloaded from here.