Easy Tutorial
❮ Verilog2 Specify Moible Web Front Source ❯

Python Print Diamond, Triangle, and Rectangle

Category Programming Techniques

Example

#coding:utf-8
rows = int(input('Enter the number of rows: '))
i = j = k = 1 #Declare variables, i is for controlling the outer loop (number of lines of the shape), j is for controlling the number of spaces, k is for controlling the number of *
#Isosceles right triangle 1
print "Isosceles right triangle 1"
for i in range(0, rows):
    for k in range(0, rows - i):
        print " * ", #Note the "," here, it must not be omitted, it can prevent a line break
        k += 1
    i += 1
    print "\n"

#Print solid equilateral triangle
print "Print hollow equilateral triangle, remove the if-else condition here and it will be solid"
for i in range(0, rows + 1): #Variable i controls the number of lines
    for j in range(0, rows - i): #(1,rows-i)
        print " ",
        j += 1
    for k in range(0, 2 * i - 1): #(1,2*i)
        if k == 0 or k == 2 * i - 2 or i == rows:
            if i == rows:
                if k % 2 == 0: #Since the first number starts from 0, even numbers print *, odd numbers print space
                    print "*",
                else:
                    print " ", #Note the "," here, it must not be omitted, it can prevent a line break
            else:
               print "*",
        else:
            print " ",
        k += 1
    print "\n"
    i += 1

#Print diamond
print "Print hollow diamond, remove the if-else condition here and it will be solid"
for i in range(rows): #Variable i controls the number of lines
    for j in range(rows - i): #(1,rows-i)
        print " ",
        j += 1
    for k in range(2 * i - 1): #(1,2*i)
        if k == 0 or k == 2 * i - 2:
            print "*",
        else:
            print " ",
        k += 1
    print "\n"
    i += 1
    #The lower half of the diamond
for i in range(rows):
    for j in range(i): #(1,rows-i)
        print " ",
        j += 1
    for k in range(2 * (rows - i) - 1): #(1,2*i)
        if k == 0 or k == 2 * (rows - i) - 2:
            print "*",
        else:
            print " ",
        k += 1
    print "\n"
    i += 1
#Solid square
print "Solid square"
for i in range(0, rows):
    for k in range(0, rows):
        print " * ", #Note the "," here, it must not be omitted, it can prevent a line break
        k += 1
    i += 1
    print "\n"

#Hollow square
print "Hollow square"
for i in range(0, rows):
    for k in range(0, rows):
        if i != 0 and i != rows - 1:
            if k == 0 or k == rows - 1:
                #Due to visual effects, it looks more like a square, so * is added with spaces on both sides to increase the distance
                print " * ", #Note the "," here, it must not be omitted, it can prevent a line break
            else:
                 print "   ", #There are three spaces here
        else:
            print " * ", #Here * is added with spaces on both sides
        k += 1
    i += 1
    print "\n"

Execution output:

* * * *

* * *

* *

*

Print hollow equilateral triangle, remove the if-else condition here and it will be solid

  * 

*   *

* *

* * * *

Print hollow diamond, remove the if-else condition here and it will be solid

  * 

*   *

* *

* *

* *

❮ Verilog2 Specify Moible Web Front Source ❯