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

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support

Sciaku (सियाकु)

Sciaku (सियाकु) provides you a technical and programming content like Java Programming, Python Programming, C Programming,Android Development, Web Development, etc. Learn how to make software, website, and applications here and also we have industrial internship for you.

Contact

G20, Gopal Vihar Colony, Noida Sector 2, Uttar Pradesh, India, 201301

info@sciaku.com

Copyright © 2022-2025 Created by ❤️ Sciaku

Privacy Policy | Terms & Conditions | Refunds Policy