Tuesday, 13 October 2026

๐Ÿ Python Pattern Challenge — Day 25

 




๐Ÿ Python Pattern Challenge — Day 25

Pattern printing is a fun way to improve your Python loops, number sequences, and logical thinking. For Day 25, let's create a beautiful Number Pyramid Pattern ๐Ÿ”บ.

This pattern starts with 1 at the top and expands row by row. Each row increases up to a middle number and then decreases symmetrically, creating a pyramid-like design.

๐ŸŽฏ Today's Challenge

Write a Python program to print the following pattern:

 

Best and cleanest code will be rewarded! ๐Ÿ†

Solution 1 — Using Nested for Loops

n = 5 for i in range(1, n + 1): print(" " * (n - i), end="") for j in range(1, i + 1): print(j, end=" ") for j in range(i - 1, 0, -1): print(j, end=" ") print()








How It Works

1. Controlling the rows

for i in range(1, n + 1):

This loop creates five rows, increasing the value of i from 1 to 5.

2. Adding spaces

print(" " * (n - i), end="")

Spaces move each row toward the center, forming the pyramid shape.

3. Printing increasing numbers

for j in range(1, i + 1): 
    print(j, end=" ")

This loop prints numbers from 1 up to the current row number.

4. Printing decreasing numbers

for j in range(i - 1, 0, -1): 
    print(j, end=" ")

This loop prints the numbers in reverse order, without repeating the middle number.

For example, when i = 4, the row becomes:

1 2 3 4 3 2 1

Solution 2 — Using a Simple List

for i in range(1, 6): row = list(range(1, i + 1)) row += list(range(i - 1, 0, -1)) print(" " * (5 - i) + " ".join(map(str, row)))





Here, range() creates the increasing and decreasing sequences, while map(str, row) converts the numbers into strings for joining.

Solution 3 — Using a Function

def number_pyramid(n): for i in range(1, n + 1): left = list(range(1, i + 1)) right = list(range(i - 1, 0, -1)) row = left + right print(" " * (n - i) + " ".join(map(str, row))) number_pyramid(5)







You can change number_pyramid(5) to number_pyramid(7) to create a larger pyramid.

⚡ Short & Clean Code

for i in range(1, 6): print(" " * (5-i), *range(1, i+1), *range(i-1, 0, -1))




This compact version generates the same number pyramid using Python's unpacking operator.

๐Ÿš€ Challenge Yourself

Can you modify this pattern?

  • Increase the pyramid to 10 rows.

  • Create the same pattern using a while loop.

  • Replace numbers with alphabets.

  • Print the pyramid upside down.

  • Create a similar pattern using * symbols.

Drop your solution below! ๐Ÿ‘‡

25 Days. 25 Patterns. Stronger Python Logic. ๐Ÿ๐Ÿ”ฅ

Learn • Practice • Grow with CLCODING ๐Ÿš€


Projects: 107 Pattern Plots Using Python

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (348) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (437) Data Strucures (20) Deep Learning (222) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (79) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (408) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1383) Python Coding Challenge (1276) Python Library (23) Python Mathematics (21) Python Mistakes (51) Python Pattern Challenge (22) Python Quiz (658) Python Tips (114) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)