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.Computational thinking is used in real-world applications like password strength checkers. In this task, you will write a Python program to check the strength of a password based on these rules:
Open Thonny and type the following code:
# Password Strength Checker
def password_strength_checker():
print("Password Strength Checker")
password = input("Enter a password: ")
# Check password length
if len(password) < 8:
print("Weak password: Must be at least 8 characters long.")
return
# Check for uppercase and lowercase letters
if not any(char.isupper() for char in password) or not any(char.islower() for char in password):
print("Weak password: Must include both uppercase and lowercase letters.")
return
# Check for numbers
if not any(char.isdigit() for char in password):
print("Weak password: Must include at least one number.")
return
print("Strong password!")
# Run the password strength checker
password_strength_checker()
Run your code in Thonny and test it with different passwords. Notice how the program checks for multiple conditions to determine password strength.