Variable Assignment
& snake_case
Learn how to store data in Python variables and follow the snake_case naming convention used by professional developers.
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.
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.
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.
x = 42t = "Bob"a = 3.14student_age = 42teacher_name = "Bob"circle_area = 3.14The snake_case Convention
Python's official style guide (PEP 8) recommends snake_case for variable and function names. The rule is simple:
_ instead of spaces or other characters.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.
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
name = value with a single =.type(name) to confirm.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:
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.
print(total_points) # 0
total_points = 50
print(total_points) # 50
total_points = total_points + 25
print(total_points) # 75
Example 3: Multiple Assignment
Python lets you assign multiple variables in a single line, and even swap values elegantly:
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 = "hello" # now a str
data = [1, 2, 3] # now a list
data = True # now a bool
Naming Best Practices
retry_count not r. Name what it stores.max_score not maxScore.list, type, print, or class.num_students not total_number_of_students_enrolled_this_year.Common Mistakes
1st_place = "Gold" causes a SyntaxError. Use first_place instead.my-var = 5 is interpreted as subtraction. Python sees my - var.my var = 5 is invalid syntax. Use an underscore: my_var.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?
= is the assignment operator. It stores the right-hand value in the left-hand variable. To test equality, Python uses the double equals ==.Question 3 of 5
What is the output of this code?
x = x + 5
x = x * 2
print(x)
x = 10, then x = 10 + 5 = 15, then x = 15 * 2 = 30. Each reassignment replaces the previous value.Question 4 of 5
After running this code, what are the values of a and b?
b = "world"
a, b = b, a
a, b = b, a is Python's swap idiom. It evaluates both right-hand values first, then assigns them simultaneously. So a gets the old value of b ("world") and b gets the old value of a ("hello").Question 5 of 5
Which line will cause a SyntaxError in Python?
class is a reserved keyword in Python (used to define classes). You cannot use it as a variable name. The other options are all valid: _count starts with an underscore (allowed), total_99 contains digits (allowed after the first character), and x = x + 1 is standard reassignment (assuming x was previously defined).