If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To print a pyramid pattern in Python, you can use nested loops to control the number of spaces and stars (or any other character) in each row. Here's an example of how to create a pyramid pattern using nested loops:
pythonCopy code
def pyramid_pattern(size): for i in range(1, size + 1): print(" " * (size - i) + "* " * i) # Example usage: size_of_pyramid = 5 pyramid_pattern(size_of_pyramid)
Output:
markdownCopy code
* * * * * * * * * * * * * * *
In this example, the
pyramid_pattern()
function takessize
as input, which represents the number of rows in the pyramid. The function uses afor
loop to iterate from 1 tosize
. For each iteration, the loop calculates the number of spaces required before the stars using the expressionsize - i
. The*
character is then repeatedi
times in each row using the expression"* " * i
.The
print()
statement is used to print the spaces and stars in the required format to create the pyramid pattern. The*
character is followed by a space to separate each star.When you call the
pyramid_pattern()
function withsize_of_pyramid = 5
, it will create a pyramid pattern with 5 rows, as shown in the output.You can modify the
size_of_pyramid
variable to create pyramid patterns of different sizes or replace the "*" character with any other character or string to create patterns using different symbols.
Comments: 0