0% found this document useful (0 votes)
3 views5 pages

Complete Python Patterns With Output

The document contains various Python code snippets that generate different patterns using asterisks, including shapes like pyramids, triangles, and diamonds. Each code snippet is accompanied by its expected output, demonstrating how the patterns are formed based on the value of 'n'. The patterns range from simple shapes like right triangles to more complex designs like butterflies and sandglasses.

Uploaded by

Prity
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)
3 views5 pages

Complete Python Patterns With Output

The document contains various Python code snippets that generate different patterns using asterisks, including shapes like pyramids, triangles, and diamonds. Each code snippet is accompanied by its expected output, demonstrating how the patterns are formed based on the value of 'n'. The patterns range from simple shapes like right triangles to more complex designs like butterflies and sandglasses.

Uploaded by

Prity
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

1.

Pyramid

Code:
n = 5
for i in range(n):
print(" "*(n-i-1) + "* "*(2*i+1))

Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

2. Right Triangle

Code:
n = 5
for i in range(n):
print("* "*(i+1))

Output:
*
* *
* * *
* * * *
* * * * *

3. Left Triangle

Code:
n = 5
for i in range(n):
print(" "*(n-i-1) + "* "*(i+1))

Output:
*
* *
* * *
* * * *
* * * * *

4. Right Downward Triangle

Code:
n = 5
for i in range(n):
print("* "*(n-i))

Output:
* * * * *
* * * *
* * *
* *
*

5. Left Downward Triangle

Code:
n = 5
for i in range(n):
print(" "*i + "* "*(n-i))

Output:
* * * * *
* * * *
* * *
* *
*

6. Double Hill

Code:
n = 5
for i in range(n):
print("* "*(i+1) + " "*(n-i-1) + "* "*(i+1))
Output:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *

7. Reverse Pyramid

Code:
n = 5
for i in range(n):
print(" "*i + "* "*(2*(n-i)-1))

Output:
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

8. Butterfly

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

Output:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
9. Diamond

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

Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

10. Sandglass

Code:
n = 5
for i in range(n):
print(" "*i + "* "*(n-i))
for i in range(1,n):
print(" "*(n-i-1) + "* "*(i+1))

Output:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

11. Left Pascal's Triangle

Code:
n = 5
for i in range(n):
print("* "*(i+1))
for i in range(n-1):
print("* "*(n-i-1))

Output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

12. Right Pascal's Triangle

Code:
n = 5
for i in range(n):
print(" "*(n-i-1) + "* "*(i+1))
for i in range(n-1):
print(" "*(i+1) + "* "*(n-i-1))

Output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

You might also like