In this tutorial, we will learn how to print a pyramid in Python. A pyramid is a shape made of characters that form an equilateral triangle. You might have seen pyramids made of stars or diamonds in computer programs. In Python, printing a pyramid is quite simple, and it can be done using loops, functions, and a few basic concepts.
Step 1: Using a for loop to create a pyramid
The simplest way to create a pyramid in Python is by using a nested for loop. You can create a pyramid of any size by simply setting the number of rows you want. Here’s a basic example with 5 rows:
1 2 3 4 5 6 7 8 |
rows = 5 for i in range(1, rows + 1): for j in range(rows - i): print(" ", end="") for k in range(2 * i - 1): print("*", end="") print() |
In the example above, the outer loop sets the height of the pyramid, while the inner loops set the width. The first nested loop is responsible for printing spaces before the stars or any other character to align the pyramid, and the second one prints the stars.
Step 2: Creating a customizable pyramid
In this step, we will create a pyramid of a user-defined size and symbol. This will require the user’s input and the use of the input() and int() functions.
1 2 3 4 5 6 7 8 9 |
rows = int(input("Enter the number of rows: ")) symbol = input("Enter the symbol: ") for i in range(rows + 1): for j in range(rows - i): print(" ", end="") for k in range(2 * i - 1): print(symbol, end="") print() |
Now you can enter the number of rows and the symbol you want to use to create the pyramid. The input() function takes user input and stores it as a string. We use the int() function to convert the string input into an integer to perform arithmetic operations (like the loop).
Step 3: Creating a function to print a pyramid
To make our code more reusable, we can create a function that takes the number of rows and the symbol as inputs and returns the pyramid as output. Here’s a function called print_pyramid() that accepts rows and a symbol as its parameters:
1 2 3 4 5 6 7 8 9 10 11 12 |
def print_pyramid(rows, symbol): for i in range(rows + 1): for j in range(rows - i): print(" ", end="") for k in range(2 * i - 1): print(symbol, end="") print() rows = int(input("Enter the number of rows: ")) symbol = input("Enter the symbol: ") print_pyramid(rows, symbol) |
Now you can simply call the print_pyramid() function with the desired number of rows and a symbol to create a pyramid.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 |
def print_pyramid(rows, symbol): for i in range(rows + 1): for j in range(rows - i): print(" ", end="") for k in range(2 * i - 1): print(symbol, end="") print() rows = int(input("Enter the number of rows: ")) symbol = input("Enter the symbol: ") print_pyramid(rows, symbol) |
Output
Enter the number of rows: 5 Enter the symbol: @ @ @@@ @@@@@ @@@@@@@ @@@@@@@@@
Conclusion
In this tutorial, we learned how to print a pyramid in Python using for loops and functions. We created a simple and customizable pyramid that can be printed in any size and with any symbol. This exercise also helped us practice basic Python concepts such as loops, inputs, and functions. You can now create various shapes, sizes, and designs of pyramids in Python.