Open In App

Simple Diamond Pattern in Python

Last Updated : 29 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer n, the task is to write a python program to print diamond using loops and mathematical formulations. The minimum value of n should be greater than 4.

Examples :

For size = 5

  * * *   
* * * * * 
  * * *   
    *  
    
For size = 8

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

For size = 11

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

Approach :

The following steps are used :

  • Form the worksheet of (size/2+2) x size using two loops.
  • Apply the if-else conditions for printing stars.
  • Apply else condition for rest spaces.

Below is the implementation of the above approach :

Example 1:

Python3




# define the size (no. of columns)
# must be odd to draw proper diamond shape
size = 8
 
# initialize the spaces
spaces = size
 
# loops for iterations to create worksheet
for i in range(size//2+2):
    for j in range(size):
       
        # condition to left space
        # condition to right space
        # condition for making diamond
        # else print *
        if j < i-1:
            print(' ', end=" ")
        elif j > spaces:
            print(' ', end=" ")
        elif (i == 0 and j == 0) | (i == 0 and j == size-1):
            print(' ', end=" ")
        else:
            print('*', end=" ")
             
    # increase space area by decreasing spaces
    spaces -= 1
     
    # for line change
    print()


Output :

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

Example 2:

Python3




# define the size (no. of columns)
# must be odd to draw proper diamond shape
size = 11
 
# initialize the spaces
spaces = size
 
# loops for iterations to create worksheet
for i in range(size//2+2):
    for j in range(size):
       
        # condition to left space
        # condition to right space
        # condition for making diamond
        # else print ^
        if j < i-1:
            print(' ', end=" ")
        elif j > spaces:
            print(' ', end=" ")
        elif (i == 0 and j == 0) | (i == 0 and j == size-1):
            print(' ', end=" ")
        else:
            print('*', end=" ")
             
    # increase space area by decreasing spaces
    spaces -= 1
     
    # for line change
    print()


Output :

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


Previous Article
Next Article

Similar Reads

Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half start the for loop with iterator (i) from 1 to n and
4 min read
Python program to display half diamond pattern of numbers with star border
Given a number n, the task is to write a Python program to print a half-diamond pattern of numbers with a star border. Examples: Input: n = 5 Output: * *1* *121* *12321* *1234321* *123454321* *1234321* *12321* *121* *1* * Input: n = 3 Output: * *1* *121* *12321* *121* *1* * Approach: Two for loops will be run in this program in order to print the n
2 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Python Program to print a number diamond of any given size N in Rangoli Style
Given an integer N, the task is to print a number diamond of size N in rangoli style where N means till Nth number from number ‘1’. Examples: Input : 2 Output : --2-- 2-1-2 --2-- Input : 3 Output : ----3---- --3-2-3-- 3-2-1-2-3 --3-2-3-- ----3---- Input : 4 Output : ------4------ ----4-3-4---- --4-3-2-3-4-- 4-3-2-1-2-3-4 --4-3-2-3-4-- ----4-3-4----
3 min read
bokeh.plotting.figure.diamond() function in Python
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, HTML and server. Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with default axes, grids, tools, etc. bokeh.plotting.figur
2 min read
Draw Diamond shape using Turtle graphics in Python
In this article, we are going to learn how to draw the shape of a Diamond using turtle graphics in Python. Turtle graphics: forward(length): moves the pen in the forward direction by x unit.right(angle): rotate the pen in the clockwise direction by an angle x.left(angle): rotate the pen in the anticlockwise direction by an angle x. Approach: Import
2 min read
Program to print the diamond shape
Given a number n, write a program to print a diamond shape with 2n rows. Examples : C/C++ Code // C++ program to print diamond shape // with 2n rows #include &lt;bits/stdc++.h&gt; using namespace std; // Prints diamond pattern with 2n rows void printDiamond(int n) { int space = n - 1; // run loop (parent loop) // till number of rows for (int i = 0;
11 min read
Create simple Blockchain using Python
Blockchain is a time-stamped decentralized series of fixed records that contains data of any size is controlled by a large network of computers that are scattered around the globe and not owned by a single organization. Every block is secured and connected with each other using hashing technology which protects it from being tampered by an unauthor
8 min read
Simple Attendance Tracker using Python
In many colleges, we have an attendance system for each subject, and the lack of the same leads to problems. It is difficult for students to remember the number of leaves taken for a particular subject. If every time paperwork has to be done to track the number of leaves taken by staff and check whether it is a lack of attendance or not, it involve
9 min read
Simple registration form using Python Tkinter
Prerequisites: Tkinter Introduction, openpyxl module.Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let's make a simple information form GUI application using Tkinter. In this application, User has to fill up the required info
5 min read
Practice Tags :