![]() |
|
CHAPTER 4 — VARIABLES & DATA TYPES - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 4 — VARIABLES & DATA TYPES (/showthread.php?tid=222) |
CHAPTER 4 — VARIABLES & DATA TYPES - Leejohnston - 11-15-2025 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"• 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"--- 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"Output: Code: MiaYou can combine text + variables: Code: age = 16Output: Code: Age: 16--- 4.5 Changing a Variable Variables can be updated at any time: Code: score = 10Output: Code: 10Python 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_nameInvalid: Code: 2score--- 4.7 Combining Variables Python can use variables in calculations. Example: Code: a = 4Output: Code: 10Another example: Code: length = 12.5--- 4.8 Strings vs Numbers Very important difference: Code: age = 20 # numberOne is a number you can calculate with. The other is just characters. If you add a number + text, Python gets confused: Code: age = 20You 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)--- 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 |