Variables & Naming Conventions
Variables store data in Python. Unlike other languages, you don't need to declare types — Python figures it out automatically. Learn proper naming conventions and best practices for clean, readable code.
Variables in Python
A variable is a named container for data. In Python, you create a variable by simply assigning a value with =. Python uses dynamic typing — it automatically determines the type. Variable names are case-sensitive (name and Name are different).
Everything in Python is an object — use type() to check
Naming Rules
- Must start with a letter or underscore: name, _count
- Can contain letters, numbers, underscores: score_1, user2
- Cannot start with a number: 1name is INVALID
- Cannot use reserved words: if, for, class, def are INVALID
- Use snake_case: my_name, total_score (Python convention)
- Be descriptive: user_age is better than x or a
- Constants use UPPER_CASE: MAX_RETRIES = 3, PI = 3.14159
Variables Example
# Creating variables (no type declaration needed!)
name = "Alice"
age = 25
height = 5.6
is_student = True
# Printing variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Student:", is_student)
# Reassigning variables
age = 26
print("Next year:", age)
# Multiple assignment
x, y, z = 10, 20, 30
print(x, y, z) # 10 20 30
# Same value to multiple variables
a = b = c = 0
print(a, b, c) # 0 0 0Try It Yourself: Variables
Tip
Tip
Use f-strings for output: f'{name} is {age}' is cleaner than string concatenation. Also use meaningful variable names — user_age is much better than x or a.
Common Mistake
Warning
Variable names cannot start with a number. 1name = 'Alice' is a SyntaxError. Also avoid using Python built-in names like list, str, type, print as variable names — it shadows them.
Practice Task
Note
Create a profile script: (1) Store your name, age, city, hobby in variables. (2) Print them using f-strings. (3) Reassign age to next year's value and print again. (4) Use multiple assignment: a, b, c = 1, 2, 3.
Quick Quiz
Key Takeaways
- Variables store data in Python.
- Must start with a letter or underscore: name, _count
- Can contain letters, numbers, underscores: score_1, user2
- Cannot start with a number: 1name is INVALID