Python program for a diamond pattern [2 Methods]

In this Python tutorial, I will discuss how to write a Python program for a diamond pattern. Let’s talk about two main diamond printing methods present in Python using stars. Let’s start by printing a whole diamond pattern using those methods with examples.

diamond pattern in python

Python program for the diamond pattern

  • The number of rows and columns are printed using the first outer and inner for loops, respectively, in the numerous for loops used to print the patterns in Python.
  • First, we will print the number of rows using the outer loop and then print the number of columns using the inner loop in Python. The Python variable is used to output whitespace when it is needed.
  • In this example, we will print the number of asterisks and white spaces. Let’s take an example and check how to print the upper half of the diamond in Python.

Example:

new_val = eval(input("Enter the upper height of diamond: "))
for i in range(new_val):
    print(" " * (new_val - i), "*" * (2*i + 1))

Output: If you look at the diamond, you’ll notice that there are now 1, 3, 5, and 7 more asterisks in the upper part. The number of asterisks in that row is therefore 2x+1, where x is the diamond’s row number from top to bottom.

As we move forward, the number of empty spaces on the left must drop by one in order to make up for the rise in asterisks. As a result, there are h-x white spaces. (There are no white spaces on the right!)

You should therefore print h-x white spaces and 2x+1 asterisks for each row, where x is the height that ranges from 0 to height – 1.

Here is the implementation of the following code:

Enter the upper height of diamond: 5
      *
     ***
    *****
   *******
  *********
print diamond pattern in python

This is how to create a program of diamond patterns in Python.

Methods to write Python program for a diamond Pattern

There are two different methods present in Python to print diamond pattern using stars:

  1. for loop
  2. while loop

Let’s see them one by one using some illustrative examples:

Note: The pattern in the output block can differ because of the line-gapping difference between the code block in the article and in the Pycharm editor.

Python program to make a diamond pattern using for loop

  • Choose how many rows and columns to use. The number of rows and columns is a common structure for printing any pattern. To print any pattern, we must use two loops; in other words, we must use nested loops.
  • The number of rows and the number of columns required to print the pattern are both specified by the outer and inner loops, respectively.
  • The input() function can be used to determine the size of a pattern by asking the user how many rows they want.
  • Row iteration Next, create an outer loop with a for loop and the range() method to iterate the number of rows.
  • column iterations: To manage the number of columns, construct an inner loop or nested loop next. depending on the values of the outer loop, the internal loop iteration.

Example:

Let’s take an example and check how we can make a diamond pattern in Python.

Source Code:

number_of_stars_USA = int(input("Enter the row's value for diamond"))   
for m in range(0,number_of_stars_USA):
    for n in range(0,number_of_stars_USA-m-1):
        print(" ",end='')
    for l in range(0,2*m+1):
        print("*",end='')
    print()
for m in range(number_of_stars_USA-1,0,-1):
    for n in range(number_of_stars_USA,m,-1):
        print(" ",end='')
    for l in range(2*m-1,0,-1):
        print("*",end='')
    print() 

Output:

Enter the row's value for diamond: 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Here is the Screenshot of the following code:

diamond pattern in python using for loop

In this example, we have understood how to make a diamond pattern in Python by using for loop method.

Python program to print diamond pattern using while loop

  • In this section, we will discuss how to print diamond patterns by using a while loop in Python.
  • To perform this particular task we are going to use the while loop concept and define the function and within this function, we passed the number as an argument.
  • In this Python program, the user’s row is initially read. The number of rows in this triangle of a diamond design is indicated by the row. Given a row value of 6, the pattern’s total number of lines will be 10.

Example:

Here we are going to take an example and check how to print diamond patterns by using a while loop.

def pattern(number_of_values):
   
   m = 1
   while m<number_of_values:
      
      print(" "*(number_of_values-m) + "* " * m)
      m+=1 

   m = number_of_values
   while m>=1:
      print(" "*(number_of_values-m) + "* " * m)
      m-=1
number_of_values = int(input('Enter the number of rows: '))

pattern(number_of_values)

Here is the execution of the following given Python program for a diamond pattern:

Enter the number of rows: 3
  * 
 * * 
* * * 
 * * 
  * 
diamond pattern in python using while loop

This is how to write a Python program for a diamond pattern using the while loop method.

Python Program to print half diamond pattern

  • In this example, we will discuss how to print half-diamond patterns in Python.
  • The asterisk count drops from, 7, 5, 3, and 1 in the lower part. To put it another way, if x is the diamond’s row number starting from the middle of the diamond, then 2x+1 is the number of asterisks in that row.
  • The more we go down, the fewer asterisks there are, so the more empty spaces there must be on the left, which must increase by one. Therefore, there are h-x white spaces in all.
  • In order to print h-x white spaces and 2x+1 asterisks for each row, where x is the height between height- 2 and 0, please see the example below.

Example:

new_val = eval(input("Enter the lower height of diamond: "))
for i in range(new_val - 2, -1, -1):
    print(" " * (new_val - i), "*" * (2*i + 1))

Output:

Enter the lower height of diamond: 5
   *******
    *****
     ***
      *

You can refer to the below Screenshot of Python program for a diamond pattern:

diamond star pattern in python

As you can see in the screenshot we have discussed how to print half-diamond patterns in Python.

Python code to print diamond pattern

  • In this section, we will discuss how to print the diamond pattern in Python.
  • First, we will print the number of rows using the outer loop and then print the number of columns using the inner loop. The Python variable is used to output whitespace when it is needed.
  • These are the steps we are going to follow in our example
    • Enter the number of rows required to create a pattern in the shape of a diamond then use for loop with range (n)
    • Use a second for loop with the following range: 1, int((n/2))-i+3. and use the statement Print(“sep”, “end”). End of a loop (3) and then use the range(1,i+2) for loop.
    • printing “*”, “end=” End of a loop (6), and the space is printed after the loop (2) ends.
    • We utilize a loop with a range for the lower part of the diamond (n) and then Use a second for loop that has a range of (1.5-(int((n/2))-i+3)+2). Print(“sep”, “end”)
    • Use for loop with range (1,5-i) and print(“*”, end=” “) after the space, print the loop(10) ends.

Example:

new_val=int(input("enter the value for row's"))

for m in range(new_val):

    for n in range(1,int((new_val/2))-m+3):

        print(sep=" ",end=" ")

    for l in range(1,m+2):

        print("^", end=" ")

    print()

for m in range(new_val):

    for n in range(1,5-(int((new_val/2))-m+3)+2):

        print(sep=" ",end=" ")

    for l in range(1,5-m):

        print("^", end=" ")

    print()

Here is the implementation of the following given Python Python program for a diamond pattern:

Python program for a diamond pattern

This is how to write a Python program for a diamond pattern.

Python Program to print diamond-shape

  • We will use two outers for loops, one for the top triangle and the other for the lower triangle, as well as nested loops to print the diamond pattern in Python.
  • In this method, the string property in Python will be used to repeat itself by a specified number including the multiplication symbol.

Example:

number = 8

for n in range(number-1):
  print((number-n) * ' ' + (2*n+1) * '@')

for n in range(number-1, -1, -1):
  print((number-n) * ' ' + (2*n+1) * '@')

In the following given code first, we declare a variable and assign the integer number to it. Next, we used the for-loop method iterated the for-loop, and set the condition (2*n+1).

Output:

     @
    @@@
   @@@@@
  @@@@@@@
 @@@@@@@@@
  @@@@@@@
   @@@@@
    @@@
     @

Here is the Screenshot of the following given Python code:

number diamond pattern in python

In this example, we have understood how to display the diamond shape in Python.

Note: The pattern in the output block is different because of the line-gapping difference between the code block in the article and in the Pycharm editor.

Conclusion

In this tutorial, we have discussed how to Python program for a diamond pattern. We have also seen, two different methods to print diamond using stars in Python like, for loop and while loop. And also we have covered the following given Python topics:

  • Python program for the diamond pattern
  • Python program to make a diamond pattern using for loop
  • Python program to print diamond pattern using while loop
  • Python code to print diamond pattern
  • Python Program to print half diamond pattern
  • Python Program to print diamond shape

You may like the following Python tutorials: