Lesson Notes By Weeks and Term v5 - Grade 10

Solution development: algorithmic thinking and introductory programming – Week 2 focus

Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.

Subject: Information Technology

Class: Grade 10

Term: 2nd Term

Week: 2

Theme: General lesson support

Lesson Video

This page supports the lesson note with a companion video and a short classroom-ready summary.

For class groups and homework, share this lesson page so learners also get the summary, objectives, and full lesson context.

Performance objectives

Lesson summary

This week, we delve deeper into algorithmic thinking and introductory programming. Last week, we introduced the concepts of algorithms and flowcharts. This week, we'll focus on translating those logical steps into actual code, specifically using Python, a beginner-friendly and powerful language. We'll be looking at fundamental programming constructs: variables, data types, input and output, and basic arithmetic operations. These skills are vital, not just for IT, but for problem-solving in general. Understanding how to break down a problem into manageable steps and then translate those steps into instructions a computer can follow is increasingly important in our technology-driven world.

Lesson notes

2.1 Variables and Data Types Variables: A variable is like a labelled container in your computer's memory that stores a value. We use variables to hold information that our program needs to work with. In Python, you don't need to declare the type of a variable explicitly; Python infers it based on the value assigned to it.

Data Types: Data types classify the kind of value a variable can hold.

Common data types in Python include: Integer (int): Whole numbers (e.g., -5, 0, 100).

Float (float): Numbers with decimal points (e.g., 3.14, -2.5, 0.0).

String (str): Sequences of characters, enclosed in single or double quotes (e.g., "Hello", 'World', "123").

Boolean (bool): Represents truth values: either `True` or `False`.

Example 1: Declaring and assigning variables ```python age = 16 # Integer variable height = 1.75 # Float variable (height in meters) name = "Zola" # String variable is_student = True # Boolean variable print(age) # Output: 16 print(height) # Output: 1.75 print(name) # Output: Zola print(is_student) # Output: True ``` Explanation: We use the `=` (assignment) operator to assign values to variables. Python automatically determines the data type of each variable based on the assigned value. The `print()` function displays the value of a variable to the console.

Example 2: Determining data types ```python x = 5 y = "5" print(type(x)) # Output: print(type(y)) # Output: ``` Explanation: The `type()` function tells you the data type of a variable. Note the difference between `5` (integer) and `"5"` (string). 2.2 Input and Output Input: Allows the program to get information from the user (e.g., through the keyboard). The `input()` function is used to get input from the user as a string.

Output: Allows the program to display information to the user (e.g., on the screen). The `print()` function is used to display output.

Example 3: Getting input from the user ```python name = input("Enter your name: ") age = input("Enter your age: ") print("Hello, " + name + "! You are " + age + " years old.") ``` Explanation: The `input()` function displays a prompt to the user (e.g., "Enter your name: ") and waits for the user to type something and press Enter. The value entered by the user is returned as a string and assigned to the variable.

Important: The `input()` function always returns a string. If you need a number, you must convert the string to an integer or float using `int()` or `float()`.

Example 4: Converting input to a number ```python price = input("Enter the price of the item: ") price = float(price) # Convert the string to a float quantity = input("Enter the quantity: ") quantity = int(quantity) # Convert the string to an integer total_cost = price * quantity print("The total cost is:", total_cost) ``` Explanation: `float(price)` converts the string stored in the `price` variable to a floating-point number. `int(quantity)` converts the string stored in the `quantity` variable to an integer. We can then perform arithmetic operations with the converted numbers. 2.3 Basic Arithmetic Operations Python supports the following basic arithmetic operations: Addition (+): Adds two numbers.

Subtraction (-): Subtracts one number from another. Multiplication (\): Multiplies two numbers.

Division (/): Divides one number by another (results in a float).

Floor Division (//): Divides one number by another and rounds down to the nearest integer. Modulo (%) : Returns the remainder of a division. Exponentiation (\\*): Raises a number to a power.

Example 5: Arithmetic operations ```python num1 = 10 num2 = 3 sum_result = num1 + num2 # 13 difference = num1 - num2 # 7 product = num1 * num2 # 30 division = num1 / num2 # 3.3333333333333335 floor_division = num1 // num2 # 3 remainder = num1 % num2 # 1 power = num1 ** num2 # 1000 print("Sum:", sum_result) print("Difference:", difference) print("Product:", product) print("Division:", division) print("Floor Division:", floor_division) print("Remainder:", remainder) print("Power:", power) ``` Explanation: Each line demonstrates a different arithmetic operation. Pay attention to the difference between `/` (division) and `//` (floor division). 2.4 Syntax and Syntax Errors Syntax: Refers to the rules that govern the structure of a programming language. Just like grammar in English, syntax dictates how you must write your code for the computer to understand it.

Syntax Errors: Occur when you violate the syntax rules of the programming language. Python will display an error message, preventing your program from running.

Common Syntax Errors: Misspelled keywords: E.g., `prnt("Hello")` instead of `print("Hello")`.

Missing colons: Required at the end of control flow statements (e.g., `if`, `for`, `while`).

Incorrect indentation: Python uses indentation to define code blocks. Inconsistent indentation will cause errors.

Unclosed parentheses or quotes: E.g., `print("Hello)` or `print(Hello")`.