0% found this document useful (0 votes)
5 views4 pages

Pattern Programs With Comments

The document provides a series of Python programs that generate various star patterns, including decreasing and increasing triangles, diamond shapes, and right-aligned structures. Each pattern is accompanied by comments explaining the logic and structure of the code. The output for each pattern is also included to illustrate the result of the code.

Uploaded by

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

Pattern Programs With Comments

The document provides a series of Python programs that generate various star patterns, including decreasing and increasing triangles, diamond shapes, and right-aligned structures. Each pattern is accompanied by comments explaining the logic and structure of the code. The output for each pattern is also included to illustrate the result of the code.

Uploaded by

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

Pattern Programs in Python with

Comments
Pattern 1: Decreasing then Increasing Stars with Spacing

n = 4
for i in range(n):
# Print leading spaces based on the current row
for j in range(n):
if i == j:
print(" " * i, end=" ")
# Print decreasing stars
for k in range(n):
if i == k:
print("* " * (n - i), end="")
print()

for i in range(1, n):


# Print spaces for lower half
for j in range(1, n):
if i == j:
print(" " * (n - i), end="")
# Print increasing stars
for k in range(n):
if i == k:
print("* " * (i + 1), end="")
print()
# Output:
# * * * *
# * * *
# * *
# *
# * *
# * * *
# * * * *

Pattern 2: Diamond-like Structure

n = 4
for i in range(n - 1):
# Print spaces for upper half
for j in range(n - 1):
if i == j:
print(" " * (n - i), end="")
# Print increasing stars
for k in range(n):
if i == k:
print("* " * (i + 1), end="")
print()

for i in range(n):
# Print leading spaces for lower half
for j in range(n):
if i == j:
print(" " * i, end=" ")
# Print decreasing stars
for k in range(n):
if i == k:
print("* " * (n - i), end="")
print()
# Output:
# *
# * *
# * * *
# * * * *
# * * *
# * *
# *

Pattern 3: Upper Inverted Triangle

n = 4
rows = n
cols = n
for i in range(rows):
# Print spaces based on row
for j in range(cols):
if i == j:
print(" " * i, end="")
# Print decreasing stars
for k in range(cols):
if i == k:
print("* " * (n - i), end="")
print()
# Output:
# * * * *
# * * *
# * *
# *
Pattern 4: Right-aligned Increasing Triangle

n = 4
rows = n
cols = n
for i in range(rows):
for j in range(cols):
if i == j:
print(" " * (n - i), end="")
for k in range(cols):
if i == k:
print("* " * (i + 1), end="")
print()
# Output:
# *
# * *
# * * *
# * * * *

Pattern 5: Decreasing Triangle (Aligned Right)

n = 4
rows = n
cols = n
for i in range(rows):
for j in range(cols):
if i == j:
print(" " * i, end="")
for k in range(cols):
if i == k:
print("*" * (n - i), end="")
print()
# Output:
# ****
# ***
# **
# *

Pattern 6: Right-Aligned Increasing Triangle (Solid Stars)

n = 4
rows = n
cols = n
for i in range(rows):
for j in range(cols):
if i == j:
print(" " * (n - i), end=" ")
for k in range(cols):
if i == k:
print("*" * (i + 1), end="")
print()
# Output:
# *
# **
# ***
# ****

Pattern 7: Left-aligned Decreasing Triangle

n = 4
rows = n
cols = n
for i in range(rows):
for j in range(cols):
if i == j:
print("*" * (n - i), end="")
print()
# Output:
# ****
# ***
# **
# *

Pattern 8: Left-aligned Increasing Triangle

n = 4
rows = n
cols = n
for i in range(rows):
for j in range(cols):
if i == j:
print("*" * (i + 1), end="")
print()
# Output:
# *
# **
# ***
# ****

You might also like