![]() |
|
CHAPTER 8 — Variables & Naming Rules - 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 8 — Variables & Naming Rules (/showthread.php?tid=226) |
CHAPTER 8 — Variables & Naming Rules - Leejohnston - 11-15-2025 Chapter 8 — Variables & Naming Rules How programmers store information and write clean, readable code A variable is a “box” in memory where Python stores a value. You choose the name, and Python remembers the value. Example: Code: name = "Mia"Python now remembers: • name → "Mia" • age → 14 • height → 1.62 --- 8.1 Why Variables Matter Variables allow you to: • store user input • reuse values • change values later • perform calculations • make programs dynamic Without variables, everything would be hard-coded and static. --- 8.2 Creating Variables Just use: Code: variable_name = valueExamples: Code: score = 0--- 8.3 Naming Rules (Essential!) Python variable names: ✔ must start with a letter or underscore ✔ may contain letters, numbers, and underscores ✔ are case-sensitive (age ≠ Age ≠ AGE) ✔ should be descriptive ❌ Not allowed: Code: 2name✔ Allowed: Code: name2--- 8.4 The Professional Naming Style Python developers use: snake_case → all lowercase, words separated by underscores. Examples: Code: max_speedAvoid one-letter names except in maths loops. --- 8.5 Dynamic Typing In Python, a variable can change type: Code: value = 10 # intThis is flexible but can also cause confusion, so beginners should keep variables consistent. --- 8.6 Overwriting Variables A variable stores *the latest* value assigned: Code: score = 10Output: Code: 20--- 8.7 Input + Variables Everything from input() arrives as a string: Code: age = input("Enter age: ")Convert if needed: Code: age = int(input("Enter age: "))--- 8.8 Combining Variables Examples: Code: first = "Lee"Output: Code: Lee Johnston--- 8.9 Reassigning using the Old Value This is extremely common: Code: score = 10Short version: Code: score += 5Other shortcuts: Code: score -= 2--- 8.10 Mini Project — Personal Info Card Ask the user: • name • age • favourite colour • hobby Then print a formatted “profile card.” Example: Code: Name: Mia--- 8.11 Challenge — The Food Bill Ask the user: • food price • drink price • dessert price Then: • calculate total • apply 10% tax • output final bill to 2 decimals --- 8.12 Chapter Summary • variables store values • snake_case is the professional naming standard • variables must start with letters/underscores • input() values must be converted for maths • shortcut operators save time • variables update to the newest assigned value Next: Chapter 9 — Strings & Text Manipulation Where beginners learn how to edit text, join text, format text, and build real programs with output that looks professional. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |