To learn programming, programmers must practice to use loops like For Loop and While Loop. Creating patterns is the most preferred method to do this. This is beneficial as you will use nested loops and understand to master loop for better coding. Here we are presenting 20 Programs to Create Star Pattern in Python using For Loop.
In almost all the code examples below, nested for loops are used. Nesting of loop means a loop is embedded within another loop to implement multidimensional aspects of coding logic. When you use arrays, lists, dictionaries etc loops are essential to process the elements of these structures. For loop is an efficient iterative control manage predefined repetitions.
So, here are the 20 programs to create Star Pattern in Python using For Loop.
1- Create a simple star pattern with count of stars incrementing in each iteration
print("------------Pattern 1---------") txt="" for i in range(6): j=0 #create star pattern for one line for j in range(i): txt=txt+"*" #print the created pattern print(txt) #clear variable for next iteration. txt=""
2 A simple star pattern program where count of stars decrement in each iteration
print("------------Pattern 2---------") txt="" i=0 for i in range(5): j=0 #create star pattern for one line for j in range(5-i): txt=txt+"*" #print the created pattern print(txt) #clear variable for next iteration. txt=""
3- A right angled triangle of stars using For loop in Python
print("------------Pattern 3---------") txt="" i=0 for i in range(5): j=0 for j in range(5-i-1): txt=txt+" " for k in range(i+1): txt=txt+"*" print(txt) txt=""
4- An inverted right angled triangle of stars
print("------------Pattern 4---------") txt="" i=0 for i in range(5): j=0 for k in range(i): txt=txt+" " for j in range(5-i): txt=txt+"*" print(txt) txt=""
5 A triangle of stars, 2 stars added in every iteration
print("------------Pattern 5---------") txt="" i=0 for i in range(5): for j in range(4-i): txt=txt+" " for k in range(2*i+1): txt=txt+"*" for l in range(4-i): txt=txt+" " print(txt) j=0 l=0 k=0 txt=""
6 A down pointed triangle of stars pattern
print("------------Pattern 6---------") txt="" i=0 for i in range(5): for j in range(i): txt=txt+" " for k in range(9-2*i): txt=txt+"*" print(txt) j=0 k=0 txt=""
7 -Left moving star pattern
print("------------Pattern 7---------") txt="" i=0 for i in range(5): for j in range(i): txt=txt+" " for k in range(5): txt=txt+"*" print(txt) j=0 k=0 txt=""
8. Right moving star pattern
print("------------Pattern 8---------") txt="" i=0 for i in range(5): for j in range(5-i): txt=txt+" " for k in range(5): txt=txt+"*" print(txt) j=0 k=0 txt=""
9. Empty Right angled triangle of asterisks
print("------------Pattern 9---------") txt="" i=0 for j in range(5): txt=txt+" " print(txt+"*") for i in range(3): j=0 txt="" for j in range(5-i-1): txt=txt+" " txt=txt+"*" for l in range(i): txt=txt+" " txt=txt+"*" print(txt) txt="" j=0 txt=" " for j in range(5): txt=txt+"*" print(txt)
10. Empty Reverse Right angled triangle of asterisks
print("------------Pattern 10---------") txt="" i=0 print("*") for i in range(3): txt=txt+"*" for l in range(i): txt=txt+" " txt=txt+"*" print(txt) txt="" txt="" for j in range(5): txt=txt+"*" print(txt)
11. Empty upside down Right angled triangle of asterisks
print("------------Pattern 11---------") txt="" i=0 for j in range(5): txt=txt+"*" print(txt) for i in range(3): j=0 txt="" txt=txt+"*" for j in range(3-i-1): txt=txt+" " txt=txt+"*" print(txt) txt="" print("*")
12. Triangle of asterisks
print("------------Pattern 12---------") txt="" i=0 for j in range(5): txt=txt+"*" print(txt) i=0 for i in range(3): j=0 txt="" for l in range(i): txt=txt+" " txt=txt+"*" for j in range(3-i): txt=txt+" " txt=txt+"*" print(txt) txt="" j=0 txt="" for j in range(4): txt=txt+" " print(txt+"*")
13. Unfilled Triangle of asterisks
print("------------Pattern 13---------") txt="" i=0 for j in range(4): txt=txt+" " print(txt+"*") txt="" j=0 for i in range(3): for j in range(3-i): txt=txt+" " txt=txt+"*" for k in range(2*i+1): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for j in range(9): txt=txt+"*" print(txt)
14. Upside down empty triangle of asterisks
print("------------Pattern 14---------") txt="" i=0 for j in range(9): txt=txt+"*" print(txt) txt="" j=0 for i in range(3): for j in range(i): txt=txt+" " txt=txt+"*" for k in range(6-2*i): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for j in range(4): txt=txt+" " print(txt+"*")
15. Trapezium of stars
print("------------Pattern 15---------") txt="" i=0 txt="" i=0 for j in range(4): txt=txt+" " print(txt+"*") txt="" j=0 for i in range(3): for j in range(3-i): txt=txt+" " txt=txt+"*" for k in range(2*i+1): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for i in range(4): for j in range(i): txt=txt+" " txt=txt+"*" for k in range(7-2*i): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for j in range(4): txt=txt+" " print(txt+"*")
16. An empty figure of stars
print("------------Pattern 16---------") txt="" i=0 print(txt+"*") txt="" j=0 for i in range(3): txt=txt+"*" for k in range(i): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for i in range(4): txt=txt+"*" for k in range(3-i): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" print(txt+"*")
17. Reversed empty figure of stars
print("------------Pattern 17---------") txt="" i=0 for j in range(4): txt=txt+" " print(txt+"*") txt="" j=0 for i in range(3): for j in range(3-i): txt=txt+" " txt=txt+"*" for j in range(i): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for i in range(4): for j in range(i): txt=txt+" " txt=txt+"*" for j in range(3-i): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for j in range(4): txt=txt+" " print(txt+"*")
18. Empty Parallelogram of stars
print("------------Pattern 18---------") txt="" i=0 for i in range(5): txt=txt+"*" print(txt) txt="" i=0 for i in range(3): for j in range(i+1): txt=txt+" " txt=txt+"*" for k in range(3): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" for i in range(4): txt=txt+" " i=0 for i in range(5): txt=txt+"*" print(txt)
19. Reverse empty Parallelogram of stars
print("------------Pattern 19---------") txt="" i=0 for i in range(4): txt=txt+" " i=0 for i in range(5): txt=txt+"*" print(txt) txt="" i=0 for i in range(3): for j in range(3-i): txt=txt+" " txt=txt+"*" for k in range(3): txt=txt+" " txt=txt+"*" print(txt) j=0 k=0 txt="" i=0 for i in range(5): txt=txt+"*" print(txt)
20. A Star made with stars
print("------------Pattern 20---------") txt="" i=0 j=0 for i in range(5): for j in range(i): txt=txt+" " txt=txt+"*" for k in range(4-i): txt=txt+" " txt=txt+"*" for l in range(4-i): txt=txt+" " txt=txt+"*" print(txt) txt="" i=0 for i in range(11): txt=txt+"*" print(txt) txt="" i=0 txt="" i=0 j=0 for i in range(5): for j in range(4-i): txt=txt+" " txt=txt+"*" for k in range(i): txt=txt+" " txt=txt+"*" for l in range(i): txt=txt+" " txt=txt+"*" print(txt) txt=""
Really interesting post!