Programming Fundamentals: Variables, Data Types, and Decisions
In Week 7, we explored the spectrum of programming languages and learned how compilers and interpreters translate human code into machine binary.
Now, it is time to write code! When you play a video game, how does the program remember that your score is 450? How does an online banking app remember your name, or determine whether your balance is high enough to withdraw cash?
It does this using Variables, Data Types, and Conditional Decisions. In this lesson, we will learn the three universal concepts shared by virtually every programming language on Earth, using the friendly syntax of Python.
Learning Outcomes
By the end of this lesson, you will be able to:
- Explain the concept of a Variable as a named box in memory.
- Identify four fundamental Data Types: Integer (
int), Float (float), String (str), and Boolean (bool). - Write and trace conditional decision logic using
if,elif, andelse. - Predict the exact output of basic Python code scripts.
Key Vocabulary
| Term | Technical Meaning |
|---|---|
| Variable | A named storage location in RAM holding data that can change during program execution. |
Assignment Operator (=) |
Stores a value on the right into the variable named on the left (e.g., score = 100). |
| Data Type | A classification specifying what type of value a variable holds and what operations can be performed on it. |
Integer (int) |
Whole numbers without decimal fractions (e.g., 5, 42, -10). |
Float (float) |
Numbers containing decimal fractions (e.g., 3.14, 19.99, -0.5). |
String (str) |
Text characters enclosed in quotation marks (e.g., "Amina", 'Lagos'). |
Boolean (bool) |
A logic value that can only be either True or False. |
Study Notes: Variables and Decisions in Python
MEMORY STORAGE:
Variable Name: [ student_name ] [ score ] [ is_passed ]
Value Inside: | "Chidi Okon" | | 85 | | True |
Data Type: String (str) Integer (int) Boolean (bool)
1. Variables: Named Storage Boxes
Think of a variable as a cardboard box with a label pasted on the front:
- The label is the variable name (e.g., player_score).
- The content inside the box is the value (e.g., 50).
- If you defeat an enemy in the game, the program changes the value inside the box to 60!
In Python, creating a variable is as simple as writing:
2. The Four Essential Data Types
| Type | Name in Python | Example | What It Stores |
|---|---|---|---|
| Integer | int |
10, -3, 500 |
Whole counting numbers |
| Float | float |
12.50, 3.14159 |
Numbers with decimal points |
| String | str |
"Hello", 'Abuja' |
Text letters, words, and sentences |
| Boolean | bool |
True, False |
Logical truth values |
3. Making Decisions: The if-elif-else Structure
Computers make choices by evaluating conditions. In Python:
score = 75
if score >= 70:
print("Grade: Distinction")
elif score >= 50:
print("Grade: Credit")
else:
print("Grade: Needs Improvement")
How Python reads this code:
1. Is 75 >= 70? Yes (True)!
2. Python executes the line indented beneath it: printing "Grade: Distinction".
3. It skips the rest of the elif and else blocks and continues!
Notice the Indentation: In Python, four spaces of indentation show which lines of code belong inside the if decision branch.
Worked Example: Tracing a Ticket Pricing Script
Trace what this Python code will print for a 12-year-old child:
visitor_age = 12
if visitor_age < 5:
ticket_price = 0
elif visitor_age <= 17:
ticket_price = 500
else:
ticket_price = 1500
print("Your ticket fee is Naira:", ticket_price)
Tracing Step-by-Step:
1. visitor_age is assigned 12.
2. First condition: Is 12 < 5? False! Jump to next condition.
3. Second condition: Is 12 <= 17? True!
4. Execute branch: ticket_price is set to 500.
5. Skip else branch.
6. Print statement executes: "Your ticket fee is Naira: 500".
Practical Class Activity: Code Detective
Read the Python script below and predict its output in your notebook without running it on a computer:
account_balance = 5000
transfer_amount = 2000
if transfer_amount <= account_balance:
account_balance = account_balance - transfer_amount
print("Transfer successful!")
print("Remaining balance:", account_balance)
else:
print("Insufficient funds!")
Questions to answer:
- Does the
ifcondition evaluate to True or False? (Answer: True, because 2000 <= 5000) - What will be displayed on the first line of output? (Answer: "Transfer successful!")
- What number will be displayed as the remaining balance? (Answer: 3000)
Common Mistakes & Corrections
| Common Mistake | Why It Is Incorrect | Proper Correction |
|---|---|---|
| "Forgetting quotation marks around a string: name = Fatima." | Without quotes, Python thinks 'Fatima' is another variable name and throws a NameError. | Always enclose text in quotes: name = "Fatima". |
| "Using a single equals '=' for comparison instead of '=='." | A single = assigns a value; double == compares whether two values are equal. |
Use if score == 100: for comparisons. |
| "Forgetting the colon ':' at the end of an if line." | In Python syntax, colons are required after if, elif, and else. |
Always end decision lines with a colon: if age >= 18:. |
Notebook Summary
Copy Into Your Computing Notebook
- A Variable is a named location in RAM memory holding data that can change during execution.
- Four core data types: Integer (
int), Float (float), String (str), and Boolean (bool). - The assignment operator
=stores values; comparison==checks equality. - Use
if,elif, andelsewith indented code blocks to create intelligent decision-making logic.