Patterns using python

PHOTO EMBED

Fri Jul 22 2022 17:53:08 GMT+0000 (Coordinated Universal Time)

Saved by @ITSMERAFAY #python

## simple square pattern ##

n = 5
for i in range(n):              ## this plays as a row ##
    for j in range(n):          ## this plays as a column ##
        print("*",end = " " )   ## end showing stars with gap ##
    print()                     ## this print is necessary to write to print for next line ##
    
    
################################################################################################

## simple triangle pattern ##


n = 5
for i in range(n):            ## n is already printing 5 rows ##         
    for j in range(i+1):      ## j is set for i + 1 so that every line will add 1 star bcz of column is set for row + 1 star   
        print("*",end = " " ) ## same ##
    print()                   ## same ## 
    
################################################################################################


n = 5
for i in range(n):              # same bcz of 5 star printing 
    for j in range(i,n):        # in this we have given the range from i means it will go first for 0 to 5 then 1 to 5 ..... 4 to 5 this happens due to  
        print("*",end = " " )   # range given a/c to i and stop it to max 5 . In this way it will go like  first 0 index then columns go for 0 to 5 that 
    print()                     # gradually down to 4 to 5 and print only 1 star in the end



################################################################################################

### now the case is diff , now for this we'll set the space as decreasing triangle and and set the triangle from left side ### 


n = 5
for i in range(n):             
    for j in range(i,n):       ###  (0,5)--(0,1,2,3,4)  ###       
        print(" ",end = " " )  ### till here pretend that it has printed the above star pattern but with space ### 
    for j in range(i+1):    
        print("*",end = " " )  ### from here it prints the star pattern for ###
    print()
    
    

################################################################################################


### HILL pattern ###


n = 5
for i in range(n):
    for j in range(i,n):   ### 01234 ### first 0 to 4 then 1 to 4 then .....3 to 4 ### here it will print space of decreasing triangle ###
        print(" ", end = " ")
    for j in range(i):          ###we take i to print one less column to make edge ### here it will print * star in increasing triangle order with right ###
        print("*" ,end = " ")
    for j in range(i + 1):       ### here it will print * star in increasing order with left ###
        print("*" , end = " ")  
        
    print()



################################################################################################


content_copyCOPY