Skip to content
pythoneo

Pythoneo

Online How to Python stuff

How to draw pyramid?

Posted on January 14, 2021September 21, 2023 By Pythoneo

Let’s embark on an exciting journey to learn how to draw various pyramids using Python.

python pyramid

Drawing a Numeric Pyramid

To start, we’ll write a Python script that prompts the user to enter a number. Based on the provided number, the script will draw a pyramid of that size.

python pyramid enter number

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(" ")

This script utilizes nested loops to draw spaces and numbers from 1 to the user-provided number.

Creating a Hashed Pyramid

Now, let’s explore another Python pyramid example, this time using 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()

python pyramid hashes

This pyramid will be taller and thinner, constructed using hash symbols.

See also  What are the max and min values of integer in Python?

Crafting an Upside-Down Pyramid

In a similar fashion, we can create an upside-down pyramid.

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()

python pyramid upside down

Constructing a Digits Pyramid

Let’s design another Python pyramid, this time composed of digits.

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))

python digits pyramid

Designing a Letter Pyramid

Lastly, let’s create a pyramid using letters:

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)

python letters pyramid

Now that you’ve become proficient in creating pyramids, let’s delve into additional intriguing variations to expand your Python skills.

See also  How to run a Python script in Linux?

Hollow Pyramid

Enhance your Python pyramid by crafting a hollow version. This design leaves the center of the pyramid empty, creating an exciting geometric pattern. Here’s a Python script to get you started:

size = int(input("Enter a number: "))

for i in range(1, size + 1):
    for j in range(size - i):
        print(' ', end='')
    for j in range(2 * i - 1):
        if j == 0 or j == 2 * i - 2 or i == size:
            print('#', end='')
        else:
            print(' ', end='')
    print()

Pyramid of Symbols

Explore the world of symbols by designing a pyramid with your favorite characters. You can choose any character or symbol to construct your unique pyramid. Here’s an example using asterisks:

size = int(input("Enter a number: "))

for i in range(1, size + 1):
    for j in range(size - i):
        print(' ', end='')
    for j in range(2 * i - 1):
        print('*', end='')
    print()

Inverted Hollow Pyramid

Reverse the direction of your hollow pyramid, creating a unique inverted pattern. Here’s how you can achieve it:

size = int(input("Enter a number: "))

for i in range(size, 0, -1):
    for j in range(size - i):
        print(' ', end='')
    for j in range(2 * i - 1):
        if j == 0 or j == 2 * i - 2 or i == size:
            print('#', end='')
        else:
            print(' ', end='')
    print()

Feel free to explore these variations and experiment with different symbols, patterns, and directions to create captivating Python pyramids.

See also  How To Exit A Function In Python
Python Tags:draw, pyramid

Post navigation

Previous Post: How to convert cm to inch?
Next Post: How to convert char to string in Python

Categories

  • bokeh (1)
  • Django (5)
  • matplotlib (11)
  • numpy (99)
  • OpenCV (4)
  • Pandas (3)
  • paramiko (12)
  • Pillow (3)
  • Plotly (3)
  • Python (30)
  • Scipy (4)
  • Seaborn (7)
  • statistics (7)
  • Tkinter (8)
  • turtle (2)

RSS RSS

  • OpenCV FindContours: Detecting and Analyzing Objects in Images
  • How to create a simple animation in Tkinter
  • Adaptive Thresholding with OpenCV
  • Hot to use the grid geometry manager in Tkinter
  • How to install and use paramiko for SSH connections in Python
  • How to automate file transfers with paramiko and SFTP
  • How to Execute Remote Commands with Paramiko and SSHClient
  • Handling Paramiko Errors and Timeouts
  • How to use paramiko with multiprocessing and threading
  • How to use matplotlib cmap?

Tags

arithmetic mean array axis button calculations chart column conversion count data type dictionary dimension draw error files fill float generate grid GUI image index integer list matrix max mean min mode multiply normal distribution plot random reshape rotate round rows size string sum test text time type zero

Copyright © 2023 Pythoneo.

Powered by PressBook WordPress theme

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Go to mobile version