 Fuddlepixel coz tech is fun();

# Making patterns with python

Patterns are fun! Let us discuss some fundamentals on how to implement and print patterns with python.

By Khushwant Soni
19 February 2018 · 5 mins read
# `python` • I guess many of you may have heard of writing a program to print certain kind of star pattern or something like that.
• In this post, we are going to do the same thing which will be done in python which will be very easy for you all to learn and implement by yourself.

• My basic goal is to clear the fundamentals of it, so you can easily do the same thing for some different kind of pattern.
• If you ever get stuck in any kind of pattern, just give me a response and I will be there to help you.

## 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):
``````

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 white-spaces 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 Height-Current 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

• In this, we will write a code to print an star pattern which will be in the form of a diamond.

• We will prompt the user to enter the size of the diamond and we will iterate through it and make a pyramid with incrementing 2 stars every time until we reach the maximum length and then reverse the pyramid to make a diamond.
• Here’s the code to print star pattern of a diamond.

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)*' ')+(((((n-i)*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
*
***
*****
*******
*********
*******
*****
***
*
``````

##### Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

#### Khushwant Soni He eats and drinks python. Dedicated more than 3 years to study python, Khushwant knows what this language is supposed to do.

Meet me!

#### Recent Top