In this tutorial, we will learn how to print patterns to console using Python While Loop. Example 1 – Python Program to Print Right Triangle using While LoopIn this example, we will write a Python program to print the following start pattern to console. We shall read the number of rows and print starts as shown below. Pattern For an input number of 4, following would be the pattern. * * * * * * * * * * Try Online Python Program n = int(input('Enter number of rows : ')) i = 1 while i <= n : j = 1 while j <= i: print("*", end = " ") j += 1 print() i += 1 Inner while loop prints a single row after its complete execution. Outer while loop helps to print In other words, outer while loop prints the rows, while inner while loop prints columns in each row. Output Enter number of rows : 6 * * * * * * * * * * * * * * * * * * * * * Example 2 – Python Program to Print Inverted Right Triangle using While LoopIn this example, we will write a Python program to print the following start pattern to console. Pattern For an input number of 4, following would be the pattern. * * * * * * * * * * Try Online Python Program n = int(input('Enter number of rows : ')) i = 1 while i <= n : j = n while j >= i: print("*", end = " ") j -= 1 print() i += 1 Output Enter number of rows : 5 * * * * * * * * * * * * * * * Example 3 – Python Program to Print Number Pattern using While LoopIn this example, we will write a Python program to print the following pattern to console. We shall read the number of rows and print numbers as shown below. Pattern For an input number of 5, following would be the pattern. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Try Online Python Program n = int(input('Enter number of rows : ')) k = 1 i = 1 while i <= n : j = 1 while j <= i: print("{:3d}".format(k), end = " ") j += 1 k += 1 print() i += 1 Output Enter number of rows : 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ConclusionIn this Python Tutorial, we learned to write Python programs to print different types of patterns using While Loop. Programs to print triangles using *, numbers and charactersExample 1: Program to print half pyramid using ** * * * * * * * * * * * * * * Source Code
In the above program, let's see how the pattern is printed.
Example 2: Program to print half pyramid a using numbers1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Source Code
In the above program, let's see how the pattern is printed.
Example 3: Program to print half pyramid using alphabetsA B B C C C D D D D E E E E E Source Code
The working of the above example is also similar to the other examples
discussed above except that the ascii values are printed here. The ascii value for alphabets start from 65 (i.e. A). Therefore, in each iteration, we increase the value of Programs to print inverted half pyramid using * and numbersExample 4: Inverted half pyramid using ** * * * * * * * * * * * * * * Source Code
This example is similar to an upright pyramid except that here we start from the total number of Example 5: Inverted half pyramid using numbers1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Source Code
The only difference between an upright and an inverted pyramid using numbers is that the first loop starts from the total number of Programs to print full pyramidsExample 6: Program to print full pyramid using ** * * * * * * * * * * * * * * * * * * * * * * * * Source Code
This type of pyramid is a bit more complicated than the ones we studied above.
Example 7: Full Pyramid of Numbers1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 Source Code
Like example 6, this example also makes use of two loops inside a for loop.
Example 8: Inverted full pyramid of ** * * * * * * * * * * * * * * * * * * * * * * * * Source Code
In this example, we have used a total of 4 for loops.
Example 9: Pascal's Triangle1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Source Code
In this example, we have used three for loops.
Example 10: Floyd's Triangle1 2 3 4 5 6 7 8 9 10 Source Code
This is one of the easiest patterns.
How do you print a pyramid pattern in Python while loop?Pattern - 1: Simple pyramid pattern. # This is the example of print simple pyramid pattern.. n = int(input("Enter the number of rows")). # outer loop to handle number of rows.. for i in range(0, n):. # inner loop to handle number of columns.. # values is changing according to outer loop.. for j in range(0, i + 1):. # printing stars.. How do you make a full pyramid in Python?Source Code. First, we get the height of the pyramid rows from the user.. In the first loop, we iterate from i = 0 to i = rows .. The second loop runs from j = 0 to i + 1. ... . Once the inner loop ends, we print new line and start printing * in a new line.. |