Python Programs to Print Patterns – Pyramid, Triangle using Star
This tutorial will guide on how to write python programs to print patterns like pyramid or triangle using start symbol. Let’s see how to print half pyramids, inverted half pyramid, full pyramid or triangle and inverted full pyramid patterns using Python programming language.
Python Programs to Print Patterns
Patterns can be printed using simple for loops in python programming. We need to use two for loops for printing all the patterns. The first outer for loop is used to handle number of rows and the inner nested for loop is used to handle number of columns.
You need to manipulate the print statements to print the pattern in required format. Here we have tried to print start patterns. Therefore, we will be manipulating print statement using star “*” in the statements.
Print simple half pyramid pattern
The following program will print Half Pyramid Pattern.
>>> def pyhalfpyramidpattern(n): ... for i in range(0,n): ... for j in range(0, i+1): ... print("* ",end="") ... print("\r") ...
Output
>>> n = 6 >>> pyhalfpyramidpattern(n) * * * * * * * * * * * * * * * * * * * * *
Print inverted half pyramid pattern
Now, let’s see how to print the above half pyramid pattern in the inverted fashion.
Here is the program to print Inverted Half Pyramid Pattern
>>> def pyinvertedhalfpyramidpattern(n): ... k=2*n-2 ... for i in range(n, -1, -1): ... for j in range(k, 0, -1): ... print(end="") ... k=k+1 ... for j in range(0, i+1): ... print("* ",end="") ... print(" ")
Output
>>> n=6 >>> pyinvertedhalfpyramidpattern(n) * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Half Pyramid Pattern – 180 rotation
Next, let’s try to rotate the half pyramid pattern by 180 degree and print.
Here is the program to print Half Pyramid Pattern – 180 degree rotation.
>>> def pyhalfpyramidpattern2(n): ... k = 2*n-2 ... for i in range(0,n): ... for j in range(0, k): ... print(end=" ") ... k = k-2 ... for j in range(0, i+1): ... print("* ", end="") ... print("\r") ...
Output
>>> n = 6 >>> pyhalfpyramidpattern2(n) * * * * * * * * * * * * * * * * * * * * *
Similarly, in the next section let’s learn how to print full pyramid or triangle pattern and inverted full pyramid pattern.
Full Pyramid/ Triangle pattern
Here is the program to print Full Pyramid Pattern or Triangle Pattern.
>>> def pyfullpyramidpattern(n): ... k = n-1 ... for i in range(0,n): ... for j in range(0,k): ... print(end=" ") ... k = k-1 ... for j in range(0,i+1): ... print("* ", end="") ... print("\r") ...
Output
>>> n = 6 >>> pyfullpyramidpattern(n) * * * * * * * * * * * * * * * * * * * * *
Inverted Pyramid or Triangle
Next, lets try to print Inverted Full Pyramid or Triangle Pattern.
>>> def pyinvertedtriangle(n): ... k = n -2 ... for i in range(n, -1, -1): ... for j in range(k, 0, -1): ... print(end=" ") ... k = k + 1 ... for j in range(0, i+1): ... print("* ", end="") ... print()
Output
>>> n = 6 >>> pyinvertedtriangle(n) * * * * * * * * * * * * * * * * * * * * * * * * * * * *
That’s it. You had learnt how to print half pyramids, inverted half pyramid, full pyramid or triangle and inverted full pyramid patterns using Python programming language.
Hope it helped 🙂
You’ll also like:
- Convert floating point number to fixed point in Python
- Convert single digit number to double digits string in Python
- What is %matplotlib inline and how to use ?
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Reset jupyter notebook theme to default theme
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- Check if a file or folder exists without getting exceptions ?
- Python program to find the greatest of three numbers
- Find difference between two given numbers in Python
- String indexing in Python -Examples
- Embed HTML within IPython Notebook
- Check if Python Object is a Number ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python
- Java String substring() example program
References