Introduction
This article shows how to draw different patterns in a Python console program.
Pattern 1
- str=""
- for i in range(1,6):
- for j in range(1,i+1):
- str=str+"*"
- str=str+"\n"
-
- print(str)
Output
Pattern 2
- str=""
- for i in range(6,1,-1):
- for j in range(1,i):
- str=str+"*"
- str=str+"\n"
- print(str)
Output
Pattern 3
- str=""
- for i in range(1,6):
- for space in range(1,6-i):
- str=str+" "
- for j in range(1,i+1):
- str=str+"*"
- str=str+"\n"
- print(str)
-
Output
Pattern 4
- str=""
- for i in range(6,1,-1):
- for space in range(6-i,0,-1):
- str=str+" "
- for j in range(1,i):
- str=str+"*"
- str=str+"\n"
- print(str)
Output
Pattern 5
- rows=int(input("Enter the number of rows :"));
- str=""
- for i in range(1,rows+1):
- for space in range(1,rows+1-i):
- str=str+" "
- for j in range(0,2*i-1):
- str=str+"*"
- str=str+"\n"
- print(str)
Output
Pattern 6
- for i in range(rows,0,-1):
- for space in range(rows-i):
- str=str+" "
- for j in range(i,2*i):
- str=str+"* "
- for k in range(0,i-1):
- str=str+"* "
- str=str+"\n"
- print(str)
Output
Pattern 7: The following program prints Pascal Triangle.
- import math
- coef = 1
- rows = int(input("Enter the number of rows :"))
- stringVal = ""
- for i in range(rows):
- for space in range(1,rows - i + 1):
- stringVal = stringVal + " "
- for j in range(0,i + 1):
- if(i == 0 or j == 0):
- coef = 1
- else:
- coef = coef * (i - j + 1) / j
-
- temp = coef
- TotalSpace=0
- while(temp != 0):
- TotalSpace = TotalSpace + 1
- temp = int(math.floor(temp / 10))
- p=0
- while((p+TotalSpace)!=4):
- stringVal = stringVal + " "
- p=p+1
- stringVal = stringVal + str(int(math.floor(coef)))
- stringVal = stringVal + "\n"
- print(stringVal)
Output:
Pattern 8: The following program prints floyd's triangle.
- rows = int(input("Enter the number of rows :"))
- stringVal = ""
- k=0
- for i in range(1,rows+1):
- for j in range(1,i+1):
- stringVal=stringVal+str(k+j)+" "
- k=k+1;
- stringVal=stringVal+"\n"
- print(stringVal)
Output
Thanks!