How To Create A Pyramid In Python
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programs for printing pyramid patterns in Python
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.
Pattern -1
We draw a right angle based pattern.
Example
Live Demo
def pyramid(p): for m in range(0, p): for n in range(0, m+1): print("* ",end="") print("\r") p = 10 pyramid(p)
Output
Running the above code gives us the following result −
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pattern-2
We make a 180 degree rotation to the above pattern.
Example
Live Demo
def pyramid(p): X = 2*p - 2 for m in range(0, p): for n in range(0, X): print(end=" ") X = X - 2 for n in range(0, m+1): print("* ", end="") print("\r") p = 10 pyramid(p)
Output
Running the above code gives us the following result −
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pattern-3
In this case we draw a triangle shape of type isosceles. Both the sides of the triangle are of equal length.
Example
Live Demo
n = 0 r = 12 for m in range(1, r+1): for gap in range(1, (r-m)+1): print(end=" ") while n != (2*m-1): print("* ", end="") n = n + 1 n = 0 print()
Output
Running the above code gives us the following result −
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Pattern-3
Now we draw a case of triangle in which all the three sides are equal. Also called equilateral triangle.
Example
Live Demo
length = 12 k = (2 * length) - 2 for p in range(0, length): for n in range(0, k): print(end=" ") k = k - 1 for n in range(0, p + 1): print("@", end=' ') print(" ")
Output
Running the above code gives us the following result −
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
Published on 04-Feb-2020 07:26:51
- Related Questions & Answers
- Java Programs for printing Pyramid Patterns. (of numbers)
- Printing Pyramid in C++
- Printing Pyramid using Recursion in C++
- C++ Programs To Create Pyramid and Pattern
- Program for volume of Pyramid in C++
- Structuring Python Programs
- Regular Expression Patterns in Python
- Multi-Line printing in Python
- Warning control in Python Programs
- Program for printing array in Pendulum Arrangement.
- Program for triangular patterns of alphabets in C
- Printing to the Screen in Python
- C++ Program for Expressionless Face Pattern printing
- Application Programs vs System Programs
- How to specify options for MySQL programs?
How To Create A Pyramid In Python
Source: https://www.tutorialspoint.com/programs-for-printing-pyramid-patterns-in-python
Posted by: buchananlatepred.blogspot.com
0 Response to "How To Create A Pyramid In Python"
Post a Comment