Hollow square pattern in python assignment expert

This shot will discuss how to generate a hollow diamond pattern using letters of the alphabet in Python.

Numerous patterns can be printed using Python once we have a firm grip over the loops concepts. Here we will be using simple for loops to generate a hollow diamond pattern using letters of the alphabet in Python.

Description

To execute a diamond pattern using Python programming, we will be using 2 outer for loops, one for the upper triangle and the other for the lower triangle, and 4 nested loops to print the pattern.

Code

Let’s have a look at the code.

# Number of rows

row = 5

# Upper part of hollow diamond

for i in range(1, row+1):

for j in range(1,row-i+1):

print(" ", end="")

for j in range(1, 2*i):

ch = chr(64+i)

if j==1 or j==2*i-1:

print(ch, end="")

else:

print(" ", end="")

print()

# Lower part of hollow diamond

for i in range(row-1,0, -1):

for j in range(1,row-i+1):

print(" ", end="")

for j in range(1, 2*i):

if j==1 or j==2*i-1:

ch = chr(64+i)

print(ch, end="")

else:

print(" ", end="")

print()

Explanation

  • In line 2, we take the input for the number of rows (i.e., the length of one side of the diamond).

  • In lines 5-14, we create a for loop to generate the upper triangle.

  • In line 5, we create a for loop to handle the number of rows.

  • In lines 6-7, we create a for loop to handle the number of spaces.

  • In lines 8 to 14, we create a for loop to print the patterns.

    • ch is used to create letters from numbers by using the iterative value of i and the concept of ASCIIAmerican Standard Code for Information Interchange conversion. The starting value 64 + (i=1) has been used as ASCII value of A (starting the diamond is 65).
    • j==1 creates the left arm of the triangle.
    • j==2*i-1 creates the right arm of the triangle.
    • The end statement is used to stay on the same line.
    • The print() statement is used to move to the next line.
  • In lines 17-26, we create a for loop to generate the lower triangle.

  • In line 17, we create a for loop to handle the number of rows.

  • In lines 18-19, we create a for loop to handle the number of spaces.

  • In lines 20-26, we create a for loop to print the patterns.

    • ch is used to create letters from numbers by using the iterative value of i and the concept of ASCII conversion. The starting value 64 + (i=1), has been used as ASCII value of A (starting the diamond is 65).
    • j==1 creates the left arm of the triangle.
    • j==2*i-1 creates the right arm of the triangle.
    • The end statement is used to stay on the same line.
    • The print() statement is used to move to the next line.

CONTRIBUTOR

Vinisha Maheshwari

In this shot, we will discuss how to generate a hollow rectangle pattern using stars in Python.

Numerous patterns can be printed in Python once you have a strong grip over loops. Here, we will use simple for loops to generate a rectangle pattern with stars.

Description

To execute a rectangular pattern with Python programming, we will use 2 for loops - one outer and one nested loop:

  • Outer loop: Iterates over the number of rows.
  • Inner nested loop: Iterates over the number of columns in each row.

Code

Let us look at the code snippet below.

# Initialising Length and Breadth

rows = 3

columns = 6

# Loop through number of rows

for i in range(rows):

# Loop through number of columns

for j in range(columns):

# Printing Pattern

if(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):

print('*', end = ' ')

else:

print(' ', end = ' ')

print()

Explanation

  • In line 2, we take the input for the number of rows.
  • In line 3, we take the input for the number of columns.
  • In line 6, we create a for loop to iterate through the number of rows.
  • In line 9, we create a for loop to iterate through the number of columns.
  • In lines 12 and 16, we print the pattern.
    • i == 0 creates the upper side of the rectangle.
    • i == rows - 1 creates the upper side of the rectangle.
    • j == 0 creates the left side of the rectangle.
    • j == columns - 1 creates the right side of the rectangle.
    • The end statement is used to stay on the same line.
    • The print() statement is used to move to the next line.

CONTRIBUTOR

Vinisha Maheshwari