Thread Closed 
Thread Rating:
CHAPTER 4 — VARIABLES & DATA TYPES
#1
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:

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 = value

NOTE:
• 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 = 39

3. Float — decimal number 
 
Code:
temperature = 18.7

4. Boolean — True or False 
 
Code:
is_online = True

These 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:
Mia

You 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
15

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:
Code:
user_name
player2
temperature_reading

Invalid:
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:
10

Another 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"  # text

One 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)    # ERROR

You must convert the number:

Code:
print("Age: " + str(age))    # Works

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.:

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
« Next Oldest | Next Newest »
Thread Closed 


Forum Jump:


Users browsing this thread: 1 Guest(s)