Variables & Assignment
Learn how variables let you store, name, and reuse values in your programs — like labeled boxes that hold information.
Every useful program needs to remember things. The price of an item, someone's name, a score in a game — all of that has to live somewhere while your code is running.
In Python, we use variables to store values. Once you give a value a name, you can use that name anywhere in your program instead of typing the value over and over.
See it in action
— step through the idea, then dive into the details below.Variables: Named Boxes for Data
A variable lets you give a value a name so your program can remember it. Instead of repeating 25 everywhere, just write age — and Python knows what you mean.
#What Is a Variable?
Think of a labeled box
Imagine a cardboard box with a sticky note on it. The sticky note is the variable name, and whatever is inside the box is the value. You can look inside the box anytime, and you can swap out what's inside whenever you want.
Creating a variable is called assignment. You do it with the = sign. The name goes on the left, the value goes on the right.
age = 25
name = "Alex"
is_student = True
print(name)
print(age)#Naming Your Variables
Python has a few rules for variable names:
- Can contain letters, digits, and underscores (
_) - Cannot start with a digit
- Are case-sensitive (
scoreandScoreare two different variables) - Cannot be a Python keyword like
if,for, orTrue
# Valid names
user_name = "Jordan"
score1 = 100
_hidden = "secret"
# This would be a SyntaxError — starts with a digit
# 1st_place = "gold"Use snake_case by convention
Python programmers write multi-word names with underscores between words: first_name, high_score, total_items. This style is called snake_case and it makes names easy to read at a glance.
#Reassigning a Variable
You can change what a variable holds at any time. Just assign it a new value. The old value is simply forgotten.
score = 0
print(score)
score = 50
print(score)
score = score + 10
print(score)#Dynamic Typing
In Python, a variable is not locked to one type of value. You can store a number in it now and a piece of text later. Python figures out the type from whatever value you assign — this is called dynamic typing.
thing = 42
print(thing, type(thing))
thing = "hello"
print(thing, type(thing))
thing = 3.14
print(thing, type(thing))Switching types can cause surprises
While Python allows it, reassigning a variable to a completely different type is usually a sign of confusing code. Pick a clear name and stick to one kind of value. Future-you will be grateful.
#Multiple Assignment
Python has a handy shortcut: you can assign values to several variables on a single line. This is great for setting related values together.
x, y = 10, 20
print(x)
print(y)
# Assign the same value to multiple variables at once
a = b = 0
print(a, b)= is not the same as ==
A single = assigns a value to a variable. Two equals signs == checks whether two values are equal. Mixing them up is one of the most common beginner mistakes.
``python age = 18 # stores 18 in age age == 18 # asks: is age equal to 18? (True/False) ``
#Constants: Values That Should Not Change
Sometimes a value is meant to stay fixed throughout your program — like the speed of light or a tax rate. Python does not enforce constants, but the convention is to write their names in UPPER_CASE. This signals to anyone reading your code: "don't change this."
MAX_RETRIES = 3
PI = 3.14159
APP_NAME = "PyQuest"
print(APP_NAME, "allows up to", MAX_RETRIES, "retries")Which of the following is a valid Python variable name?
Key takeaways
- A variable is a named container that stores a value; create one with `name = value`.
- Names must start with a letter or underscore, are case-sensitive, and use `snake_case` by convention.
- You can reassign a variable at any time — Python will hold whatever value you give it last.
- Python is dynamically typed, so a variable can hold any type of value.
- Use `UPPER_CASE` names for values that are meant to stay constant throughout your program.
What does this code print?
score = 0
score = 50
score = score + 10
print(score)This code has a bug. What is wrong?
my_name = "Jordan"
my_age = 20
print("Hi, I'm" + my_name + "and I'm" + my_age + "years old")Complete the code so it assigns 'Alex' to name and 25 to age on a single line using multiple assignment.
, = "Alex", 25 print(name) print(age)
Put these lines in the right order so the program stores a tax rate as a constant, creates a price variable, and prints the final price after tax.
price = 50
print(final_price)
TAX_RATE = 0.1
final_price = price + price * TAX_RATE
Create three variables: your name (a string), your age (a number), and whether you like pizza (True or False). Then print a sentence using all three, like: "Hi, I'm Alex, I'm 25 years old, and it's True that I like pizza."
Try it live — edit the code and hit Run to execute real Python: