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.
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:
# Creating variables name = "Sarpanch" age = 5 is_snake = True favourite_food = "mice (don't judge)" # Using variables print(name) print(age) print(is_snake)
- Start with a letter or underscore (not a number)
- Only letters, numbers, and underscores allowed
- Case-sensitive:
Nameandnameare different variables - Use
snake_caseby convention:my_name, notmyName - 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
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³)
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
The Variable Incident
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!
- Create a variable with your name and print it.
- Create two number variables and print their sum.
- 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.