Star Patterns in Python

John Doe ·

961 Views

1.

*
**
***
****
*****
****
***
**
*
i=1
while i <=4:
    print("*" * i)
    i+=1
i = 5
while i > 0:
    print("*" * i)
    i -= 1

# or

i = 1
j = 6
while i < j * 2 - 1:
    if i < j :
        print('*' * i)
    else:
        print('*' * (j - i + j - 2))
    i += 1

2.

*
**
***
****
*****
i = 0
while(i < 5):
    i += 1
    print('*' * i)

3.

*****
****
***
**
*
for x in range(5, 0, -1):
    for y in range(x):
        print("*", end="")
    print()

4.

    *
   **
  ***
 ****
*****
*****
 ****
  ***
   **
    *
spaces = 5
for x in range(5):
    for y in range(spaces-1):
        print(" ", end="")
    for z in range(0, x + 1):
        print("*", end="")
    print()
    spaces -= 1

spaces = 0
for x in range(5,0,-1):
    for y in range(spaces):
        print(" ", end="")
    for z in range(0,x):
        print("*", end="")
    print()
    spaces+=1

5.

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
rows = 5
k = 0
for i in range(1, rows + 1):
    for j in range (1, (rows - i) + 1):
        print(end = " ")

    while k != (2 * i - 1):
        print("*", end = "")
        k = k + 1
    k = 0
    print()

k = 2
m = 1
for i in range(1, rows):
    for j in range (1, k):
        print(end = " ")
    k = k + 1

    while m <= (2 * (rows - i) - 1):
        print("*", end = "")
        m = m + 1
    m = 1
    print()