Variables
Learn to store and name your data
What are variables?
A variable is like a labeled box where you can store information. You give it a name and put a value inside. Later, you can use that name to get the value back.
In real life:
- Your name is a "variable" that stores what people call you
- Your age is a "variable" that stores a number
- Your favorite color is a "variable" that stores text
In Python, we create variables to store data our programs need to remember.
Creating your first variable
Here's how to create a variable:
name = "Alice"
age = 25
is_student = TrueLet's break this down:
nameis the variable name=means "store the value on the right in the variable on the left""Alice"is the value we're storing
Naming rules
Python has rules for variable names:
Allowed:
user_name = "Dave" # lowercase with underscores (Python style)
userName = "Dave" # camelCase (works but not Python style)
age2 = 30 # numbers are OK (not at start)
_private = "secret" # underscore at start is OKNot allowed:
2age = 30 # Can't start with number
my-name = "Dave" # No hyphens (Python thinks it's subtraction)
my name = "Dave" # No spaces
class = "Python" # Can't use Python keywordsPython naming convention
In Python, we use lowercase letters with underscores between words. This is called "snake_case" and it's the standard way to name variables in Python.
# Good Python style
first_name = "Alice"
user_age = 25
is_logged_in = True
shopping_cart_total = 49.99
# Avoid camelCase (this is for other languages)
firstName = "Alice" # Works, but not Python style
userAge = 25
isLoggedIn = Truex or temp, use names like user_score or file_path.# Pick the one that matches Python style
A: firstName = "Alice"
B: first_name = "Alice"
C: FirstName = "Alice"Changing variables
Variables can change (that's why they're called variables!):
# Start with one value
score = 0
print(score) # Shows: 0
# Change it
score = 10
print(score) # Shows: 10
# Change it again
score = score + 5
score = score + 5
print(score) # Shows: 15username = "vp0fVibes"
password = ""
score = 0
password = "Hello"
score = 5
password = "HelloH@ck3rs!"
print(score)
score = 19Data Types Basics
Every variable in Python holds a value — and every value has a type. Python automatically detects the type based on what you assign.
The 3 most common types you'll use right away:
str(text) → assigned with quotes:name = "Alice"int(whole number) → assigned without quotes:age = 25bool(True/False) → assigned as:is_student = True
You can check any variable's type using the built-in type() function.
name = "Alice"
age = 25
is_student = True
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(is_student)) # <class 'bool'>Formatted Strings (f-strings)
An f-string lets you insert a variable's value directly inside a string. Just add f before the opening quote, then put the variable name inside curly braces .
name = "Alice"
age = 25
# Without f-string (old way)
print("My name is " + name + " and I am " + str(age))
# With f-string (Python way)
print(f"My name is {name} and I am {age}")city = "Delhi"
print(f"I live in {city}")Common mistakes
Forgetting quotes around text
# Wrong
name = Alice # Python looks for a variable called Alice
# Right
name = "Alice" # This creates textUsing undefined variables
# Wrong
print(score) # Error: score doesn't exist yet
score = 10
# Right
score = 10
print(score) # Now it worksConfusing = and ==
# = means "store"
age = 25
# == means "compare" (we'll learn this later)
if age == 25:
print("Quarter century!")Practice
Try these exercises to lock in what you just learned.
123print(name)4print(age)
What's next?
Before we explore data types, let's learn about comments — a crucial tool for making your code readable.