Different Patterns of Pyramids
Taking advantage of the for loop and range function in python, we can
draw a variety of for pyramid structures. The key to the approach is
designing the appropriate for loop which will leave both vertical and
horizontal space for the position of the symbol we choose for drawing
the pyramid structure.
Table of content
|
In every pattern we required the following methods:
1.input :-take the input from user that defines the number of row in a
pyramid.
2.for loop :-to iterate the condition until required triangle is not
drown.
3.range() :- it defines the starting and ending point of the
pyramid
4.print() :- to print the asterisk (*)
Pattern -1: Pyramid Pattern
Example:
num =int(input("Enter the number of rows"))
for i in range (0,num):
for j in range (0,num - i -
1):
print(end = " ")
for j in range (0,i+ 1):
print("*",end = " ")
print()
|
Pattern -2: Reverse Pyramid Pattern
Example:
num =int(input("Enter the number of rows"))
for i in range (num,0,-1):
for j in range (0,num- i):
print(end = " ")
for j in range (0,i):
print("*",end = " ")
print()
|
Example:
num =int(input("Enter the number of rows"))
for i in range (1,num + 1):
for j in range (1, i + 1):
print("*",end = " ")
print()
|
Pattern -4:Alternative way First
Example:
rows = int(input("Enter the number of rows "))
for j in range(1, rows+1):
print("* " * j)
|
Output:The output will be the same.
Pattern -5: Alternative way Second
Example:
rows = int(input("Enter the number of rows "))
for i in range(rows):
for j in range(i +1):
print("* " ,end=" ")
print()
|
Output:The output will be the same.
Pattern -6: Reversed Right angle based pattern
Example:
num =int(input("Enter the number of rows"))
for i in range (num,0,-1):
for j in range (0,num- 1):
print(end = " ")
for j in range (0,i):
print("*",end = " ")
print()
|
Pattern-7: Left angle based pattern
Example:
rows = int(input("Enter the number of rows "))
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i + 1):
print("* ", end="")
print("")
|
0 Comments