How to print a graphical shape using Python?

To print a specific shape using Python, you will need to use loops and conditional statements to control the printing process.

Here are some examples demonstrating how to use Python to print out shapes of different designs:

  1. Print rectangle.
width = 5
height = 3

for i in range(height):
    for j in range(width):
        print("*", end="")
    print()

This code will print a rectangle with a width of 5 and a height of 3.

*****
*****
*****
  1. Print a right triangle.
size = 5

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

This code will print a right-angled triangle with a height of 5.

*
**
***
****
*****
  1. Print an upside-down right triangle.
size = 5

for i in range(size):
    for j in range(size-i):
        print("*", end="")
    print()

This code will print an inverted right-angled triangle with a height of 5.

*****
****
***
**
*
  1. Print an isosceles triangle.
size = 5

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

This code will print an isosceles triangle with a height of 5.

    *
   ***
  *****
 *******
*********

These are just some simple examples that you can modify and expand as needed. Depending on your requirements, you can use different loops and conditional statements to control the printing process and create a variety of shapes.

bannerAds