Thursday, 24 September 2026

🐍 Python Pattern Challenge — Day 12

 




🐍 Python Pattern Challenge — Day 12

Pattern printing is a great way to improve your Python logic, nested loops, mathematical thinking, and problem-solving skills. For Day 12, let's move beyond simple star patterns and create a number pattern based on Pascal's Triangle.

The challenge is to generate each row dynamically, where every number is calculated from the values of the previous row.

Today's Challenge

Write a Python program to print:

 




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


Solution 1 — Using Nested for Loops

n = 6 for i in range(n): num = 1 print(" " * (n - i - 1), end="") for j in range(i + 1): print(num, end=" ") num = num * (i - j) // (j + 1) print()






How it works:

The variable num starts with:

num = 1

For every next value, we calculate:

num = num * (i - j) // (j + 1)

This formula generates the next value of the current Pascal's Triangle row.

For example:

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1




The spacing:

" " * (n - i - 1)

keeps the triangle centered.


Solution 2 — Using Lists

n = 6 row = [1] for i in range(n): print(" " * (n - i - 1), end="") print(*row) row = [ row[j] + row[j + 1] for j in range(len(row) - 1) ] row = [1] + row + [1]






How it works:

We start with:

row = [1]

Then every new row is created by adding neighboring values from the previous row.

For example:

1 3 3 1

produces:

1 4 6 4 1

because:

1 + 3 = 4 
3 + 3 = 6 
3 + 1 = 4

Then 1 is added to both ends.


Solution 3 — Using a Function

def pascal(n): row = [1] for i in range(n): print(" " * (n - i - 1), end="") print(*row) row = [1] + [ row[j] + row[j + 1] for j in range(len(row) - 1) ] + [1] pascal(6)







How it works:

Putting the pattern inside a function makes it reusable.

You can easily change:

pascal(6)

to:

pascal(10)

to generate more rows.


⚡ Short & Clean Code

r = [1] for i in range(6): print(" " * (5-i), *r) r = [1] + [r[j] + r[j+1] for j in range(i)] + [1]





πŸ”₯ This compact version generates the same Pascal's Triangle pattern using a single main loop.


πŸš€ Challenge Yourself

Can you modify this pattern:

  • Generate 10 or 15 rows?
  • Create Pascal's Triangle using only nested loops?
  • Take the number of rows using input()?
  • Print the triangle upside down?
  • Replace the numbers with *?
  • Calculate the sum of every row?
  • Find the largest number in the generated triangle?

Drop your solution below! πŸ‘‡

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

Learn • Practice • Grow with CLCODING

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 (433) 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 (405) 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 (11) Python Quiz (640) 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)