0% found this document useful (0 votes)
38 views10 pages

Star Pattern in Python: Esha Gupta

they are good

Uploaded by

X
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)
38 views10 pages

Star Pattern in Python: Esha Gupta

they are good

Uploaded by

X
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/ 10

Star Pattern in Python

Esha Gupta
Associat e Senior Execut ive
Updated on Sep 12, 2024 16:22 IST
Pattern programs in Python are a type of problem that uses loops to
produce different patterns of numbers, stars (*), or other characters. In this
blog, we will explore the star pattern in Python further.

Star pattern in Python are popular for practising nested loop control structures.
They involve using loops to print out a particular pattern of stars (*) on the screen.

Basic Approach to Solve a Star Pattern in Python


Using an Example
A step-by-step guide using a simple example: Butterf ly Pattern

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Steps to Solve

1. Understand the Pattern

The butterfly pattern consists of two symmetrical halves with increasing and
decreasing star counts.

2. Initialize the Loop for Rows

Set up loops for the upper and lower halves.

Copy code

n = 5 # Total rows in each half

3. Nested Loop for Printing Stars and Spaces.

Use nested loops to manage the stars and spaces.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code

# Upper half of the butterfly


for i in range(1, n + 1):
# Print left stars
for j in range(i):
print("*", end="")
# Print spaces
for j in range(2 * (n - i)):
print(" ", end="")
# Print right stars
for j in range(i):
print("*", end="")
print()

# Lower half of the butterfly


for i in range(n, 0, -1):
# Print left stars
for j in range(i):
print("*", end="")
# Print spaces
for j in range(2 * (n - i)):
print(" ", end="")
# Print right stars
for j in range(i):
print("*", end="")
print()

Complete Code

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code

n=5

# Upper half of the butterfly


for i in range(1, n + 1):
for j in range(i):
print("*", end="")
for j in range(2 * (n - i)):
print(" ", end="")
for j in range(i):
print("*", end="")
print()

# Lower half of the butterfly


for i in range(n, 0, -1):
for j in range(i):
print("*", end="")
for j in range(2 * (n - i)):
print(" ", end="")
for j in range(i):
print("*", end="")
print()

This basic approach can be adapted to solve various other star patterns in Python.

Top Star Patterns in Python


Square Star Pattern
Inverted Pyramid Star Pattern

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Pyramid Star Pattern
Diamond Star Pattern

Hollow Square Star Pattern

Let’s understand each of these one by one in detail :


1. Square Star Pattern: This pattern forms a square with stars.

Code

Copy code

for i in range(5):
for j in range(5):
print("*", end="")
print()

2. Inverted Pyramid Star Pattern: This pattern forms an inverted pyramid with
stars.

Code

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Copy code

for i in range(5):
for j in range(i):
print(" ", end="")
for j in range(i, 5):
print("* ", end="")
print()

3. Pyramid Star Pattern: This pattern forms a pyramid with stars.

Code

Copy code

for i in range(5):
for j in range(5, i, -1):
print(" ", end="")
for k in range(i + 1):
print("* ", end="")
print()

4. Diamond Star Pattern: This pattern prints a diamond shape with stars.

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Code

Copy code

n=5

# Upper half of the diamond


for i in range(n):
for j in range(n - 1, i, -1):
print(" ", end="")
for k in range(i + 1):
print("* ", end="")
print()

# Lower half of the diamond


for i in range(1, n):
for j in range(i):
print(" ", end="")
for k in range(n - 1, i - 1, -1):
print("* ", end="")
print()

5. Hollow Square Star Pattern: This pattern prints a square but leaves the

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
middle portion hollow.

Code

Copy code

n=5

for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()

Cont rol St at ement s in C | Meaning and Types


Co ntro l statements in C are used to determine the o rder in which the
instructio ns within a pro gram are executed. They are o f three types: Selectio n,
Iteratio n & Jump statements. Think...re ad m o re

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
Number Pat t ern Programs in Java
Pattern pro grams in Java are a type o f pro blem that use nested lo o ps to
pro duce different patterns o f numbers, stars (*), o r o ther characters. In this blo g
we will dive...re ad m o re

St ar Pat t ern Programs in Java


Pattern pro grams in Java are a type o f pro blem that uses nested lo o ps to
pro duce different patterns o f numbers, stars (*), o r o ther characters. In this blo g,
we will dive...re ad m o re

Number Pat t ern Programs in C


Pattern pro grams in C are a type o f pro blem that use nested lo o ps to pro duce
different patterns o f numbers, stars (*), o r o ther characters. In this blo g, we will
dive...re ad m o re

Number Pat t ern in Pyt hon


Pattern pro grams in Pytho n are a type o f pro blem that uses nested lo o ps to
pro duce different patterns o f numbers, stars (*), o r o ther characters. In this blo g,
we will dive...re ad m o re

Top 10 Pat t ern Programs in C


Have yo u ever wo ndered ho w intricate patterns can be created with simple
lo o ps in C? Pattern pro grams in C are a fascinating way to learn abo ut nested
lo o ps and co ntro l...re ad m o re

Conclusion
Thus, the star pattern in Python is essential to foundational programming
learning and practice. They help understand the use and manipulation of loops and

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.
control statements and enhance logical thinking and problem-solving skills.

FAQs

What is a Star Pattern in Python?

Why are Star Patterns Important to Learn in Python?

Can Star Patterns be Created Using Dif f erent Types of Loops in


Python?

Are There Variations in Star Patterns?

What are Some Common Challenges Faced While Creating Star


Patterns?

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 13-Sep-20 24.

You might also like