Python BasicsBeginner7 min06 / 63

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

Visual walkthrough1 / 5
1

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.

Variables are how programs remember things while they run.

#What Is a Variable?

Think of it like

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.

Three variables storing different kinds of values
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 (score and Score are two different variables)
  • Cannot be a Python keyword like if, for, or True
Valid variable names vs. one that breaks the rules
# Valid names
user_name = "Jordan"
score1 = 100
_hidden = "secret"

# This would be a SyntaxError — starts with a digit
# 1st_place = "gold"
Tip

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.

The variable score is updated step by step
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.

The same variable holds an int, then a str, then a float
thing = 42
print(thing, type(thing))

thing = "hello"
print(thing, type(thing))

thing = 3.14
print(thing, type(thing))
Watch out

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.

Two ways to assign multiple variables in one line
x, y = 10, 20
print(x)
print(y)

# Assign the same value to multiple variables at once
a = b = 0
print(a, b)
Common mistake

= 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."

UPPER_CASE names are a signal that the value is a constant
MAX_RETRIES = 3
PI = 3.14159
APP_NAME = "PyQuest"

print(APP_NAME, "allows up to", MAX_RETRIES, "retries")
Quick check

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.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
score = 0
score = 50
score = score + 10
print(score)
Fix the bug#2

This code has a bug. What is wrong?

fix-bug
my_name = "Jordan"
my_age = 20
print("Hi, I'm" + my_name + "and I'm" + my_age + "years old")
Fill in the blank#3

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)
Reorder the lines#4

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.

1
price = 50
2
print(final_price)
3
TAX_RATE = 0.1
4
final_price = price + price * TAX_RATE
Your turn
Practice exercise

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:

solution.py · editable