Grade 11
Course ContentProgramming (Python/Java)
Habari, Future Tech Mchawi! An Introduction to Programming
Ever wondered how your phone works? How M-Pesa sends money in seconds? Or how a website like eCitizen knows your details? It’s not magic, it’s code! Welcome to the world of programming, where you learn to speak the language of computers and tell them exactly what to do. Think of yourself as a director, and the computer is your actor, ready to follow your every command.
In this lesson, we will explore the exciting world of programming using Python, a language famous for being powerful and easy to learn. We'll also take a quick look at its powerful cousin, Java. Get ready to give your first instructions to a computer!
Real-World Scenario: Imagine you are building the next big mobile money app for Kenya. You need to tell the app: "When a user enters a phone number and an amount, and then presses 'Send', you must check if they have enough balance. If they do, send the money and a message. If not, show an error message." These instructions are what we call a program!
What is an Algorithm? The Recipe for Code
Before you write any code, you need a plan. In programming, this plan is called an algorithm. It's just a step-by-step list of instructions to solve a problem. Think of it like a recipe for making chapati:
- Ingredients (Data): Flour, water, salt, oil.
- Steps (Instructions): Mix ingredients, knead the dough, roll it out, cook it on a pan.
- Result (Output): Delicious, hot chapatis!
A computer program works the same way. Let's map out a simple algorithm for a common task: deciding what to wear based on the weather in Nairobi.
+--------------------------+
| Start: Check Weather |
+--------------------------+
|
v
+--------------------------+
| Is it raining? |---- (No) ----+
+--------------------------+ |
| |
(Yes) v
| +-------------------------+
+-------------------------+ | Wear a T-shirt |
| Carry an umbrella | +-------------------------+
| Wear a jacket | |
+-------------------------+ |
| |
+-------------------------------+
|
v
+---------------------+
| Go outside |
+---------------------+
| End |
+---------------------+
Our First Language: Why Python is Your Best Friend
There are many programming languages, just like there are many spoken languages (English, Swahili, Kikuyu, etc.). We will start with Python because:
- It reads like English: Its syntax is clean and easy to understand, making it perfect for beginners.
- It's versatile: You can use it for building websites, analysing data (like farming yields!), creating games, and even for Artificial Intelligence. Companies like Safaricom and Google use it extensively!
- It has a huge community: Lots of help and resources are available online.
Image Suggestion:A vibrant, colourful digital art image of a young Kenyan student sitting under an Acacia tree with a laptop. On the screen, a simple Python code is visible. In the background, a modern Nairobi skyline with the KICC is subtly visible. The style is optimistic and futuristic.
Your First Code: "Jambo, Dunia!"
It's a tradition for every new programmer to write a program that says "Hello, World!". Let's do it the Kenyan way! The print() command in Python is used to display text on the screen.
# This line of code tells the computer to display the text inside the quotation marks.
print("Jambo, Dunia!")
When you run this code, the computer will simply output: Jambo, Dunia!. Hongera! You are now officially a programmer!
Variables: Storing Your Data in a "Kibuyu"
In programming, we need a way to store information. We use variables for this. Think of a variable as a labeled container, like a kibuyu (gourd) or a box where you can keep things.
You give the box a name (the variable name) and put something inside it (the value).
# variable_name = value
# Storing text (a "string")
student_name = "Wanjiku"
# Storing a whole number (an "integer")
age = 16
number_of_counties = 47
# Storing a number with decimals (a "float")
mpunga_price_kg = 150.75
# Storing a true or false value (a "boolean")
is_school_open = True
# We can print variables to see what's inside them
print(student_name)
print(age)
Doing "Mahesabu" (Math) with Python
Computers are fantastic at math. Let's write a simple program to calculate the total cost of some items from the local duka.
# --- Duka Shopping Calculator ---
# Step 1: Store the prices in variables
bread_price = 60 # Price in KSh
milk_price = 55 # Price in KSh
eggs_price = 120 # Price for a tray
# Step 2: Calculate the total cost using the '+' operator
total_cost = bread_price + milk_price + eggs_price
# Step 3: Display the result in a friendly way
print("The total cost for your shopping is:")
print(total_cost)
When you run this, it will first calculate 60 + 55 + 120 and then print the result: 235.
Making Decisions: The M-Pesa Balance Check
Programs need to make decisions. The if...else statement helps a program decide what to do based on a condition. It's like checking your M-Pesa balance before you pay for something.
Image Suggestion:A simple, clean flowchart diagram. The main box asks "M-Pesa Balance >= Boda Fare?". An arrow labeled "Yes" points to a green box that says "Payment Successful! Enjoy your ride!". An arrow labeled "No" points to a red box that says "Insufficient Funds. Please top up."
Here's how that logic looks in Python:
mpesa_balance = 500
boda_fare = 150
print("Checking your balance...")
# The 'if' statement checks if a condition is True
if mpesa_balance >= boda_fare:
# This code runs ONLY if the condition is True
print("Payment successful! You can take the boda.")
new_balance = mpesa_balance - boda_fare
print("Your new balance is:", new_balance)
else:
# This code runs ONLY if the condition is False
print("Ouch! Insufficient funds. You cannot pay the fare.")
print("--- Transaction check complete ---")
A Quick Look at Java
While Python is friendly, Java is a more structured and formal language. It's like the difference between speaking Sheng with your friends and writing an official letter. Java is very powerful and is used to build many large-scale applications, especially the Android apps on your phone!
Here's "Jambo, Dunia!" in Java. Notice how it needs more structure (like a "class" and a "main method") to do the same thing.
// This is how you write "Jambo, Dunia!" in Java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Jambo, Dunia!");
}
}
Don't worry about understanding it all now! It just shows why Python is a great place to start your coding journey.
Your Challenge: The Matatu Fare Calculator
Time to put your new skills to the test! Your task is to create a simple program that calculates the change a passenger should receive from a matatu conductor.
- The matatu fare from Ngong to Nairobi CBD is 100 KSh.
- A passenger gives the conductor a 500 KSh note.
- Calculate the change.
Try to write the code yourself first! Here's a template to get you started:
# --- Matatu Change Calculator ---
fare = 100
amount_paid = 500
# Your calculation goes here. Create a variable called 'change'.
# Print the final change for the passenger.
# Example: "Your change is: 400 KSh"
... finished? Great! Here is one way to solve it:
# --- Solution ---
fare = 100
amount_paid = 500
# Calculate the difference
change = amount_paid - fare
# Print the result
print("Your change is:", change, "KSh")
Conclusion: Your Journey Has Just Begun!
Kazi nzuri! You have successfully taken your first steps into the world of programming. You learned what an algorithm is, wrote your first Python code, used variables to store data, performed calculations, and even made a program that can make decisions.
This is just the beginning. From here, you can learn to create loops that repeat tasks, functions that organize your code, and eventually build amazing projects that can solve real-world problems right here in Kenya. Keep practicing, stay curious, and you'll be a tech mchawi in no time!
Endelea vivyo hivyo! (Keep it up!)
Pro Tip
Take your own short notes while going through the topics.