Stage 2 Building Blocks
Lesson 2 of 12 +10 XP

Building Blocks: Variables & Data Types

Every program needs to remember things. A calculator needs to remember the numbers you type. A game needs to remember your score. A weather app needs to remember the temperature. Python uses variables to remember stuff. And that's exactly what we're learning today.

Sarpanch
Imagine your brain is RAM. Variables are sticky notes you put everywhere so your brain doesn't have to remember everything. Python's brain works the same way. 🧠

What Is a Variable?

A variableA variable is a named container that stores a value. You give it a name, Python remembers the value. is basically a sticky note for your program. You write something on it, Python remembers it. Later, you can change what's written on it. Simple.

Creating a variable in Python looks like this:

variables.py
# Creating variables
name = "Sarpanch"
age = 5
is_snake = True
favourite_food = "mice (don't judge)"

# Using variables
print(name)
print(age)
print(is_snake)
2name = "Sarpanch"Creates a variable called name and stores the string "Sarpanch" in it.
3age = 5Creates a variable called age and stores the number 5. No quotes — it's a number, not text.
4is_snake = TrueTrue is a boolean (yes/no value). It means: yes, Sarpanch is indeed a snake.
8–10print(name)...print() can display a variable — no quotes needed around the variable name!
📏 Variable Naming Rules:
  • Start with a letter or underscore (not a number)
  • Only letters, numbers, and underscores allowed
  • Case-sensitive: Name and name are different variables
  • Use snake_case by convention: my_name, not myName
  • Don't use Python keywords like if, for, print

The 4 Basic Data Types

Every value in Python has a type. Python needs to know what kind of data it's dealing with so it knows what to do with it.

🔢

int — Integer

Whole numbers. No decimals.

age = 25
score = -10
floors = 100
🔣

float — Decimal Number

Numbers with a decimal point.

price = 9.99
pi = 3.14159
temp = -0.5
📝

str — String (Text)

Text in single or double quotes.

name = "Sarpanch"
city = 'Mumbai'
msg = "I code, therefore I am"
✅

bool — Boolean (True/False)

Only two values: True or False.

is_awake = True
is_bored = False
has_bugs = True  # always
check_types.py
print(type(42))        # <class 'int'>
print(type(3.14))      # <class 'float'>
print(type("hello"))  # <class 'str'>
print(type(True))      # <class 'bool'>

Python as a Calculator

Python can do math. All the operators you'd expect, plus a few extras:

x = 10
y = 3

print(x + y)   # 13  — Addition
print(x - y)   # 7   — Subtraction
print(x * y)   # 30  — Multiplication
print(x / y)   # 3.333... — Division (always float)
print(x // y)  # 3   — Floor division (no remainder)
print(x % y)   # 1   — Modulo (remainder only)
print(x ** y)  # 1000 — Power (10³)
⚠️ "5" + 5 will crash! — You can't add a string and a number directly. Use int("5") + 5 to convert the string first. Type mismatches = errors.

A Python variable is like a labelled jar on a kitchen shelf. The label is the variable name. Whatever's inside the jar is the value. You can look inside, change what's inside, or use the contents to cook something (do calculations).

Different jars hold different things: a spice jar holds spices (strings), a number jar holds quantity (int), a yes/no jar is either full or empty (bool). Python's data types are just different kinds of jars.

Variables: Labelled Boxes

name "Sarpanch" str = age 5 int = price 9.99 float = is_snake True bool name = "Sarpanch" age = 5 price = 9.99 is_snake = True
Each variable is a labelled box that stores a value of a specific type.

The Variable Incident

Panel 1
Sarpanch
"I'll remember everyone's birthday in my head! I don't need variables!"
Panel 2
Sarpanch
"It's Meena's birthday? No wait — Meena or Reena? The 12th or the 21st?"
Panel 3
Sarpanch
meena_bday = "March 12"
Ah yes. Variables. The solution to everything.

Variables in Action

Edit the code below and run it. Try changing the values and watch what happens!

💡 Challenges:
  1. Create a variable with your name and print it.
  2. Create two number variables and print their sum.
  3. Try print(type(your_variable)) to see what type it is.

Building Blocks — Done! 🧱

  • A variable stores a value with a name. name = "Sarpanch"
  • Python has 4 basic data types: int, float, str, bool
  • Use type() to find out what type a value is.
  • Python does math with + - * / // % ** operators.
  • You can't mix types carelessly — "5" + 5 is an error. Convert first!
  • Variable names: lowercase, snake_case, no spaces, no keywords.
Sarpanch victorious
Variables mastered! Stage 3 is next — If/Else. You'll teach Python to make decisions. Like: "Should I eat? If hungry: yes. Else: also yes." 😎