Studyfin Studyfin ✨ Sign up free

Intro to Python

Grade 6 · Coding · Free lesson

Hi there! I am Studyfin, your coding buddy. Today, we will use a fun software design process to build cool patterns with nested loops in Python!

Before writing code, we use a software design process. First, we define our real-world problem. Then, we break it down into smaller subproblems before writing any code.

ai generated

A nested loop is simply a loop inside another loop! Each time the outer loop takes one step, the inner loop runs all of its steps from start to finish. This is perfect for grids, clocks, or drawing shapes.

OUTER LOOP (Rows) INNER LOOP (Columns)

Let us explain how this solves subproblems. If we want to print a grid of stars, the outer loop handles the 'row' subproblem. The inner loop handles the 'character in each row' subproblem.

Row 1: Row 2: Row 3:
✏️ Worked example

Use the software design process to create a text program that prints a 3x3 square of hash tags (#) using nested loops.

  1. 1. Define the problem: We need to print three rows, and each row must contain exactly three '#' symbols.
  2. 2. Identify Subproblem A (Rows): We need a loop that repeats three times to create our three rows.
  3. 3. Identify Subproblem B (Columns): Inside each row, we need a loop that prints '#' three times next to each other.
  4. 4. Write the outer loop in Python: 'for row in range(3):' to start our rows.
  5. 5. Nest the inner loop inside: 'for col in range(3):' to print each '#' character.
  6. 6. Complete the program: Use 'print("#", end="")' inside the inner loop, and 'print()' in the outer loop to start a new line.
snake
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons