Computational thinking involves breaking down problems into smaller, manageable parts (decomposition) and identifying patterns to solve them (pattern recognition). Think of it like solving a jigsaw puzzle—each piece contributes to the bigger picture.
Take a moment to think of a daily activity, like making a cup of tea. How would you break this activity into smaller steps? Write down the steps in your notebook before moving on.
In this step, you will set up the Thonny editor, which is a beginner-friendly Python editor. Follow these steps:
Once Thonny is set up, you're ready to start coding!
Now lets create a simple calculator in Python that can add, subtract, multiply, or divide two numbers. First, break the problem into smaller tasks:
Now, open Thonny and write the code for the calculator:
# Simple Calculator
def calculator():
print("Welcome to the Simple Calculator!")
# Step 1: Get user input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Step 2: Ask for the operation
print("Choose an operation: add, subtract, multiply, divide")
operation = input("Enter the operation: ").lower()
# Step 3: Perform the operation
if operation == "add":
result = num1 + num2
elif operation == "subtract":
result = num1 - num2
elif operation == "multiply":
result = num1 * num2
elif operation == "divide":
if num2 != 0:
result = num1 / num2
else:
result = "Error: Division by zero is not allowed."
else:
result = "Invalid operation."
# Step 4: Display the result
print("Result:", result)
# Run the calculator
calculator()Pattern recognition helps us identify similarities or trends in data. For example, even numbers are divisible by 2, while odd numbers are not. In this task, you will write a Python program to identify whether a number is even or odd. Open Thonny and type the following code:
# Even or Odd Checker
def check_even_odd():
print("Even or Odd Checker")
# Get user input
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")
# Run the checker
check_even_odd()Explanation of the code:
if number % 2 == 0: checks whether the number is divisible by 2 without leaving a remainder. The % symbol is called the modulus operator, and it gives the remainder of a division. If the remainder is 0, the number is even; otherwise, it is odd.You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.