
Python
Variables are named containers that store data values in your program. Think of them as labeled boxes where you can put information and retrieve it later using the name you gave it. Variables are fundamental because nearly every program uses them to store and manipulate information.
A variable is like a labeled box that holds data. You give the box a name (the variable name) so you can refer to it later, and you put something inside it (the value). The variable stores the value so you can use it throughout your program, and you can change what's inside whenever you need to.
# Creating variables
name = "Alice"
age = 25
height = 5.6
# Using variables
print(name) # Output: Alice
print(age) # Output: 25
print(height) # Output: 5.6Python has specific rules for naming variables. Your variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and cannot contain spaces or special characters. Following these rules ensures your code works and is readable to other programmers.
name = "Alice" # Letters
age = 25 # Starts with letter
user_name = "bob" # Underscore is OK
count2 = 10 # Numbers after first character
_private = "secret" # Can start with underscore
USER_ID = "12345" # All caps for constants2age = 25 # ❌ Can't start with number
user-name = "bob" # ❌ Can't use hyphen (use underscore)
user name = "alice" # ❌ Can't have spaces
for = 5 # ❌ Can't use Python keywords (if, for, while, etc.)
class = "Python 101" # ❌ Can't use Python keywordsUse descriptive names that explain what the variable stores. Good variable names make your code easy to understand, even when you read it months later. Avoid single letters (except for loop counters) and vague names that don't describe the data.
# ❌ Bad - unclear what these store
x = 25
y = "Alice"
a = True
# ✅ Good - clear and descriptive
user_age = 25
user_name = "Alice"
is_student = TrueYou create a variable by using the assignment operator (=) to give it a value. You can update a variable's value later by assigning it a new value. The old value is replaced with the new one, but the variable name stays the same.
# Create variables
name = "Alice"
score = 85
# Use them
print(f"Hello, {name}!") # Output: Hello, Alice!
print(f"Score: {score}") # Output: Score: 85
# Update variables
name = "Bob" # name now contains "Bob"
score = score + 10 # Add 10 to the current score (95)
print(f"Name: {name}, Score: {score}") # Output: Name: Bob, Score: 95The = sign means "assign the value on the right to the variable on the left." It's not the same as equals in math — it's a direction of assignment. The computer evaluates the right side first, then stores the result in the variable on the left.
# Simple assignment
x = 10 # x gets the value 10
# Using existing variables
y = x + 5 # y gets 10 + 5 = 15
# Self-reference (using variable to update itself)
x = x + 1 # Get current x (10), add 1, store back as 11
print(x) # Output: 11
print(y) # Output: 15# Create variables for a person's information
full_name = "Sarah Johnson"
birth_year = 1998
email = "sarah@email.com"
is_member = True
# Display the profile
print(f"Name: {full_name}")
print(f"Age: {2024 - birth_year}")
print(f"Email: {email}")# Store student information
student_id = "STU001"
course_name = "Python 101"
credits = 3
is_enrolled = True
print(f"Student {student_id} is enrolled in {course_name}")| Concept | Remember |
|---|---|
| Variable | Named container for storing data |
| Assignment | Use = to give a variable a value |
| Naming Rules | Start with letter/underscore, only letters/numbers/underscores |
| Keywords | Can't use Python reserved words like if, for, while |
| Descriptive Names | Choose names that clearly explain what the variable stores |
| Update | Assign a new value to change what a variable contains |
Now that you understand how to create variables, let's learn about the different types of data you can store in them. We'll start with numbers!
Next: Numbers — Integers and Floats →
Ready to practice? Try challenges or take the quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward