Variables & Data Types
What Are Variables?
Think of a variable like a labeled jar on a shelf. The label is the variable's name, and whatever you put inside is its value. You can swap out the contents anytime — put in a number today, a word tomorrow.
In Python, you create a variable just by picking a name and using the = sign. No special keywords needed — Python figures out the type for you.
There are a few rules for naming your jars:
- Names can use letters, numbers, and underscores — but can't start with a number.
- Names are case-sensitive:
Scoreandscoreare different jars. - You can't use Python's reserved words like
if,for, orclass. - By convention, use
snake_casefor variable names:player_name, notplayerName.
Creating Variables
The Four Core Data Types
Every value in Python has a type — it tells Python what kind of thing it is and what you can do with it.
int— Whole numbers like42,-7,0. Use these for counting things.float— Decimal numbers like3.14,-0.5. Use these for measurements, money, percentages.str— Text wrapped in quotes:"hello"or'hello'. Even"123"is a string if it's in quotes!bool— Only two values:TrueorFalse. Perfect for yes/no decisions.
There's also a special value called None — it means "nothing here yet." It's like an empty jar with a label on it.
Checking Types with type()
Type Conversion (Casting)
b = a, you're sticking a second label on the same value — not copying it into a new box. This matters a lot later when you work with lists and objects!Multiple Assignment & Swapping
Python has a neat trick: you can assign several variables in one line. You can also swap two variables without needing a temporary one — something most other languages can't do this cleanly.