11-15-2025, 10:16 PM
Chapter 4 — Variables & Data Types
Learning how computers remember information
Up to now, Python has only printed text.
But real programs need to:
• store information
• remember things
• update values
• perform calculations
• keep track of data
For that, we use variables.
---
4.1 What Is a Variable?
A variable is like a labelled box in the computer’s memory.
You choose the label,
you put something in the box,
and Python remembers it.
Example:
• The variable name stores the text “Alice”
• The variable age stores the number 15
Whenever you use the variable later, Python looks inside the box.
---
4.2 Creating Variables (The Simple Rule)
A variable is always created like this:
NOTE:
• no spaces around the variable name
• one equals sign (=)
• Python reads it as “store this value here”
Examples:
---
4.3 Data Types — What Kind of Information?
There are 4 beginner-friendly data types:
1. String — text
2. Integer — whole number
3. Float — decimal number
4. Boolean — True or False
These are the building blocks of all beginner programs.
---
4.4 Printing Variables
You can print the value inside a variable:
Output:
You can combine text + variables:
Output:
---
4.5 Changing a Variable
Variables can be updated at any time:
Output:
Python always uses the most recent value.
---
4.6 Naming Variables (Important Rules)
Variable names:
• must start with a letter
• cannot start with a number
• cannot contain spaces
• must not use special symbols (!, ?, /, %)
• can use underscores ( _ )
Examples:
Valid:
Invalid:
---
4.7 Combining Variables
Python can use variables in calculations.
Example:
Output:
Another example:
---
4.8 Strings vs Numbers
Very important difference:
One is a number you can calculate with.
The other is just characters.
If you add a number + text, Python gets confused:
You must convert the number:
You’ll learn more conversions later.
---
4.9 Exercise — Create a Personal Profile Program
In a file called profile.py, store these variables:
• your name (string)
• your age (integer)
• your favourite food (string)
• whether you like coding yet (boolean)
• your lucky number (integer)
Then print them all nicely, e.g.:
---
4.10 Small Challenge
Create a program that stores:
• a product price
• the tax percentage
• the final price after tax
Then print the final result.
Example formula:
This is your first step towards real-world applications.
---
4.11 Chapter Summary
• A variable stores information
• Strings = text, Integers = whole numbers, Floats = decimals, Booleans = True/False
• Variables can be printed, updated, and used in calculations
• Naming rules help keep your code clean
• You now understand the memory system behind all programs
Next chapter:
Chapter 5 — Input & Output
This is where your programs become interactive.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
Learning how computers remember information
Up to now, Python has only printed text.
But real programs need to:
• store information
• remember things
• update values
• perform calculations
• keep track of data
For that, we use variables.
---
4.1 What Is a Variable?
A variable is like a labelled box in the computer’s memory.
You choose the label,
you put something in the box,
and Python remembers it.
Example:
Code:
name = "Alice"
age = 15• The variable name stores the text “Alice”
• The variable age stores the number 15
Whenever you use the variable later, Python looks inside the box.
---
4.2 Creating Variables (The Simple Rule)
A variable is always created like this:
Code:
variable_name = valueNOTE:
• no spaces around the variable name
• one equals sign (=)
• Python reads it as “store this value here”
Examples:
Code:
city = "London"
score = 92
price = 4.99---
4.3 Data Types — What Kind of Information?
There are 4 beginner-friendly data types:
1. String — text
Code:
name = "Johno"2. Integer — whole number
Code:
age = 393. Float — decimal number
Code:
temperature = 18.74. Boolean — True or False
Code:
is_online = TrueThese are the building blocks of all beginner programs.
---
4.4 Printing Variables
You can print the value inside a variable:
Code:
name = "Mia"
print(name)Output:
Code:
MiaYou can combine text + variables:
Code:
age = 16
print("Age:", age)Output:
Code:
Age: 16---
4.5 Changing a Variable
Variables can be updated at any time:
Code:
score = 10
print(score)
score = 15
print(score)Output:
Code:
10
15Python always uses the most recent value.
---
4.6 Naming Variables (Important Rules)
Variable names:
• must start with a letter
• cannot start with a number
• cannot contain spaces
• must not use special symbols (!, ?, /, %)
• can use underscores ( _ )
Examples:
Valid:
Code:
user_name
player2
temperature_readingInvalid:
Code:
2score
user-name
my variable---
4.7 Combining Variables
Python can use variables in calculations.
Example:
Code:
a = 4
b = 6
total = a + b
print(total)Output:
Code:
10Another example:
Code:
length = 12.5
width = 4
area = length * width
print(area)---
4.8 Strings vs Numbers
Very important difference:
Code:
age = 20 # number
age_text = "20" # textOne is a number you can calculate with.
The other is just characters.
If you add a number + text, Python gets confused:
Code:
age = 20
print("Age: " + age) # ERRORYou must convert the number:
Code:
print("Age: " + str(age)) # WorksYou’ll learn more conversions later.
---
4.9 Exercise — Create a Personal Profile Program
In a file called profile.py, store these variables:
• your name (string)
• your age (integer)
• your favourite food (string)
• whether you like coding yet (boolean)
• your lucky number (integer)
Then print them all nicely, e.g.:
Code:
print("Name:", name)
print("Age:", age)
print("Favourite food:", food)---
4.10 Small Challenge
Create a program that stores:
• a product price
• the tax percentage
• the final price after tax
Then print the final result.
Example formula:
Code:
final_price = price + (price * tax_rate)This is your first step towards real-world applications.
---
4.11 Chapter Summary
• A variable stores information
• Strings = text, Integers = whole numbers, Floats = decimals, Booleans = True/False
• Variables can be printed, updated, and used in calculations
• Naming rules help keep your code clean
• You now understand the memory system behind all programs
Next chapter:
Chapter 5 — Input & Output
This is where your programs become interactive.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
