0% found this document useful (0 votes)
9 views6 pages

Pattern Programs

The document contains various pattern programs written in Python that demonstrate how to print different shapes and sequences using loops. It includes examples of star patterns, number patterns, and aligned outputs with spaces. Each section provides a brief explanation of the logic behind the code and the expected output.

Uploaded by

Naseem Ahamad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Pattern Programs

The document contains various pattern programs written in Python that demonstrate how to print different shapes and sequences using loops. It includes examples of star patterns, number patterns, and aligned outputs with spaces. Each section provides a brief explanation of the logic behind the code and the expected output.

Uploaded by

Naseem Ahamad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#pattern programs

# *
# * *
# * * *
# * * * *
# * * * * *

for row in range(1, 6): #1, 2, 3, 4, 5


print("* " * row)

#row
#1 "* " * 1 = *
#2 "* " * 2 = * *
#3 "* " * 3 = * * *
#4 "* " * 4 = * * * *
#5 "* " * 5 = * * * * *
##########################################
print()
# * * * * *
# * * * *
# * * *
# * *
# *
for row in range(5, 0, -1): #5, 4, 3, 2, 1
print("* " * row)

#row
#5 "* " * 5 = * * * * *
#4 "* " * 4 = * * * *
##############################################
# *
# * *
# * * *
# * * * *
# * * * * *

space = 4
for star in range(1, 6): #1, 2, 3, 4, 5
print((" " * space) + ("* " * star))
space -= 1

#space = 4
# star = 1
#" " * 4 + "* " * 1 = *
#4-1 = 3
##################################################
print()
# 1
# 2 2
# 3 3 3
# 4 4 4 4
# 5 5 5 5 5

for num in range(1, 6):


print((str(num) + " ") * num)

for number in range(1, 6):


print(f"{number} " * number)

#first iteration
# num = 1
#str(num) = "1"
#str(num) + " " = "1 "
#str(num) + " " * num = "1 " * 1
# output = 1
###############################################
print()
# 5
# 4 4
# 3 3 3
# 2 2 2 2
# 1 1 1 1 1
for num in range(5, 0, -1): #5, 4, 3, 2, 1
print((str(num) + " ") * (6-num))

#first iteration
#num = 5
#str(num)="5"
#str(num) + " " ="5 "
#str(num) + " " * (6-num)= "5 " * 6-5="5 " * 1
#output = 5

#second iteration
# num = 4
##################################
print()
# 1 1 1 1 1
# 2 2 2 2 2
# 3 3 3 3 3
# 4 4 4 4 4
# 5 5 5 5 5
n = 5
for num in range(1, 6): #1, 2, 3, 4, 5
print((str(num) + " ") * n)

#first iteration
#n = 5
#num = 1
#str(num) = "1"
#str(num) + " " = "1 "
#str(num) + " " * n= "1 " * 4
#ouput 1 1 1 1 1
###############################################
print()
# 1 1 1 1 1
# 2 2 2 2
# 3 3 3
# 4 4
# 5
n = 5
for num in range(1, 6):
print((str(num) + " ") * n)
n -= 1

#method 2
for n in range(1, 6):
print((str(n) + " ") * (6-n))
################################################
#Assignment
# 1 1 1 1 1
# 2 2 2 2
# 3 3 3
# 4 4
# 5
space = 0
n = 5
for num in range(1, 6):
print((" " * space) + (str(num) + " ") *n)
space += 1
n -= 1

#first
#num = 1
#(" " * space) = (" " * 0) = ""
#str(num) = str(1) = "1"
#str(num) + " " = "1" + " " = "1 "
#str(num) + " " * n = "1 " * 5
#ouput =1 1 1 1 1

#second iteration
# num = 2
# space = 1
# n = 4
#(" ") * 1 = " "
#str(num) = "2"
#str(num) + " " = "2 "
#str(num) + " " * n = "2 " * 4
# output = 2 2 2 2
#############################################
# 5 4 3 2 1
# 5 4 3 2 1
# 5 4 3 2 1
# 5 4 3 2 1
for row in range(4): #0, 1, 2, 3
for num in range(5, 0, -1): #5, 4, 3, 2, 1
print(num, end = " ")
print()
#############################################
print()
#5 4 3 2 1
#5 4 3 2
#5 4 3
#5 4
#5
for row in range(5): #0, 1, 2, 3, 4
for num in range(5, row, -1):
print(num, end = " ")
print()

#firts iteration
# row = 0
#range(5, 0,-1)
# 5, 4, 3, 2, 1
#secon iteration
# row =1
# range(5, 1, -1)
#5 4 3 2
#########################################3
#1 2 3 4 5
#2 3 4 5
#3 4 5
#4 5
#5
for row in range(1, 6):
for num in range(row, 6):
print(num, end = " ")
print()

#first iteration
#row = 1
# range(1, 6)
#ouput = 1, 2, 3, 4, 5

#second iteration
# row = 2
#range(2, 6)
#2, 3, 4, 5
###############################################
# *
# ***
# *****
# *******
# *********
n = 5
for i in range(1, n+1): #(1, 6) 1, 2, 3, 4, 5
print(" " * (n-i) + "*" *(2 * i -1))
#first iteration
# n = 5
# i = 1
#" " * n-i = " " * 5-1 = " " * 4 = " "
#"*" *(2 *i-1) = "*" * (2 * 1 - 1) = "*" * 1 = "*"
# *
#secon iteration
#n = 5
# i = 2
#" " ( n-i) = " " * 5-2 = 3 = " " * 3 = " "
#"*" *(2*2-1) = "*" *4-1 = "*" * 3= "***"

############################################
print()
# *
# ***
# *****
# *******
# *********
# *******
# *****
# ***
# *
n = 5
for i in range(1, n+1): #(1, 6) 1, 2, 3, 4, 5
print(" " * (n-i) + "*" *(2 * i -1))
for i in range(n-1,0,-1): #(4,0,-1)#4, 3, 2, 1
print(" " * (n - i) + "*" * (2 * i - 1))
################################################
# 1
# 2 3
# 4 5 6
# 7 8 9 10
# 11 12 13 14 15
n = 5
num = 1
for row in range(1, n+1):
for j in range(1, row + 1):
print(num, end = " ")
num += 1
print()

#row = 1
# (1, 1+1)
# (1, 2)
# 1
# num = num + 1
# 1+1 = 2
##############################################################
##

You might also like