Class 11 Practical Programs
Generate Patterns Using Nested Loop
AIM
To write Python programs to generate different patterns using nested loops including:
- • Star Pattern (1 to 5 stars)
- • Reverse Number Pattern (12345 to 1)
- • Alphabet Pattern (A to ABCDE)
ALGORITHM
- 1: Start
- 2: Accept user choice
- 3: Based on choice, execute respective pattern logic:
- For Star Pattern:
- - Use outer loop from 1 to 5
- - Use inner loop from 1 to current row number
- - Print star followed by space
- For Reverse Number Pattern:
- - Use outer loop from 5 to 1 (decreasing)
- - Use inner loop from 1 to current row number
- - Print numbers without space
- For Alphabet Pattern:
- - Use outer loop from 1 to 5
- - Use inner loop from 1 to current row number
- - Convert numbers to alphabets using chr(64+j)
- 4: Print newline after each row
- 5: Stop
PROGRAM
# Pattern Generation Program using Nested Loops
print("\nStar Pattern:")
for i in range(1, 6):
for j in range(1, i + 1):
print("*", end=" ")
print() # New line after each row
print("\nReverse Number Pattern:")
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print() # New line after each row
print("\nAlphabet Pattern:")
for i in range(1, 6):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print() # New line after each row
OUTPUT
Pattern 1: Star Pattern
Star Pattern: --------------- * * * * * * * * * * * * * * *
Pattern 2: Reverse Number Pattern
Reverse Number Pattern: ------------------------- 12345 1234 123 12 1
Pattern 3: Alphabet Pattern
Alphabet Pattern: ------------------ A AB ABC ABCD ABCDE
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
Q1. What is a nested loop and why is it used in pattern generation?
A nested loop is a loop inside another loop. It's used in pattern generation because we need to control both rows (outer loop) and columns (inner loop) to create 2D patterns.
Q2. How does the chr() function work in alphabet pattern generation?
chr() function converts ASCII values to characters. chr(65) gives 'A', chr(66) gives 'B', etc. We use chr(64 + j) where j starts from 1 to get alphabets A, B, C...
Q3. What is the difference between print() and print(end='') in pattern programs?
print() adds a newline character after printing, while print(end='') doesn't add newline. We use end='' to print elements in the same line and print() to move to next line.
Q4. How do you create a reverse number pattern like 12345, 1234, 123?
Use outer loop with range(5, 0, -1) to go from 5 to 1, and inner loop with range(1, i+1) where i is the current outer loop value.
Q5. What is the ASCII value of 'A' and how is it used in alphabet patterns?
ASCII value of 'A' is 65. We use chr(64 + j) or chr(65 + j - 1) to convert numbers to alphabets starting from A.
Q6. How can you modify the star pattern to create a right-aligned triangle?
Add spaces before stars using another inner loop: for k in range(5-i): print(' ', end='') before printing stars.
Q7. What happens if you use range(1, 6) vs range(5, 0, -1) in outer loop?
range(1, 6) creates increasing pattern (1,2,3,4,5 rows), while range(5, 0, -1) creates decreasing pattern (5,4,3,2,1 rows).
Q8. How do you create a pattern with both numbers and alphabets in the same row?
Use two inner loops or combine both in one loop: print(j, chr(64+j), end=' ') to print both number and corresponding alphabet.
Theory Topics
MCQs & Practice
Video Tutorial
Pattern Generation in Python - Complete TutorialLearn nested loops and pattern creation
Quick Tips
- Use nested loops: outer for rows, inner for columns
- chr(65) = 'A', chr(66) = 'B' for alphabet patterns
- Use end='' to print in same line, print() for new line
- range(5,0,-1) creates decreasing sequence