1.5 Hours

3. Variables & Data Types

Module 03: Variables & Data Types

Teaching your program to remember things

Your computer has two types of memory: RAM (Random Access Memory), which is fast and temporary — it forgets everything when you turn off the computer — and storage (SSD/HDD), which is slower but permanent.

When your program runs, it lives in RAM. Every value your program needs — a number, a name, a true/false flag — is stored somewhere in RAM. Variables are how you name those memory locations so you can use them.

At the deepest level, everything in a computer is binary — 1s and 0s. The number 5 is stored as 00000101. The letter A is stored as 01000001. An image is millions of these bits organized in a specific pattern. Programming languages handle this translation for you.

Variables and Constants

Memory Model: How Variables Point to RAM Addresses Variables are named identifiers storing references to specific physical memory cells VARIABLE IDENTIFIERS age price user_name RAM ALLOCATION TABLE 0x7FFE_001 25 Integer (4 bytes) 0x7FFE_005 49.99 Float (8 bytes) 0x7FFE_00D 'Ama' String (UTF-8)

A variable is a named container for a value. You give it a name, and you can store, change, and retrieve its value throughout your program.

Python 3
# Creating variables in Python
age = 25
name = 'Kofi'
is_student = True

# Changing a variable's value
age = 26  # Happy birthday, Kofi

# Using variables
print(name + ' is now ' + str(age) + ' years old.')

A constant is a variable whose value should never change after it's set. In Python, we signal this by writing the name in ALL_CAPS — the language doesn't enforce it, but it's a strong convention:

Python 3
# Constants (by convention)
MAX_USERS = 1000
PI = 3.14159
APP_NAME = 'MyApp'

Primitive Data Types

Primitive types are the fundamental building blocks of all data in programming. Every complex data structure is ultimately made of these.

  • Integers (int): Whole numbers, positive, negative, or zero. No decimal point.
    Python 3
    score = 100
    temperature = -5
    year = 2025
  • Floats (float): Numbers with decimal points. Used for anything requiring precision.
    Python 3
    price = 29.99
    pi = 3.14159
    percentage = 85.5
    Watch Out

    Floats are stored as approximations in binary. This means 0.1 + 0.2 might give you 0.30000000000000004, not exactly 0.3. For financial calculations, use the decimal module in Python.

  • Strings (str): Text. Any sequence of characters enclosed in quotes. Strings are one of the most-used types in real programs.
    Python 3
    first_name = 'Ama'
    last_name = "Mensah"
    full_name = first_name + ' ' + last_name
    print(full_name)  # Output: Ama Mensah
  • Booleans (bool): The simplest type: either True or False. Used in every decision your program makes.
    Python 3
    is_logged_in = True
    has_premium = False
    x = 10
    print(x > 5)   # True
    print(x == 3)  # False

Type Conversion & Casting

Sometimes you need to convert one type to another. Python provides built-in functions for this:

Python 3
# Converting types
age_text = '25'           # This is a string
age_number = int(age_text)  # Now it's an integer

price = 49                # Integer
price_float = float(price)  # Now it's 49.0

count = 3
message = 'I have ' + str(count) + ' apples'
print(message)  # Output: I have 3 apples

# Common mistake:
# print('Age: ' + age_number)  # ERROR! Can't add string and int
print('Age: ' + str(age_number))  # CORRECT

Dynamic vs Static Typing

In Python and JavaScript, variables can hold any type — and you can even change a variable's type. This is called dynamic typing.

Python 3
# Python (dynamically typed)
x = 5        # x is an integer
x = 'hello'  # x is now a string — no error!
x = True     # x is now a boolean — still fine

In languages like Java, C++, or TypeScript, you must declare what type a variable will hold — and it can only ever be that type. This is static typing. It catches bugs earlier but requires more upfront code.

Java
// Java (statically typed)
int x = 5;
x = 'hello';  // COMPILE ERROR — x must be an int
Which is Better?

Neither is universally better. Dynamic typing is faster to write and great for beginners. Static typing catches more errors and scales better in large teams. Python supports optional type hints that give you a middle ground.

Knowledge Check

Ready to test your understanding of 3. Variables & Data Types?

Back