Computer Science Grade 9 20 min

Creating Simple Patterns

Creating Simple Patterns

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Analyze a visual pattern and break it down into a row-and-column structure. Implement nested loops to control the rows and columns of a character-based pattern. Use the outer loop's iteration variable to dynamically control the number of iterations in the inner loop. Modify loop variables to print numerical patterns instead of character patterns. Debug common errors in pattern-printing code, such as incorrect newline placement and off-by-one errors. Construct simple geometric patterns like squares, right-angled triangles, and pyramids using code. Ever wondered how games create repeating backgrounds or how text can form cool art? 🎨 It's all about telling the computer to repeat simple instructions in a clever way! In this lesson, you'll mas...
2

Key Concepts & Vocabulary

TermDefinitionExample Pattern LogicThe set of rules that defines how a pattern is constructed, specifically how many rows and columns it has and what character or number appears at each position.For a 4x4 square of asterisks, the logic is: 'Create 4 rows. In each row, create 4 columns. Place an asterisk in every position.' Nested LoopA loop that is placed inside the body of another loop. The inner loop executes completely for each single iteration of the outer loop.for i in range(3): # Outer loop for j in range(2): # Inner loop print(i, j) # This line runs 3 * 2 = 6 times. Outer LoopIn a nested loop structure, this is the primary loop that controls the overall number of major repetitions. In pattern creation, it almost always controls the number of rows.In a 5-row...
3

Core Syntax & Patterns

The Row-Column Nested Loop Structure Outer loop controls rows. Inner loop controls columns. A newline is printed after the inner loop finishes. This is the fundamental structure for creating any 2D pattern. The outer loop iterates once per row. Inside it, the inner loop iterates to print all the characters for that single row. After the inner loop is done, a `print()` statement moves to the next line to start the next row. The Dynamic Inner Loop Condition for row in range(N): for col in range(row + 1): To create patterns where the number of columns changes with each row (like a triangle), the inner loop's range should depend on the outer loop's iteration variable. Using `range(row + 1)` makes the number of columns equal to the current row number (plus one, sinc...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
To create a hollow N x N square pattern, a condition is needed to print a star on the border and a space inside. Which `if` condition correctly identifies the border of a square where `row` and `col` range from 0 to N-1?
A.if row == 0 or row == N-1 or col == 0 or col == N-1
B.if row == col or row + col == N-1
C.if row > 0 and row < N-1
D.if col > 0 and col < N-1
Challenging
Which code snippet would produce the following numerical pattern? 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1
A.for i in range(1, 6): for j in range(1, i + 1): print(j, end=' ')
B.for i in range(5): for j in range(i + 1): print(5 - j, end=' ')
C.for i in range(5, 0, -1): for j in range(5, i - 1, -1): print(j, end=' ')
D.for i in range(5): for j in range(5, i, -1): print(j, end=' ')
Challenging
A student's code correctly prints a right-angled triangle. To transform it into a pyramid, what is the most critical missing component in their outer loop's logic?
A.An `if-else` statement to handle the tip of the pyramid differently.
B.second inner loop that prints asterisks, running after the first one.
C.loop that prints a calculated number of leading spaces before the asterisks are printed.
D.Changing the `end=' '` parameter to `end='*'`.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.