Shubham
Ramdeo


February 19, 2018

Making patterns with python

This article is a guest post by my friend Kushwant Soni, written on my request. Kushwant is eat and breath python, he has dedicated years to master his skills. I hope you like what he has to say.

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
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

You might also like:

(prev)
(next)

(rand) Define Preprocessor
(rand) What is a Database?
(rand) Mathematical Reasoning

© Shubham Ramdeo, 2020
you are really awesome!

Site Designed with Beauter🌸