๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

Variables

๐Ÿ“š What are Variables? A variable is a named container that holds a value in your program. Think of it like a labelled box: you write a name on the outside, put something inside, and come back later to find it by name. In Python you create one with a simple assignment: name = value.

8 min 10 XP Lesson 2 of 21
Variables
๐ŸŒ

Appy Saysโ€ฆ

Every game tracks your score, your health, your username. Every app remembers your name, your settings, your playlist. All of that lives in variables. Master this and you understand how every app stores data.

๐Ÿ“–

What is a Variable?

A variable is a named storage box in your computer's memory. You give it a name, put a value inside, and use that name anywhere in your code to get the value back โ€” or change it.

  • โ€ขYou create one with name = value โ€” the = sign means 'store this'
  • โ€ขYou can store text, numbers, True/False, listsโ€ฆ anything
  • โ€ขThe value can change later โ€” that's why it's called a variable
  • โ€ขVariable names should describe what they hold: score not x123
๐ŸŽฎ

Think of it like a Minecraft chest

Each chest has a label on it. You open the chest labelled diamonds to see how many you have. You can take some out (decrease), add more (increase), or swap the contents entirely. Variables work the same way โ€” label, store, retrieve, change.

๐Ÿ“Š

Quick Cheat Sheet

โš™๏ธ

How Variables Work

  • โ€ขAssign: score = 0 โ€” creates the box and puts 0 inside
  • โ€ขRead: print(score) โ€” opens the box and shows what's inside
  • โ€ขUpdate: score = score + 10 โ€” take the old value, add 10, put the new value back
  • โ€ขRename: just use a new assignment โ€” score = 100 replaces whatever was there
๐ŸŒ

Real-World Examples

  • โ€ขA game stores lives = 3 โ€” every time you die, lives = lives - 1
  • โ€ขSpotify stores your volume = 75 โ€” moving the slider changes the variable
  • โ€ขTikTok stores likes_count = 1423 โ€” every heart press does likes_count += 1
  • โ€ขA shopping app stores cart_total = 0 and adds to it with each item
๐Ÿ’ก

Key Facts

  • โ€ขVariable names cannot start with a number: 1score is invalid, score1 is fine
  • โ€ขUse underscores for multi-word names: high_score (snake_case is the Python style)
  • โ€ขPython figures out the type automatically โ€” no need to say 'this is a number'
  • โ€ข+= is a shortcut: score += 10 means score = score + 10
๐Ÿ“Œ

Remember

The = sign in Python does not mean 'equals' like in maths. It means assign โ€” 'store this value in this variable'. To check if two things are equal, use == (double equals).

โœ…

What You Learned

  • โ€ขA variable is a named box that stores a value in memory
  • โ€ขCreate with name = value, read with print(name)
  • โ€ขUpdate the value by assigning again: score = score + 10
  • โ€ขGood names make code readable โ€” use player_health, not ph
  • โ€ข= means assign, == means check if equal

Key Facts

  • โ†’Variable names cannot start with a number: 1score is invalid, score1 is fine
  • โ†’Use underscores for multi-word names: high_score (snake_case is the Python style)
  • โ†’Python figures out the type automatically โ€” no need to say 'this is a number'
  • โ†’+= is a shortcut: score += 10 means score = score + 10

Real-World Examples

โ€ข A game stores <code>lives = 3</code> โ€” every time you die, <code>lives = lives - 1</code> โ€ข Spotify stores your <code>volume = 75</code> โ€” moving the slider changes the variable โ€ข TikTok stores <code>likes_count = 1423</code> โ€” every heart press does <code>likes_count += 1</code> โ€ข A shopping app stores <code>cart_total = 0</code> and adds to it with each item

Remember

The = sign in Python does not mean 'equals' like in maths. It means assign โ€” 'store this value in this variable'. To check if two things are equal, use == (double equals).

Quick Quiz

1 / 2

What is a variable?