Python Program To Print Diamond Pattern




 



In this post, we are going to write a python program to print a diamond pattern of * like this.








General idea :

To print this diamond, we need to divide the diamond into two parts. One triangle with base down, then the triangle with base upwards.







First, we need to print the triangle with base down, the triangle with base up. In the first row of the first triangle with base down, there are 4 spaces and then the * is printed. In the second row of the same triangle, there are 3 spaces and the 3 * are printed. In the third row 2 spaces and 5 * are printed and in the fourth row 1 space and 7 * printed. Finally, in the last row, no space, and 9 * are printed. Here the spaces are decreasing by 1 and no of * increasing by 2.

In the first row of the second triangle with base upwards. 1 space and 7 * are printed. In the second row 2 spaces and 5 * printed. Then finally 4 spaces and 1 * are printed. In this one, the number of spaces is increasing by 1, and the number of * is decreasing by 2.

Pre-requisites :

  • Python basics
  • String (replication)
Program :

j=4 #printing upper part
for i in range(1,10,2):
    print(' '*j,'*'*i)
    j=j-1
j=1 #printing lower part
for i in range(7,0,-2):
    print(' '*j,"*"*i)
    j=j+1












Explanation :



1. First we need to print the upper triangle (i.e, the triangle with base downwards). Here we are assigning j to 4. This j denoted the number of empty spaces.




2. Now as in the first triangle as the number of * increases from 1 to 9 by 2, we are using for loop. Here the variable 'i' denotes the number of * in a line. The first argument in the for loop is the start value( where the loop should start ).  Here in this case 1 because the number of * in the first line is 1. The second argument in the for loop is the stop value ( where the loop should stop ). It is always considered that the element in the stop value is not executed. The value before stop value is excuted. If we enter 10, it will be considered as 9. Here in this case 10 as the number of * in the last line of the first triangle is 9. The third argument in the for loop is the step value. Here in this case 2 as the difference between the number of * in successive line is 2 ( Number of * in line 2 - Number of * in line 1 ).




3.  This statement is written under the for loop as we can see the indentation. Here we are printing the spaces and *. We are replicating the string. Here we are replicating space with j that means j number of spaces are printed and replicating the * with 'i' which means 'i' number of * are printed. 'i' value is increasing like 1,3,5,7, and 9 due to for loop.



4. This statement is also written under the for loop. We increased i value using for loop as the number of * increases. We need to decrease the number of spaces by 1. Here we are doing that. That means the j values decrease by 1  for every increase in i value by 2 as this is written under the for loop. With this, we printed the upper triangle.

OUTPUT :





\


Now we need to print the second triangle ( i.e, triangle with base upwards ).





5. This statement is written separately as there is no indentation. Now we are assigning 1 to j as the number of spaces in the first line of the second triangle is one.





6. This is the second for loop. Like in the first triangle i represents the number of * and j value represents the number of spaces. The start value in this loop is 7 as the number of * in the first line of the second triangle is 7. The stop value is 0. The last element is not executed, hence the last value is 1. The step value is -2 as the number of * in successive lines is decreasing by 2.





7. This statement is written under there for loop. Here we are printing the spaces and *. Here we are replicating the space with j value and * with i value like we already did in step 3. The i value is decreasing from 7 to 1 by 2 by the for loop as the number of * is decreasing each line by 2.



8. This statement is written under the for loop ( step 6). As the number of spaces in successive lines increases by 1, the j value should increase by 1. We are doing that by this statement. This means the 'j' value increases by 1 for every change in the  'i' value by -2.

Output :




Comments

Popular posts from this blog

Python Program to Print Fibonacci series - Codicaly

Python - Program to find the given number is a prime or composite number

Python Program To Replace The Elements of List With its Cube