Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed.
Some of the Patterns are shown in this article.
Also Read:PYTHON SCHOOL
Also Check:PYTHON TUTORIALS
Simple pyramid pattern
Also Check:PYTHON TUTORIALS
Also Read:PYTHON SCHOOL
After 180 degree rotation
Also Read:PYTHON SCHOOL
Also Check:PYTHON TUTORIALS
Printing Triangle
Also Check:PYTHON TUTORIALS
Also Read:PYTHON SCHOOL Numbers without re assigning
Also Read:PYTHON SCHOOL
Also Check:PYTHON TUTORIALS
Character Pattern
Also Check:PYTHON TUTORIALS
Also Read:PYTHON SCHOOL Continuous Character pattern
If you Participated in our Python school programme and want certificate please Click Here.
Some of the Patterns are shown in this article.
Also Read:PYTHON SCHOOL
Also Check:PYTHON TUTORIALS
Simple pyramid pattern
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern def pypart(n): # outer loop to handle number of rows # n in this case for i in range ( 0 , n): # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # printing stars print ( "* " ,end = "") # ending line after each row print ( "\r" ) # Driver Code n = 5 pypart(n)
Output:
|
Also Read:PYTHON SCHOOL
After 180 degree rotation
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern def pypart2(n): # number of spaces k = 2 * n - 2 # outer loop to handle number of rows for i in range ( 0 , n): # inner loop to handle number spaces # values changing acc. to requirement for j in range ( 0 , k): print (end = " " ) # decrementing k after each loop k = k - 2 # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # printing stars print ( "* " , end = "") # ending line after each row print ( "\r" ) # Driver Code n = 5 pypart2(n)
Output:
|
Also Check:PYTHON TUTORIALS
Printing Triangle
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern triangle def triangle(n): # number of spaces k = 2 * n - 2 # outer loop to handle number of rows for i in range ( 0 , n): # inner loop to handle number spaces # values changing acc. to requirement for j in range ( 0 , k): print (end = " " ) # decrementing k after each loop k = k - 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # printing stars print ( "* " , end = "") # ending line after each row print ( "\r" ) # Driver Code n = 5 triangle(n)
Output:
|
Also Read:PYTHON SCHOOL Numbers without re assigning
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern of numbers def contnum(n): # initializing starting number num = 1 # outer loop to handle number of rows for i in range ( 0 , n): # not re assigning num # num = 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # printing number print (num, end = " " ) # incrementing number at each column num = num + 1 # ending line after each row print ( "\r" ) n = 5 # sending 5 as argument # calling Function contnum(n)
Output:
|
Also Check:PYTHON TUTORIALS
Character Pattern
# Python 3.x code to demonstrate star pattern # Function to demonstrate printing pattern of alphabets def alphapat(n): # initializing value corresponding to 'A' # ASCII value num = 65 # outer loop to handle number of rows # 5 in this case for i in range ( 0 , n): # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # explicitely converting to char ch = chr (num) # printing char value print (ch, end = " " ) # incrementing number num = num + 1 # ending line after each row print ( "\r" ) # Driver Code n = 5 alphapat(n)
Output:
|
Also Read:PYTHON SCHOOL Continuous Character pattern
# Python code 3.x to demonstrate star pattern # Function to demonstrate printing pattern of alphabets def contalpha(n): # initializing value corresponding to 'A' # ASCII value num = 65 # outer loop to handle number of rows - for i in range ( 0 , n): # inner loop to handle number of columns # values changing acc. to outer loop for j in range ( 0 , i + 1 ): # explicitely converting to char ch = chr (num) # printing char value print (ch, end = " " ) # incrementing at each column num = num + 1 # ending line after each row print ( "\r" ) # Driver code n = 5 contalpha(n)
Output:
|
No comments:
Post a Comment