01 / 20
x = 5

Variable Assignment
& snake_case

Learn how to store data in Python variables and follow the snake_case naming convention used by professional developers.

Python

What is a Variable?

A variable is a named container that holds a value in your program's memory. Think of it as a labeled box: the label is the variable name, and the contents are the value stored inside.

score 95 name points to value stored data

The Assignment Operator

In Python, the equals sign = is the assignment operator. It takes the value on the right and stores it in the variable on the left.

# Syntax: name = value
age = 17
name = "Alice"
gpa = 3.85
is_enrolled = True

Why Naming Matters

Variable names are for humans, not computers. A good name tells you exactly what the variable stores, making code easier to read, debug, and maintain.

✕ Unclear
x = 42
t = "Bob"
a = 3.14
✓ Descriptive
student_age = 42
teacher_name = "Bob"
circle_area = 3.14

The snake_case Convention

Python's official style guide (PEP 8) recommends snake_case for variable and function names. The rule is simple:

1
All lowercase letters. Never use capital letters in variable names.
2
Separate words with underscores _ instead of spaces or other characters.
3
Start with a letter or underscore. Never begin with a digit.
student_count = 30 # ✓ snake_case
max_retry_limit = 5 # ✓ snake_case
is_valid = True # ✓ snake_case

Python Naming Rules

Beyond convention, Python enforces strict rules about what counts as a valid variable name. Break these and you get a SyntaxError.

✓ Valid my_var _private count2 total_score MAX_SIZE x ✕ Invalid 2nd_place my-var my var class for $price starts with digit has hyphen has space reserved keyword reserved keyword special character

snake_case vs Other Styles

Different programming languages prefer different naming styles. Python uses snake_case for most names, but other styles exist:

Style Example Used In
snake_case student_name Python, Ruby
camelCase studentName JavaScript, Java
PascalCase StudentName Python classes, C#
SCREAMING_SNAKE MAX_SIZE Python constants

How to Assign a Variable

1
Choose a descriptive name that tells you what the data represents.
2
Apply snake_case: all lowercase, words separated by underscores.
3
Write the assignment: name = value with a single =.
4
Verify the type if needed: use type(name) to confirm.
5
Use the variable in expressions, function calls, or print statements.
# Following all 5 steps:
average_grade = 88.5
print(type(average_grade)) # <class 'float'>
print("Grade:", average_grade) # Grade: 88.5

Example 1: Simple Assignment

Assign basic values to well-named variables and print them:

# Store student information
first_name = "Maya"
last_name = "Johnson"
grade_level = 10
honor_roll = True

print(first_name, last_name)
# Output: Maya Johnson

print("Grade:", grade_level)
# Output: Grade: 10

Example 2: Reassignment

Variables can be reassigned: a new value replaces the old one. The old value is discarded.

total_points = 0
print(total_points) # 0

total_points = 50
print(total_points) # 50

total_points = total_points + 25
print(total_points) # 75
Step 1 0 total_points Step 2 50 total_points Step 3 75 total_points

Example 3: Multiple Assignment

Python lets you assign multiple variables in a single line, and even swap values elegantly:

# Assign multiple variables at once
width, height, depth = 10, 20, 5

# Same value to multiple variables
red = green = blue = 0

# Swap two variables (no temp needed!)
player_one = "Alice"
player_two = "Bob"
player_one, player_two = player_two, player_one
print(player_one) # Bob

Dynamic Typing in Python

Python is dynamically typed: a variable can hold any type, and you can reassign it to a completely different type at any time.

data = 42 # int
data = "hello" # now a str
data = [1, 2, 3] # now a list
data = True # now a bool
✓ Advantage
Flexible and quick to prototype. No type declarations needed.
★ Caution
Changing types mid-program can cause unexpected bugs. Name variables clearly to signal their type.

Naming Best Practices

Be Specific
Use retry_count not r. Name what it stores.
Use snake_case
Separate words with underscores: max_score not maxScore.
Avoid Reserved Words
Don't name variables list, type, print, or class.
Keep It Short
Descriptive but concise: num_students not total_number_of_students_enrolled_this_year.

Common Mistakes

✕ Starting with a Digit
1st_place = "Gold" causes a SyntaxError. Use first_place instead.
✕ Using Hyphens
my-var = 5 is interpreted as subtraction. Python sees my - var.
✕ Spaces in Names
my var = 5 is invalid syntax. Use an underscore: my_var.
✕ Shadowing Built-ins
list = [1, 2] overwrites Python's built-in list() function. Choose a unique name.

Quick Reference Table

Concept Syntax / Rule Example
Assignment name = value age = 17
snake_case lowercase + underscores first_name
Reassignment Same name, new value x = x + 1
Multiple assign Comma-separated a, b = 1, 2
Swap Tuple unpacking a, b = b, a
Constants SCREAMING_SNAKE MAX_SPEED = 100

Question 1 of 5

Which of the following is a valid Python variable name in snake_case?

Question 2 of 5

What does the = operator do in Python?

Question 3 of 5

What is the output of this code?

x = 10
x = x + 5
x = x * 2
print(x)

Question 4 of 5

After running this code, what are the values of a and b?

a = "hello"
b = "world"
a, b = b, a

Question 5 of 5

Which line will cause a SyntaxError in Python?