Python Shape Programming Assignments
Below are six Python programming assignments for learning how to draw shapes using loops.
1. Draw a Solid Square
Write a function that prints a solid square of stars.
def draw_solid_square(size):
# Your code here
draw_solid_square(5)
Example Output:
*****
*****
*****
*****
*****
def draw_solid_square(size):
for i in range(size):
print("*" * size)
2. Draw an Outline Square
Write a function that prints a hollow square.
def draw_outline_square(size):
# Your code here
draw_outline_square(5)
Example Output:
*****
* *
* *
* *
*****
def draw_outline_square(size):
for i in range(size):
if i == 0 or i == size - 1:
print("*" * size)
else:
print("*" + " " * (size - 2) + "*")
3. Draw an Inner Empty Square
Write a function that prints a square with a border thickness.
def draw_inner_empty_square(size, thickness):
# Your code here
draw_inner_empty_square(7, 2)
Example Output:
*******
*******
** **
** **
** **
*******
*******
def draw_inner_empty_square(size, thickness):
for i in range(size):
for j in range(size):
if i = size - thickness or j = size - thickness:
print("*", end="")
else:
print(" ", end="")
print()
4. Draw a Primary Diagonal
Write a function that prints a diagonal from top-left to bottom-right.
def draw_primary_diagonal(size):
# Your code here
draw_primary_diagonal(5)
Example Output:
*
*
*
*
*
def draw_primary_diagonal(size):
for i in range(size):
print(" " * i + "*")
5. Draw a Secondary Diagonal
Write a function that prints a diagonal from top-right to bottom-left.
def draw_secondary_diagonal(size):
# Your code here
draw_secondary_diagonal(5)
Example Output:
*
*
*
*
*
def draw_secondary_diagonal(size):
for i in range(size):
print(" " * (size - i - 1) + "*")
6. Draw a Square with Borders and Diagonals
Write a function that prints a square with its borders and both diagonals.
def draw_all_lines(size):
# Your code here
draw_all_lines(5)
Example Output:
*****
** **
* * *
** **
*****
def draw_all_lines(size):
for i in range(size):
for j in range(size):
if i == j or i == (size - j) - 1 or i == 0 or i == size - 1 or j == 0 or j == size - 1:
print("*", end="")
else:
print(" ", end="")
print()