Day11-Day20 Python Pattern Questions
Index: Click to Navigate:
Question11:
Print Hollow Square pattern using python.
Solution:
print("* " + " " *(n-2) + "*")
Pattern Output (for n = 5)
* * * * *
* *
* *
* *
* * * * *
Explanation:
* * * * *
* *
* *
* *
* * * * *
Explanation:
Step 1: Understanding n
-
n = 5
means we want a 5x5 square.
-
So, the output will have 5 rows and 5 columns.
n = 5
means we want a 5x5 square.
Step 2: Looping Through Rows
We use a for
loop to print
n
rows:
for i in range(1, n+1):
-
i
represents the current row number (starts from1
and goes ton
).
Step 3: Printing the Stars (*
)
Now, we check if it’s the first or last row:
if i == 1 or i == n:
print("* " * n)
✅ First (i == 1
) and Last (i == n
) rows:
- Print 5 stars because the top and bottom should be solid.
-
"* " * n
means"* * * * * "
(5 stars).
For middle rows (i = 2, 3, 4
), we print stars only
at the beginning and end, leaving spaces in between:
print("* " + " " * (n-2) + "*")
-
First
*
(left border) -
(n-2)
spaces (to make it hollow) -
Last
*
(right border)
How It Works Step-by-Step:
For n = 5
:
-
Row 1 (
i == 1
) → Print* * * * *
-
Row 2 (
i = 2
) → Print* *
-
Row 3 (
i = 3
) → Print* *
-
Row 4 (
i = 4
) → Print* *
-
Row 5 (
i == 5
) → Print* * * * *
-----------------------------------------------------------------------------------------------------------------------------------
Day- 12
Print Inverted Pyramid pattern using python.
* * * * *
* * * *
* * *
* *
*
Explanation:
-
Outer loop (
for i in range(n, 0, -1)
)-
This loop starts from
n
(5) and goes down to1
. -
It controls the number of stars (
*
) printed in each row.
-
This loop starts from
-
Printing Spaces (
" " * (n - i)
)-
The number of spaces at the beginning increases as
i
decreases. -
For the first row (
i = 5
),n - i = 5 - 5 = 0
spaces. -
For the second row (
i = 4
),n - i = 5 - 4 = 1
space. - And so on...
-
The number of spaces at the beginning increases as
-
Printing Stars (
"* " * i"
) -
The number of stars printed is equal to
i
in each row. -
In the first row,
i = 5
, so* * * * *
. -
In the second row,
i = 4
, so* * * *
. -
This continues until the last row (
i = 1
), which prints just one star.
triangle = [[1] * (i + 1) for i in range(n)]
for i in range(2, n):
for j in range(1, i):
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]
for i in range(n):
triangle = [[1] * (i + 1) for i in range(n)]
What happens here?
- This creates a list of lists (
triangle
), where each row initially contains only1
s. - The number of elements in each row is
i + 1
. - Before filling values, the structure looks like this:
triangle = [ [1], # Row 0 [1, 1], # Row 1 [1, 1, 1], # Row 2 [1, 1, 1, 1],# Row 3 [1, 1, 1, 1, 1] # Row 4 ]
Step 2: Compute Values Using Pascal’s Triangle Rule
for i in range(2, n):
for j in range(1, i):
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]
How does it work?
- Skip first two rows (
i = 0
andi = 1
) since they always contain1
s. - For each row (
i
), update elements (j
) using the formula: - It sums the two values directly above the current position.
- After filling in the values,
triangle
becomes:triangle = [ [1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1] ]
Step 3: Print Pascal’s Triangle in a Properly Formatted Way
for i in range(n):
print(" " * (n - i), *triangle[i])
Explanation:
" " * (n - i)
→ Adds spaces for center alignment.- More spaces at the top, fewer at the bottom.
- This ensures a pyramid-like shape.
*triangle[i]
→ Prints the row’s values cleanly.- The
*
operator unpacks the list and prints numbers without brackets or commas.
- The
Final Output for n = 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
---------------------------------------------------------------------------------------------------------------
Question 14:
Print 'A' pattern using python.
Explanation:
for i in range(1, n + 1):
- The loop runs from
i = 1
toi = n
(1
to7
in this case). - Each iteration corresponds to one row in the pyramid.
Step 2: Printing Leading Spaces
print(" " * (n - i), end ="")
- This adds spaces before the stars (
*
) to align the pyramid properly. - Example when
n = 7
:i = 1
→" "
(6 spaces)i = 2
→" "
(5 spaces)i = 3
→" "
(4 spaces)- …
i = 7
→" "
(0 spaces)
Step 3: Inner Loop for Printing *
and Spaces
for j in range(1, 2*i):
- Runs from
1
to2*i - 1
→ This ensures an increasing odd number of characters in each row. - Example (
n = 7
)i = 1
→j = 1
→ 1 character (*
)i = 2
→j = 1 to 3
→ 3 characters (* *
)i = 3
→j = 1 to 5
→ 5 characters (* *
)- … (keeps increasing)
Step 4: Conditions for Printing *
or Spaces
if j == 1 or j == 2*i - 1:
print("*", end="")
- Prints
*
at the first (j == 1
) and last (j == 2*i - 1
) position of each row to create a hollow effect. - For
i = 3
:* *
- For
i = 4
:* *
Step 5: Special Middle Row Condition
elif i == (n // 2) + 1 and j > 1 and j < (2*i) - 1 and j % 2 != 0:
print("*", end="")
- This affects only the middle row (
i == (n//2) + 1
).- Since
n = 7
, middle row isi = (7//2) + 1 = 4
.
- Since
- Fills
*
at odd positions between the first and last*
in the row. - Example middle row (
i = 4
):* * * *
Step 6: Printing Spaces Elsewhere
else:
print(" ", end="")
- If none of the above conditions match, print a space (
" "
). - Creates the hollow effect.
Final Output for n = 7:
*
* *
* *
* * * *
* *
* *
* *
---------------------------------------------------------------------------------------------------------------
n = 7 # Defines the size of the pattern (number of rows)
First Loop (Inverted Pyramid)
for i in range(n, 0, -1):
print(" " * (n - i) + "* " * i)
How It Works:
- This loop decreases from
n
down to1
, forming an inverted pyramid (upper part of the hourglass). " " * (n - i)
adds leading spaces to shift the stars towards the right."* " * i
printsi
stars followed by a space (*
).
Iteration Breakdown (for n = 7
)
i | Spaces (" " * (n-i) ) |
Stars ("* " * i ) |
Output |
---|---|---|---|
7 | " " * 0 | "* * * * * * * " | * * * * * * * |
6 | " " * 1 | "* * * * * * " | * * * * * * |
5 | " " * 2 | "* * * * * " | * * * * * |
4 | " " * 3 | "* * * * " | * * * * |
3 | " " * 4 | "* * * " | * * * |
2 | " " * 5 | "* * " | * * |
1 | " " * 6 | "* " | * |
Second Loop (Upright Pyramid)
for j in range(2, n+1):
print(" " * (n - j) + "* " * j)
How It Works:
- This loop increases from
2
ton
, forming a normal pyramid (lower part of the hourglass). " " * (n - j)
adds leading spaces for alignment."* " * j
printsj
stars followed by a space (*
).
Iteration Breakdown (for n = 7
)
j | Spaces (" " * (n-j) ) |
Stars ("* " * j ) |
Output |
---|---|---|---|
2 | " " * 5 | "* * " | * * |
3 | " " * 4 | "* * * " | * * * |
4 | " " * 3 | "* * * * " | * * * * |
5 | " " * 2 | "* * * * * " | * * * * * |
6 | " " * 1 | "* * * * * * " | * * * * * * |
7 | " " * 0 | "* * * * * * * " | * * * * * * * |
Final Output for n = 7:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
-----------------------------------------------------------------------------------------------------------
Day - 16
Question16:
Print Inverted Alphabet triangle pattern using python
Solution:
n = 5
for i in range(n, 0, -1):
for j in range(i):
print(chr(65 + j), end=" ")
print()
Explanation:
Outer Loop (for i in range(n, 0, -1)
)
- This loop runs from
n
down to 1 (n=5
to1
), controlling the number of rows. - It decreases
i
in each iteration, reducing the number of characters printed.
Inner Loop (for j in range(i)
)
- This loop prints characters starting from 'A' (
chr(65)
) up tochr(65 + i - 1)
. - The value of
j
starts at 0 and goes up toi-1
, ensuring the correct number of characters is printed.
print(chr(65 + j), end=" ")
chr(65 + j)
converts numbers 0,1,2... into 'A', 'B', 'C'....end=" "
keeps all characters in the same row, avoiding line breaks.
print()
(outside the inner loop)
- Moves to the next line after printing one row.
Output for n = 5:
A B C D E
A B C D
A B C
A B
A
---------------------------------------------------------------------------
Day - 17
Question17:
Print Zig Zag pattern using python
Solution:
n = 10
for i in range(n):
print(" " * (i % 2) + chr(65 + i))
Explanation:
Loop from 0 to n-1 (i.e., 10 iterations)
The variable i takes values from 0 to 9.
Character Calculation
chr(65 + i) converts i into an uppercase letter using ASCII.
chr(65) → 'A', chr(66) → 'B', chr(67) → 'C', and so on.
Adding Spaces Based on i Being Even or Odd
" " * (i % 2) adds two spaces when i is odd (i % 2 == 1).
When i is even, i % 2 == 0, so " " * 0 results in no spaces.
Pattern Output for n = 10:
A
B
C
D
E
F
G
H
I
J
Why This Works?
🔹 The
" " * (i % 2)
dynamically adds spaces for odd values ofi
,creating a zig-zag effect.
🔹 This approach removes the need for an
if-else
condition,making the code more optimized and concise.
---------------------------------------------------------------------------------------------------------------
Day -18Question 18:Print Diamond Number Pattern using PythonSolution:n = 5 # Upper part of the diamond for i in range(1, n + 1): print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1))) # Lower part of the diamond for i in range(n - 1, 0, -1): print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))Explanation:
n = 5
The variablen
defines the size of the diamond, wheren
represents thewidest row (middle row of the diamond).Step 1: Constructing the Upper Part
for i in range(1, n + 1): print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))
How does this loop work?
- This loop constructs the upper half (including the middle row) of the diamond.
i
represents the row number (starting from 1)." " * (n - i)
adds leading spaces to center-align the numbers." ".join(str(j) for j in range(1, i + 1))
prints numbers from 1 to i,
✅ By the end of this loop, the upper half of the diamond is printed.
Step 2: Constructing the Lower Part
for i in range(n - 1, 0, -1):
print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))
How does this loop work?
- This loop constructs the lower half of the diamond.
- It starts at
n - 1
(4
in this case) and goes down to1
(excluding0
). " " * (n - i)
ensures proper indentation." ".join(str(j) for j in range(1, i + 1))
prints numbers from 1 toi
.
✅ By the end of this loop, the lower half is printed.
Final Output (For n = 5
):
11 21 2 31 2 3 41 2 3 4 51 2 3 41 2 31 21
-----------------------------------------------------------------------------------------------------------------------------
Day - 19
Question-19:Print Butterfly pattern using python
Solution:
n = 5 for i in range(n+1): print("*" * i +" "*(n-i)+"*" * i ) for j in range(n-1,0,-1): print("*" * j+" "*(n-j)+"*" * j)
Explanation:
n = 5
n
defines the height of the patternStep 1: Constructing the Upper Part
for i in range(n + 1): print("*" * i + " " * (n - i) + "*" * i)
Breakdown of this loop
- The loop runs from
i = 0
toi = n
(i.e.,0
to5
). "*" * i
→ Printsi
stars on the left." " * (n - i)
→ Prints double spaces (" "
) in the middle."*" * i
→ Printsi
stars on the right.
✅ By the end of this loop, the upper half is printed.
Step 2: Constructing the Lower Part
for j in range(n - 1, 0, -1):
print("*" * j + " " * (n - j) + "*" * j)
Breakdown of this loop
- The loop runs from
j = n - 1
down to1
(i.e.,4
to1
). "*" * j
→ Printsj
stars on the left." " * (n - j)
→ Prints double spaces (" "
) in the middle."*" * j
→ Printsj
stars on the right.
✅ By the end of this loop, the lower half is printed.
Pattern Explanation:
- The first loop prints an increasing number of
*
symbols from left and right,
- The second loop mirrors the first loop to create a symmetric shrinking pattern.
- The result is a butterfly-like shape with spaces in the middle.
Final Output for n = 5
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
----------------------------------------------------------------------------------------------------------------
Day - 20
Question-20:
Print Mirror Number Pyramid using python
Solution:
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()
Explanation:Step 1: Understanding the Outer Loop
for i in range(1, n + 1):
- The loop runs from
1
to n
(i.e., 1
to 5
).
- Each iteration represents a new row in the pattern.
Step 2: Printing Leading Spaces
print(" " * (n - i), end="")
" " * (n - i)
→ Prints double spaces to align the numbers properly.
- The number of spaces decreases as
i
increases, centering the pyramid.
Step 3: Printing Ascending Numbers
for j in range(1, i + 1):
print(j, end=" ")
- This prints numbers from
1
to i
.
- For example, when
i = 3
, it prints:
1 2 3
Step 4: Printing Descending Numbers
for j in range(i - 1, 0, -1):
print(j, end=" ")
- This prints numbers from
i-1
back to 1
, creating symmetry.
- For example, when
i = 3
, it prints:
2 1 (after printing 1 2 3
in the previous step).
Step 5: Moving to the Next Line
print()
- Moves the cursor to the next line for the next row.
Output for n = 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Comments
Post a Comment