Pyramid Pattern In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 48:- Pyramid Pattern In Python

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 takes size as input, which represents the number of rows in the pyramid. The function uses a for loop to iterate from 1 to size. For each iteration, the loop calculates the number of spaces required before the stars using the expression size - i. The * character is then repeated i 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 with size_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.

6. Loops

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?