Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Function to print full pyramid pattern
def full_pyramid(n):
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")

# Print asterisks for the current row
for k in range(1, 2*i):
print("*", end="")
print()
Comment on lines +2 to +11
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make use of Pythonic features to write more effective & clean code.

Suggested change
def full_pyramid(n):
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print asterisks for the current row
for k in range(1, 2*i):
print("*", end="")
print()
def full_pyramid(n):
for i in range(1, n + 1):
# Print leading spaces
print(" " * (n - i), end="")
# Print asterisks for the current row
print("*" * (2*i-1))

Tip

Just for educational purposes, here's an oneliner version:

(lambda n : print('\n'.join([ f'{" " * (n-i)}{"*" * (2*i-1)}' for i in range(1, n+1) ])))(int(input('Size of triangle: ')))


full_pyramid(5)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a habit of using name-main idiom, even though it feels like it won't make much difference in scripts like this one.

Suggested change
full_pyramid(5)
if __name__ == "__main__":
full_pyramid(5)

Loading