Making patterns with python

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

Khushwant Soni
19 February 2018 | Khushwant Soni
   

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:

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:

Right angled Triangle

Example 1:

size = int(input("Enter the Height of Triangle: "))
for i in range(size):
	print ((i+1)*'*')

Explanation:

Output:

Enter the Height of Triangle: 5
*
**
***
****
*****

Example 2:

size = int(input("Enter the Height of Triangle: "))
for i in range(size):
	print (((size-(i+1))*" ")+(i+1)*'*')

Explanation:

Output:

Enter the Height of Triangle: 7
      *
     **
    ***
   ****
  *****
 ******
*******

Pyramids

Example 1:

size = int(input("Enter the Height of Pyramid: "))
for i in range(size):
	print (((size-(i+1))*" ")+(i+1)*'* ')

Explanation:

Output:

Enter the Height of Pyramid: 7
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

Example 2:

size = int(input("Enter the Height of Pyramid: "))
for i in range(size):
	print (((size-(i+1))*" ")+((((i+1)*2)-1)*'*'))

Explanation:

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

Explanation:

Output:

Enter the Size of diamond: 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *




       
outboxcraft logoOutboxcraft
Sitemap