Shape

Python Shape Programming Assignments body { font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; } pre { background: #f4f4f4; padding: 10px; border-radius: 5px; overflow-x: auto; } .assignment { border: 1px solid #ddd; padding: 15px; margin-bottom: 20px; border-radius: 5px; background: #f9f9f9; } .solution { display: none; background: #fff3cd; padding: 10px; border: 1px solid #ffeeba; border-radius: 5px; margin-top: 10px; } button { background: #007bff; color: white; border: none; padding: 10px; cursor: pointer; border-radius: 5px; } button:hover { background: #0056b3; } function getPassword() { const date = new Date(); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, ‘0’); const day = String(date.getDate()).padStart(2, ‘0’); const dow = date.getDay(); // Sunday=0, Monday=1, …, Saturday=6 return `${year}-${month}-${day}-${dow}`; } function showSolution(id) { const userInput = prompt(“Enter the password to view the solution:”); if (userInput === getPassword()) { document.getElementById(id).style.display = “block”; } else { alert(“Incorrect password! Try again.”); } }

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()