π Python Pattern Challenge — Day 24
Pattern printing is a simple and effective way to improve your Python loops, repetition, and logical thinking. For Day 24, let's move from numbers to alphabets and create a simple alphabet pattern.
π― Today's Challenge
Write a Python program to print exactly this pattern:
A A B A B C A B C D A B C D E A B C D E F
Can you create it with the cleanest Python code? π
Solution 1 — Using Nested for Loops
for i in range(1, 7): for j in range(i): print(chr(65 + j), end=" ") print()
How it works
The outer loop controls the number of rows:
for i in range(1, 7):The inner loop controls how many letters appear in each row:
for j in range(i):The expression:
chr(65 + j)converts numbers into uppercase alphabets.
For example:
65 → ASo each row contains one additional letter.
Solution 2 — Using a String
letters = "ABCDEF" for i in range(1, 7): print(" ".join(letters[:i]))
Output
AHere, slicing selects the required number of letters from the string.
For example:
letters[:4]gives:
ABCDSolution 3 — Using a Function
def alphabet_pattern(n): for i in range(1, n + 1): for j in range(i): print(chr(65 + j), end=" ") print() alphabet_pattern(6)
Using a function makes the pattern reusable.
Try:
alphabet_pattern(10)to create a larger pattern.
⚡ Short & Clean Code
for i in range(1, 7): print(*"ABCDEF"[:i])
π₯ Just two lines are enough to generate the complete pattern.
π Challenge Yourself
Can you modify this pattern:
- Continue the pattern from A to Z?
- Print the alphabets in reverse order?
- Use lowercase letters?
- Create the same pattern using a while loop?
- Make every row start with a different alphabet?
- Create a pattern using numbers and alphabets together?
Drop your solution below! π
24 Days. 24 Patterns. Stronger Python Logic. ππ₯
Learn • Practice • Grow with CLCODING π

0 Comments:
Post a Comment