Making Patterns in Python
Making Rectangles/Squares
These will be probably the most simple patterns to among the ones present in this post.
For Rectangle:
 All you need to draw a rectangle are two things: Length & Breadth.
 We will use only one for loop, having range=Length and Breadth for the multiplication of stars (” * “)
Example:
length = int(input("Enter Length of the rectangle: "))
breadth = int(input("Enter Breadth of the rectangle: "))
for i in range(length):
print(breadth*'*')
Output:
Enter Length of the rectangle: 3
Enter Breadth of the rectangle: 8
********
********
********
For Square:
 Similarly you can easily print pattern for square now. Just take the one variable and use that as a range in for loop as well as multiplication of ” * “.
Right angled Triangle
 In this, we will write a code to print an star pattern which will be in the form of a right angled triangle.
 So we need to take just 1 for loop for the size of triangle.
 Now inside that for loop, we will just focus on number & positions of stars & spaces in the triangle.
Example 1:
size = int(input("Enter the Height of Triangle: "))
for i in range(size):
print ((i+1)*'*')
Explanation:
 Here we took a for loop for the height of the triangle.
 We simply printed “Current Height” no. of stars.
Output:
Enter the Height of Triangle: 5
*
**
***
****
*****

It was quite easy to do as we didn’t had to consider the whitespaces in the pattern.

However, let’s do a better example with considering the spaces as well.
Example 2:
size = int(input("Enter the Height of Triangle: "))
for i in range(size):
print (((size(i+1))*" ")+(i+1)*'*')
Explanation:
 It is just similar to the first one, we just added a logic for the spaces in the for loop before the stars and concatenated them.
 The logic says that there will always be “Maximum HeightCurrent Height” no. of spaces.
Output:
Enter the Height of Triangle: 7
*
**
***
****
*****
******
*******
 Now you can print something similar kind of things, just count the occurrence & position of spaces & stars and perform the task.
Pyramids
 In this, we will write a code to print an star pattern which will be in the form of a pyramid.
 There’s just a little change we have to do in the last code to make a pyramid that consist of *‘‘** along with spaces which will be our first example here.
Example 1:
size = int(input("Enter the Height of Pyramid: "))
for i in range(size):
print (((size(i+1))*" ")+(i+1)*'* ')
Explanation:
 Here, we took a for loop which will iterate for the height of the pyramid.
 Inside the for loop, we first wrote the logic of the spaces. On analyzing the pyramid, you will notice that in every line, there are “Max. Height  Current Height” no. of spaces, so I’ve wrote the same.
 Secondly, I wrote the logic of the stars. You can see that in every line of the pyramid there are “Current Height” no. of stars along with 1 space in the end. Hence we concatenated both of them and built our Pyramid
Output:
Enter the Height of Pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
 In the next example, we will print a pyramid that won’t be having the spaces as we had in the last example.
 we will simply give the height of the pyramid and every level will bee having 2 more elements than the previous one.
Example 2:
size = int(input("Enter the Height of Pyramid: "))
for i in range(size):
print (((size(i+1))*" ")+((((i+1)*2)1)*'*'))
Explanation:
 Just like the last one, we took a for loop here also for the height of pyramid.
 Inside the for loop, we first wrote the logic of the spaces. On analyzing the pyramid, you will notice that in every line, there are “Max. Height  Current Height” no. of spaces, so I’ve wrote the same.
 Secondly, I wrote the logic of the stars. You can see that in every line of the pyramid there are “(Current Height*2)1” no. of stars. Hence we concatenated both of them and built our Pyramid
Output:
Enter the Height of Pyramid: 7
*
***
*****
*******
*********
***********
*************
Diamonds
Example:
n=int(input("Enter the Size of diamond: "))
for i in range(0,n):
print (((n(i+1))*' ')+(((2*i)+1)*'*'))
for i in range(1,n):
print (((i)*' ')+(((((ni)*2)1)*'*')))
Explanation:
 In this code, we used 2 for loops. first one simply prints a pyramid of the size ‘n’.
 Other one print a reversed pyramid of size 1 less than the first one. And hence, our diamond is constructed.
Output:
Enter the Size of diamond: 5
*
***
*****
*******
*********
*******
*****
***
*