Master Python through visual patterns and logical thinking. Click on any question to reveal the solution with smooth animation.
Each solution includes detailed explanation
Print stars in increasing order per row.
*
**
***
****
for i in range(1, 5):
print("*" * i)
Explanation: The outer loop runs 4 times (i = 1 to 4). In each iteration, we print '*' multiplied by i, which creates the increasing triangle pattern.
Print stars in decreasing order per row.
****
***
**
*
for i in range(4, 0, -1):
print("*" * i)
Explanation:
Using range(4, 0, -1) gives us values 4, 3, 2, 1.
We print '*' multiplied by i in each iteration, creating the decreasing pattern.
Align stars to the right with spaces.
*
**
***
****
for i in range(1, 5):
print(" " * (4 - i) + "*" * i)
Explanation: We print spaces before the stars to align them to the right. The number of spaces decreases as the number of stars increases.
Create a symmetrical pyramid pattern.
*
***
*****
*******
for i in range(1, 5):
print(" " * (4 - i) + "*" * (2*i - 1))
Explanation: The pattern uses the formula (2*i - 1) to determine the number of stars in each row. This creates an odd number sequence: 1, 3, 5, 7 stars.