A variable is like a labeled box where you can put any value and use it as many times as you need!
🎯 What is a variable?
Picture this: you have a box with “age” written on the side. Put the number 16 inside. Now whenever you need someone’s age, just open that box!
# Create a variable
age = 16
# Use it
print(age) # 16
print(age + 5) # 21
print(age * 2) # 32
✏️ How to create a variable
Very simple: name = value
name = 'Alex'
age = 20
height = 1.75
is_student = True
Important: The = sign does NOT mean “equals”! It means “assign this value.”
📝 Naming rules
✅ Allowed
name = 'Ivan'
user_age = 25
firstName = 'Maria'
count_2 = 10
_private = 'secret'
❌ Not allowed
2name = 'error' # Cannot start with a digit
user-age = 25 # Cannot use a hyphen
first name = 'test' # Cannot have a space
if = 5 # Cannot use a keyword
🔄 Changing a variable’s value
You can change a variable at any time!
score = 0
print(score) # 0
score = 10
print(score) # 10
score = score + 5
print(score) # 15
# Short form
score += 5 # Same as: score = score + 5
print(score) # 20
🎨 Variable types
Python figures out what you want to store automatically!
# String (text)
name = 'Anna'
city = "London"
message = '''Long text
spanning
multiple lines'''
# Integer
age = 18
year = 2026
temperature = -5
# Float (decimal)
height = 1.65
price = 99.99
pi = 3.14159
# Boolean
is_adult = True
has_license = False
🔍 Checking a variable’s type
name = 'Oleg'
age = 25
height = 1.80
is_student = True
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>
💡 Working with variables
Copying
x = 10
y = x # y gets a COPY of x's value
y = 20 # Changing y does NOT affect x!
print(x) # 10
print(y) # 20
Swapping values
# The Pythonic way!
a = 5
b = 10
a, b = b, a # Swap values
print(a) # 10
print(b) # 5
Multiple assignment
# Assign the same value to multiple variables
x = y = z = 0
print(x, y, z) # 0 0 0
# Assign different values at once
name, age, city = 'Ivan', 20, 'Moscow'
print(name) # Ivan
print(age) # 20
print(city) # Moscow
🎯 Common mistakes
Mistake 1: Using a variable before creating it
# ❌ Error!
print(name) # NameError: name 'name' is not defined
name = 'Pete'
# ✅ Correct
name = 'Pete'
print(name) # Create it first, then use it
Mistake 2: Quotes vs no quotes
name = 'Vasya'
print('name') # Prints: name (this is a string literal)
print(name) # Prints: Vasya (this is the variable's value)
# With quotes — it's text
# Without quotes — it's a variable
Mistake 3: Case matters!
age = 16
Age = 20
AGE = 25
print(age) # 16
print(Age) # 20
print(AGE) # 25
# These are THREE DIFFERENT variables!
Mistake 4: Non-ASCII variable names
# ⚠️ Technically works, but strongly discouraged
имя = 'Vasya'
print(имя) # Vasya
# ✅ Better to use ASCII names
name = 'Vasya'
print(name) # Vasya
💪 Best practices
1. Use meaningful names
# ❌ Bad
a = 16
x = 'London'
tmp = 1.75
# ✅ Good
age = 16
city = 'London'
height = 1.75
2. snake_case for variables
# ✅ Python convention
user_name = 'Ivan'
first_name = 'Maria'
total_score = 100
3. UPPER_CASE for constants
# Values that never change
MAX_SPEED = 120
PI = 3.14159
API_KEY = 'secret123'
🚀 Practical examples
Example 1: User survey
print('=== Survey ===')
name = input('Name: ')
age = int(input('Age: '))
city = input('City: ')
hobby = input('Hobby: ')
print(f'\n📝 Your details:')
print(f'Name: {name}')
print(f'Age: {age}')
print(f'City: {city}')
print(f'Hobby: {hobby}')
Example 2: Age calculator
current_year = 2026
birth_year = int(input('What year were you born? '))
age = current_year - birth_year
print(f'You are {age} years old')
print(f'In 10 years you will be {age + 10}')
print(f'5 years ago you were {age - 5}')
Example 3: Currency converter
# Exchange rates
usd_to_eur = 0.92
gbp_to_eur = 1.17
dollars = float(input('How many dollars do you have? '))
pounds = float(input('How many pounds do you have? '))
euros_from_usd = dollars * usd_to_eur
euros_from_gbp = pounds * gbp_to_eur
total_euros = euros_from_usd + euros_from_gbp
print(f'\nTotal in euros: €{total_euros:.2f}')
📋 Cheat sheet
# Create
variable = value
# Update
variable = new_value
variable += 5 # Add 5
variable -= 3 # Subtract 3
variable *= 2 # Multiply by 2
variable /= 4 # Divide by 4
# Check type
type(variable)
# Type conversions
str(123) # '123'
int('456') # 456
float('1.5') # 1.5
bool(1) # True
🎓 Summary
- A variable = a named container for a value
- Created with
name = value - Can be changed at any time
- Names should be in English, using snake_case
- Python infers the data type automatically
- Use descriptive names!
Variables are the foundation of programming. They make your code flexible and readable! 💪
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!