Built with Tutify.io

    AIStudyLabs

    Print Number Pyramid

    Print Number Pyramid

    Introduction

    In this lesson, you will learn how to use nested loops to create intricate number patterns that form a pyramid. Building on your knowledge of loops, string formatting, and user input, you'll generate number sequences to dynamically adjust the height and width of your pyramids. By the end of this lesson, you'll have a deeper understanding of how to manipulate nested structures and format output in Python to achieve visually appealing results.

    Creating Number Pyramid Patterns

    To create a number pyramid, we need to structure the numbers in increasing and then decreasing order, symmetrically around the centerline of each row. This can be achieved through careful alignment using spaces and numbers.

    Pyramid Example Code

    Let's construct a pyramid pattern using numbers:

    def print_number_pyramid(n):
        for i in range(1, n + 1):
            # Print leading spaces
            for j in range(n - i):
                print(' ', end=' ')
            # Print ascending numbers
            for k in range(1, i + 1):
                print(k, end=' ')
            # Print descending numbers
            for l in range(i - 1, 0, -1):
                print(l, end=' ')
            # Move to the next line
            print()
    
    # Example usage
    print_number_pyramid(5)
    

    In this code:

    Dynamic Adjustment of Pyramid Dimensions

    To make our pyramid more versatile, we can allow user input to define the pyramid's height dynamically. This is a useful feature in real-world applications where output needs customizability.

    User Input for Pyramid Height

    def main():
        height = int(input("Enter the height of the pyramid: "))
        print_number_pyramid(height)
    
    if __name__ == '__main__':
        main()
    

    Here, user input dictates the pyramid's height, showcasing the power of combining loops with flexible parameters to produce varied results.

    Utilizing String Formatting for Output

    Proper string formatting enhances the readability of the pyramid. Python provides a range of formatting options to ensure your output is well-aligned and aesthetic.

    Simple Formatting Example

    Employ string formatting to control the output:

    for i in range(1, n + 1):
        print(" " * (n - i), end=' ')
        print(" ".join(str(x) for x in range(1, i + 1)), end=' ')
        print(" ".join(str(x) for x in range(i - 1, 0, -1)))
    

    This approach maintains consistent spacing, ensuring numbers in each row are aligned symmetrically.

    Summary / Key Takeaways

    In Lesson 18: Print Number Pyramid, you have learned:

    Master Python by Writing 50 Programs — Loops and Mathematical Programs

    Python Basics Programs

    Number-Based Programs

    Loops and Mathematical Programs

    String-Based Programs

    List-Based Programs

    Dictionary and Set Programs

    Chapter 7: File Handling and Real Programs

    Tutorial Not Found