
A Python variables are a name that stores a value in your computer’s memory. You can think of it as a container that holds data, which you can retrieve or modify later in your program.
Python variables make programs dynamic and flexible. Instead of hardcoding values, you can store data in variables and use them throughout your code, making it easier to update, reuse, and maintain.
Since Python is dynamically typed, don’t need to declare python variable types it’s determined automatically at runtime.
Understanding Variable Assignment
Python uses the equal sign (=) as the assignment operator to assign a value to a variable.
Example: Basic Assignment
x = 10 # x is an integer
name = "Welcome to mlopsbootcamp" # String value assigned to name
price = 19.99 # Float value assigned to price
Here:
- name → holds a string
"Welcome to mlopsbootcamp" x→ holds an integer10price→ holds a float19.99
print(x)
print(name)
print(price)
Output:
10
Welcome to mlopsbootcamp
19.99
Python creates an integer object 10 in memory. The variable x points refers to that object. X doesn’t “contain” the value directly it holds a reference.
You can check a variable’s memory reference using the id() function:
print(id(x))
Multiple python Variable Assignments
Python allows assigning values to multiple variables simultaneously
a, b, c = 1, 2, 3
print(a, b, c)
1 2 3
Assigning the Same Value to Multiple python Variables
python allows to assign one value to several variables at once
x=y=z=100
print(x,y,z)
Output:
100 100 100
Swapping Values Between two python Variables
Python allows to swapping variables easy
x, y = 10, 20
x, y = y, x
print(x, y)
Output:
20 10
Reassigning and Updating Python Variables
Python allows to variables can be reassigned with new data
count = 10
count = count + 5
print(count)
Output:
15
When we reassign a variable, Python creates a new object in memory and binds the variable name to it. If the old object isn’t used elsewhere, it’s automatically garbage collected.
Python Variable Naming Rules and Best Practices
Python has specific syntactic rules that define how variable names must be written. If you break any of these rules, you’ll get a SyntaxError.
Valid and Invalid Python Variable Names
Rule 1: Variable Names Must Start with a Letter or an Underscore (_)
# Valid example
name = "Alice"
_name = "Private variable"
Age = 25
# invalid example
1name = "Bob" # Cannot start with a number
@age = 30 # Cannot start with special character
Rule 2: Variable Names Must Start with a Letter or an Underscore (_)
After the first character, you can use, Letters (a to z or A to Z), Numbers (0 to 9) and Underscore (_)
# valid Example
student1 = "Ravi"
user_name = "John"
item_count_2 = 15
# invalid Example
user-name = "Raj" # Hyphen not allowed
item@price = 50 # Special character not allowed
Rule 3: Variable Names Cannot Start with a Number, we can include numbers after the first letter, but not at the beginning.
# Valid Example
var1 = 10
# Invalid Example
1var = 10 # ❌ SyntaxError
Rule 4: Variable Names Are Case-Sensitive, python treats uppercase and lowercase letters as different.
age = 25
Age = 30
AGE = 35
print(age, Age, AGE)
Rule 5: No Special Characters Allowed Except Underscore (_), variable names cannot includes spaces, hyphens (–) and symbols like @, #, $, %, !, etc.
# Valid Example
total_amount = 100
# Invalid examples
total amount = 100 # space not allowed
total-amount = 100 # hyphen not allowed
total@amount = 100 # special char not allowed
Rule 6: Variables Cannot Be Python Keywords, python has reserved words (keywords) that have special meaning to the interpreter — you can’t use them as variable names.
Example of Python keywords:
and, or, not, if, else, elif, for, while, class, try, except,
True, False, None, return, global, lambda, def, import
# Valid Example
class_name = "Computer Science"
# Invalid Example
class = "Computer Science" # ❌ SyntaxError
Rule 7: Variable Names Should Be Descriptive, While Python allows almost any valid name, it’s best practice to use meaningful and descriptive names so that your code is easy to understand.
# Good Example
marks = 10
bonus_points = 20
#Bad Example
a = 10
b = 20
Common Mistakes When Using Python Variables
| ❌ Mistake | 🧩 Problem | ✅ Correct |
|---|---|---|
2price = 50 | Starts with a number | price2 = 50 |
user-name = "Raj" | Hyphen not allowed | user_name = "Raj" |
list = [1,2,3] | Overwrites built-in | user_list = [1,2,3] |
for = 10 | Keyword used | loop_counter = 10 |
Total Price = 200 | Contains space | total_price = 200 |
Examples of Python Variables in Action
| Purpose | Good Variable Name | Example |
|---|---|---|
| Store age | age, user_age | user_age = 30 |
| Store name | student_name | student_name = "Arun" |
| Store marks | total_marks | total_marks = 480 |
| Store boolean | is_active, has_error | is_active = True |
| Store list | user_list, product_ids | product_ids = [101, 102] |
Formatting Strings with Python Variables Using f-Strings
An f-string (formatted string literal) in Python is a clean, fast, and readable way to include variables or expressions inside strings using curly braces {}. Introduced in Python 3.6, it automatically converts values to strings and allows embedding of variables, calculations, functions, and formatted data like numbers or dates—all within a single string. f-strings are evaluated at runtime, making them both efficient and elegant compared to older methods like concatenation or .format().
Example:
name = "Alice"
age = 25
print(f"My name is {name} and I will be {age + 1} next year.")
Output:
My name is Alice and I will be 26 next year.
Another example:
name = "Ravi"
course = "Python"
duration = "3 months"
print(f"""
Student Name: {name}
Course: {course}
Duration: {duration}
""")
Output:
Student Name: Ravi
Course: Python
Duration: 3 months
Python Variable Best Practices for Beginners
🔹 Use = to assign a value to a variable.
🔹 Names must start with a letter or underscore (_), never with a number.
🔹 Python decides type automatically — it’s dynamically typed.
🔹 Variables are case-sensitive (Age ≠ age).
🔹 Avoid Python keywords (if, class, for, etc.) as variable names.
🔹 Use descriptive names — total_price is better than tp.
🔹 You can reassign a variable to a new value or type anytime.
🔹 f-strings (f"...") are the best way to display variables in strings.
🔹 Don’t overwrite built-in names like list, sum, input.
🔹 Python handles memory management automatically (no need to free memory).

Leave a Comment