Friday, 25 September 2026

🐍 Python Pattern Challenge — Day 13

 

🐍 Python Pattern Challenge — Day 13

Pattern printing is one of the best ways to improve your Python logic, loops, spacing control, and problem-solving skills. Today's challenge focuses on creating a beautiful Number Diamond Pattern, where the same number is repeated in each row, forming a symmetric diamond shape.

The pattern grows from 1 to 4 and then shrinks back to 1, making it a great exercise for understanding both increasing and decreasing sequences.

Today's Challenge

Write a Python program to print:



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


Solution 1 — Using Nested Loops

n = 4 # Upper Half for i in range(1, n + 1): for j in range(n - i): print(" ", end="") for j in range(i): print(i, end=" ") print() # Lower Half for i in range(n - 1, 0, -1): for j in range(n - i): print(" ", end="") for j in range(i): print(i, end=" ")









How it works

The upper half prints:

1 2 2 3 3 3 4 4 4 4





while the lower half prints:

3 3 3 2 2 1




The first loop controls the spaces and the second loop prints the current number repeatedly.


Solution 2 — Using String Multiplication

n = 4 for i in range(1, n + 1): print(" " * (n - i) + (str(i) + " ") * i) for i in range(n - 1, 0, -1): print(" " * (n - i) + (str(i) + " ") * i)






How it works

" " * (n - i)

creates the leading spaces.

(str(i) + " ") * i

prints the number multiple times.

For example:

i = 3

produces:

3 3 3

Solution 3 — Using a Single Loop

n = 4 rows = [1, 2, 3, 4, 3, 2, 1] for i in rows: print(" " * (n - i) + (str(i) + " ") * i)






How it works

The list:

[1, 2, 3, 4, 3, 2, 1]

already contains the exact sequence needed for the diamond.

Each value controls:

  • Number printed
  • Number of repetitions
  • Indentation of the row

This makes the code simple and easy to understand.


⚡ Short & Clean Code

for i in [1,2,3,4,3,2,1]: print(" "*(4-i) + f"{i} "*i)



πŸ”₯ Just one loop generates the entire pattern.


πŸš€ Challenge Yourself

Can you create:

1  2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1






Or try:

  • Replacing numbers with letters
  • Taking n from user input
  • Creating a hollow diamond version
  • Printing only even numbers
  • Solving it using a while loop

Drop your solution below! πŸ‘‡

13 Days. 13 Patterns. Stronger Python Logic. 🐍πŸ”₯

Learn • Practice • Grow with CLCODING πŸš€


200 Days Python Coding Challenges with Explanation

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (345) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (14) 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 (434) Data Strucures (19) 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) Generative AI (77) 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 (406) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1377) Python Coding Challenge (1251) Python Library (8) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (12) Python Quiz (641) Python Tips (112) 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)