Example 3: Print stars in the following arrangement.

Print stars in the following arrangement:

stars

# 03Loops example_03_print stars.py

stars = 6

for i in range(1,stars): # This for loop defines the number of rows we want

   for j in range(1,i+1):     # This loop tells how many stars(*) per row

       print '*',     # The comma(,) indicates no new line after printing

   print               # After a row is printed, start a new line

In this example of nested-loops, the inner loop controls how many stars(*) each row has. For instance, we want to print 3 stars in the 3rd row. Hence, when i = 3, the inner loop becomes “for j in range(1,4): print ‘*’,” which means 3 stars will be printed in this row.