Welcome to Programming Constructs!

Hello there! Welcome to one of the most exciting parts of your H2 Computing journey. Think of Programming Constructs as the "Lego bricks" of the digital world. Just as you use different bricks to build a castle, you will use these constructs to build software, apps, and games. Don't worry if some of this feels like learning a new language—by the end of these notes, you'll be speaking "Computer" fluently!

1. The Basic Data Types (LO 1.2.1)

Before we can tell a computer what to do, we need to understand the types of information it can handle. In Python (the language used in your syllabus), we focus on four primary types:

1. Integers (int): These are whole numbers. They can be positive, negative, or zero.
Example: 10, -5, 0

2. Floating-Point Numbers (float): These are numbers with decimal points.
Example: 3.14, -0.01, 2.0

3. Strings (str): This is just a fancy word for text. Strings are always wrapped in quotes.
Example: "Hello World", 'Computing is fun!', "123" (Note: "123" is text, not a number!)

4. Booleans (bool): These represent logic. They can only be one of two values: True or False.
Example: Is the sun hot? True. Is 5 greater than 10? False.

Did you know? Computers only "see" 1s and 0s. These data types are actually abstractions that help us humans talk to the machine without going crazy!

Common Mistake to Avoid: When you use the input() function, Python always treats what you type as a string. If you want to use it as a number, you must convert it like this: \( \text{age} = \text{int(input("Enter age: "))} \).

Quick Review:
int: Whole numbers.
float: Decimals.
str: Text in quotes.
bool: True/False logic.

2. Using Library Functions (LO 1.2.2)

You don't have to reinvent the wheel! Python provides "Libraries"—pre-written code that handles common tasks. You just need to know how to call them.

Input and Output

print(): Sends information to the screen.
input(): Gets information from the user.

Mathematical Operations

To use advanced math, you often use the math module. Here are some you need to know:
math.sqrt(x): Finds the square root.
math.ceil(x): Rounds a number up to the nearest whole number.
math.floor(x): Rounds a number down to the nearest whole number.

String Operations

Strings are like a "chain" of characters. You can manipulate them easily:
Concatenation (+): Joining two strings together. "Ice" + "Cream" becomes "IceCream".
Slicing: Taking a piece of a string. \( \text{str[0:3]} \) gives you the first three characters.
len(): Tells you how many characters are in the string.

Memory Aid: Think of a library in programming just like a real library. You don't have to write the books yourself; you just go in and "borrow" the knowledge (functions) you need!

Key Takeaway: Library functions save time and make your code more reliable because they have already been tested by experts.

3. Abstraction: Functions and Procedures (LO 1.2.3)

As your programs get bigger, they become messy. Abstraction is the process of hiding complex details and grouping code into "chunks" called Functions and Procedures.

Analogy: When you press the "Start" button on a microwave, you don't need to know how the electricity creates radiation to heat your food. You just use the "Start" function. The complexity is hidden from you.

The Difference

1. Function: A block of code that performs a task and returns a value back to you.
Example: A function that calculates tax and gives you the final price.

2. Procedure: A block of code that performs a task but does not return a specific value. In Python, we define both using the def keyword.
Example: A procedure that simply prints a "Welcome" message to the screen.

Why use them?
Reusability: Write it once, use it a thousand times.
Clarity: It makes your code easier to read.
Modularization: Breaking a big problem into small, manageable "modules."

Key Takeaway: Functions are like machines: you give them "inputs" (parameters), they do work, and often "output" (return) a result.

4. Local vs. Global Variables (LO 1.2.4)

Not all variables are available everywhere. This concept is called Scope.

Local Variables: These live inside a function. They are created when the function starts and disappear when it ends.
Analogy: A private diary in your bedroom. Only people in that room (the function) can read it.

Global Variables: These live outside all functions. Any part of the program can see and use them.
Analogy: A notice board in the school canteen. Everyone can see it.

Important Tip: Professional programmers try to avoid using too many Global variables. Why? Because if any part of the program can change a variable, it’s much harder to find the "bug" when something goes wrong!

Quick Review:
Local: Safe, temporary, and private to a function.
Global: Permanent and accessible to everyone, but can cause messy bugs.

5. Python Lists and Dictionaries (LO 1.2.5)

Sometimes you need to store a collection of data rather than just one piece. This is where Lists and Dictionaries come in.

Python Lists

A list is an ordered collection of items. Think of it like a shopping list or a queue.
Lookup: \( \text{my\_list[0]} \) gets the first item (remember, we count from 0!).
Insertion: Use .append() to add an item to the end.
Update: \( \text{my\_list[1] = "New Value"} \).
Deletion: Use del my\_list[index] or .remove(value).

Python Dictionaries

A dictionary stores data in Key-Value pairs. It’s like a real dictionary: you look up a "word" (the Key) to find its "definition" (the Value).
Lookup: \( \text{my\_dict["name"]} \) gets the value associated with that key.
Insertion/Update: \( \text{my\_dict["age"] = 18} \). This adds the key if it's missing or updates it if it exists.
Deletion: Use del my\_dict["key"].

Summary Table:
List: Uses index numbers (0, 1, 2...). Good for ordered items.
Dictionary: Uses custom keys (strings/numbers). Good for structured data like a user profile.

Closing Thoughts

Don't worry if this seems like a lot to memorize. Programming is a skill, not just a set of facts. The more you practice typing these constructs into a computer, the more natural they will feel. You've now covered the core fundamentals of how programs are built—great job!

Final Key Takeaway: Programming is about choosing the right tool (data type, function, or data structure) for the job. Keep practicing!