Let’s have some fun today and learn on how to draw a pyramid with Python.

## Draw a pyramid

To draw pyramid I decided to write a code which first ask the user to enter the number and based on the number will draw a pyramid that size.

UserNumber = eval(input("Please enter a number:")) for i in range(1, UserNumber + 1): for j in range(UserNumber-i, 0, -1): print(" ", end="\t") for j in range(i, 1, -1): print(j, end="\t") for j in range(1, i+1): print(j, end="\t") print(" ")

The script is basically simple for nested loops. They are drawing space or the number from 1 to UserNumber.

## Draw a hashed pyramid

Another example of python pyramid is the one made of hashes instead of numbers.

size = int(input("Enter number: ")) for number in range(1, size + 1): for i in range(size - number): print(' ', end='') for j in range(2 * number - 1): print('#', end='') print()

As you can see this python pyramid is higher and thinner 😉

### Draw a upside down pyramid

The same way we can draw pyramid upside down.

size = int(input("Enter number:")) for i in range(0, size): for j in range(i + 1): print(' ', end='') for x in range(size - 2 * i): print('#', end='') print()

### Draw a digits pyramid

Another python pyramid will be made of digits again.

import math x = 10 y = 0 for i in range(x): a = x - i y += pow(10, i) for j in range(a): print(' ', end='') print(pow(y, 2))

### Draw a letter pyramid

As we already drew digits pyramid then let’s draw letter pyramid. Here’s the one:

number = int(input('Enter the number:')) for i in range(number): n = ' ' * (number - i) for j in range((2 * i + 1) // 2): n += chr(65 + i - j) for j in range((2 * i + 1) // 2, 2 * i + 1): n += chr(65 + j - i) print(n)

You know now, how to draw a pyramid. What about drawing something more with Python?