Part 2: Core Programming Concepts
Part 2: Core Programming Concepts — read through each chapter in order. Diagrams, tables and worked examples included.
1. Variables & Data Types
Variables: labelled boxes for your data
A variable is a named place in the computer's memory where your program stores a value it needs to remember. The clearest way to picture it is a labelled box: the label is the variable's name, and whatever you put inside is its value. When you want the value later, you refer to the box by its label rather than its physical location.
Declaration and assignment
Two things happen when you create a variable. Declaration brings the name into existence; assignment places a value in it using the assignment operator =. Read = as "gets" or "becomes", never as mathematical equality.
age = 30 # create 'age', put 30 in it
price = 19.99 # a decimal number
name = "Ada" # a piece of text
is_active = True # a yes/no value
age = age + 1 # read 30, add 1, store 31 back
The last line shows why = is not equality: age = age + 1 is nonsense as maths, but as an instruction it means "take the current value, add one, and store the result back in the same box." After it runs, age holds 31.
Data types
Every value has a data type — a category that tells the computer what the value is and what you may do with it. You can add two integers, but "adding" two booleans makes no sense. Types also decide how much memory a value needs and how it is stored. The core types nearly every language shares are:
| Type | Stores | Example values | Typical use |
|---|---|---|---|
| Integer | Whole numbers | -4, 0, 42, 1000 | Counts, ages, quantities, indexes |
| Float | Numbers with a fractional part | 3.14, -0.5, 19.99 | Prices, measurements, averages |
| String | Text (a sequence of characters) | "hello", "Ada", "Z" | Names, messages, labels |
| Boolean | One of two truth values | True, False | Flags, conditions, on/off state |
| Null / None | "No value at all" | None, null, nil | Missing or not-yet-set data |
Static vs dynamic typing
Languages differ in when they check types. In statically typed languages (Java, C#) you state a variable's type up front and it cannot change. In dynamically typed languages (Python, JavaScript) a variable simply takes the type of whatever you assign, and that can change as the program runs. Both styles track types; they just enforce them at different moments.
Naming variables well
Choose names that say what the value means: total_price beats tp, and user_count beats x. Good names turn code into something a human can read like a sentence. Most languages require names to start with a letter, allow letters, digits and underscores, and are case-sensitive — so Age and age are two different boxes.
Key idea: a variable is a named, re-usable container; its type describes the kind of value it holds and the operations that value supports.