How to Print Pattern in Python
How to Print Pattern in Python.
Pattern programs are really important for beginners and they are always asked in all the technical interviews.
Multiple loops approach is used to print the different type of patterns. (de)
defined the function 'pypart' for simple pyramid. def pypart(rows): # to handle the printing of rows, this outer loop is used. for i in range(0, rows): # this inner loop is used to handle the printing of number of columns. # Values change according to the outer loop. for j in range(0, i+1): # for printing stars and one space after that every star. print("* ",end="") # \r to end line after each row. print("\r") taking number of rows as input. using 'int' because input takes ‘str’ type as input and we have to convert it into ‘int’ type. rows= int(input("Enter the number of rows:")) pypart(rows)
Simple Pyramid Pattern
OUTPUT

Function to demonstrate printing pattern def pypart(rows): myList = [] for i in range(1,rows+1): myList.append("* "*i) print("\n".join(myList)) taking number of rows as input. using 'int' because input takes ‘str’ type as input and we have to convert it into ‘int’ type. rows= int(input("Enter the number of rows:")) pypart(rows)
Simple Pyramid Using lists
OUTPUT

Simple Pyramid after 180 degree rotation
def pyramidpart2(rows):
# number of spaces n = 2*rows - 2 # this loop is used to handle the printing of rows for i in range(0, rows): # inner loop is used to handle number spaces # values are changed according to the requirement for j in range(0, n): print(end=" ") # n is decremented after each loop n = n - 2 # this inner loop is used to handle number of columns # values are changed according to the outer loop for j in range(0, i+1): # printing stars print("* ", end="") # \r to end line after each row. print("\r")
taking number of rows as input.
using 'int' beacuse input takes str type as input and we have to convert it onto int type.
rows= int(input("Enter the number of rows:"))
pyramidpart2(rows)
OUTPUT

3. Simple Triangle pattern
defined the function 'triangle' for simple triangle pattern. def triangle(rows): # no. of spaces k = 2rows - 2 # To handle the printing of rows, this outer loop is used. for i in range(0, rows): # printing of spaces is handled by this inner loop. # values will change according to the requirement. for j in range(0, k): print(end=" ") # k is decremented after each loop k = k - 1 # this inner loop is used to handle the printing of columns. # Values will change according to the requirement. for j in range(0, i+1): # for printing the stars print(" ", end="") # \r to end the line print("\r") taking number of rows as input. using 'int' beacuse input takes ‘str’ type as input and we have to convert it onto ‘int’ type rows= int(input("Enter the number of rows:")) triangle(rows)
OUTPUT

4. Inverse Triangle pattern.
defined the function 'triangle' for simple triangle pattern. def triangle(rows): # no. of spaces k = 2rows - 2 # To handle the printing of rows, this outer loop is used. for i in range(rows, -1,-1): # printing of spaces is handled by this inner loop. # values will change according to the requirement. for j in range(k,0,-1): print(end=" ") # k is decremented after each loop k = k + 1 # this inner loop is used to handle the printing of columns. # values will change according to the requirement. for j in range(0, i+1): # for printing the stars print(" ", end="") # \r to end the line print("\r") taking number of rows as input. using 'int' beacuse input takes str type as input and we have to convert it onto int type rows= int(input("Enter the number of rows:")) triangle(rows)
OUTPUT

5. Diamond pattern
# taking number of rows as input. # using 'int' beacuse input takes str type as input and we have to convert it onto int type row = int(input("Enter the number of row: ")) # k is used to print the space k = 2 * row - 2 # to print the number of rows this outer loop is used for i in range(0, row): # to print the number of spaces this inner loop is used for j in range(0, k): print(end=" ") # k is decremented after each iteration k = k - 1 # to print the stars this inner loop is used for j in range(0, i + 1): print("* ", end="") print("") # For printing downward triangle Pyramid # k is used to print space k = row - 2 # this loop is used to print downward triangle and handle the printing of rows for i in range(row, -1, -1): # to print the spaces this inner loop is used for j in range(k, 0, -1): print(end=" ") # k is incremented after each iteration k = k + 1 # stars will be printed by this inner loop for j in range(0, i + 1): print("* ", end="") print("")
OUTPUT

6. Half Diamond Pattern
# taking number of rows as input. # using 'int' because input takes str type as input and we have to convert it onto int type r = int(input("Enter the number of rows: ")) # this outer loop will be used to print the number of rows in the upper simple pyramid for i in range(0, r): # This inner loop will be used to print the stars for j in range(0, i + 1): print("*", end=' ') print(" ") # To print the downward inverse simple pyramid # this outer loop will print the number of rows of the downward inverse simple pyramid for i in range(r + 1, 0, -1): # this inner loop is used to print the stars for j in range(0, i - 1): print("*", end=' ') print(" ")
OUTPUT

7. Hour-glass Pattern
# hour_glass_pattern is defined def hour_glass_pattern(rows): # this loop is used to print the upper half for i in range(1, rows + 1): # for printing spaces at the beginning of each row for k in range(1, i): print(" ", end = "") # for printing 'i' to 'row' values at the end of each row for j in range(i, rows + 1): print("*", end = " ") print() # this loop is used for printing the lower half for i in range(rows - 1, 0, -1): # for printing spaces at the beginning of each row for k in range(1, i): print(" ", end = "") # for printing 'i' to 'row' values at the end of each row for j in range(i, rows + 1): print("*", end = " ") print() # taking number of rows as input. # using 'int' beacuse input takes ‘str’ type as input and we have to convert it onto ‘int’ type rows= int(input("Enter the number of rows:")) hour_glass_pattern(rows)
OUTPUT
