Understanding Python Variables
A Python variable is a named storage location that holds a value. It allows you to label information with a descriptive name, making it easy to access and modify that data throughout your program.
Think of a variable as a labeled box. You can put a value inside (like the number 42), and whenever you need that number later, you just refer to the box's label (e.g., answer).
Using variables makes your code flexible. Instead of hard-coding values, you use names that make sense to humans, making your code readable and easier to maintain.
How to Create a Variable
In Python, you create a variable through assignment using the equals sign (=). Unlike some other languages, you don't need a special command to declare it first. It happens automatically the moment you assign a value.
user_name = "Ada Lovelace"(String)user_age = 36(Integer)is_programmer = True(Boolean)
New to Python? This lesson is part of our Free Python course covering variables, data types, functions, and more.
Start Learning for Free →Practical Application: Creating Variables
The best way to understand variables is to create them yourself. Let's write some code.
Exercise 1: Assign and Print
Create a variable named greeting with the text "Hello, World!". Then, create a variable named year with the number 2025. Print both of them.
# Define the variables
greeting = "Hello, World!"
year = 2025
# Print them
print(greeting)
print(year)
Your output will appear here...
Exercise 2: Reassigning Variables
Variables are dynamic, their values can change. This is why they are called "variables" (able to vary).
In the box below, create a variable called current_status and set it to "Pending". Print it. Then, on a new line, change the value to "Complete" and print it again to see the update.
current_status = "Pending"
print(f"Status is: {current_status}")
# Reassigning the variable
current_status = "Complete"
print(f"Status is now: {current_status}")
Your output will appear here...
"Complete" to the variable, the old value ("Pending") is forgotten. The variable name current_status now points to the new data.
You've got the basics down! Ready to see how professionals name and organize their variables?
Python ExerciseNaming Rules and Best Practices
Python has strict rules and helpful conventions for naming variables. Following these ensures your code runs without errors and is easy for others to read.
- The Rules (Mandatory): Names must start with a letter or underscore (
_). They cannot start with a number. They are case-sensitive (ageandAgeare different). - The Conventions (Best Practice): Use snake_case. This means using all lowercase letters and separating words with underscores.
- Be Descriptive: Use
user_emailinstead ofue. Clear names act as documentation for your code.
Advanced Concept: Dynamic Typing
Python is dynamically typed. This means you do not need to tell Python what type of data (integer, string, etc.) a variable will hold. Python figures it out automatically.
You can even check what type a variable currently holds using the type() function.
# This is the same code as the prompt.
# Run it to see how the "type" changes!
data = 100
print(type(data)) #
data = "One Hundred"
print(type(data)) #
data = True
print(type(data)) #
Your output will appear here...
int (Integer) to str (String) to bool (Boolean). While this is flexible, be careful! Changing a variable's type unexpectedly can cause bugs in larger programs.
Common Mistakes to Avoid
Even experienced developers make these small mistakes:
- Unquoted Strings: Writing
name = Johncauses an error because Python thinksJohnis another variable. It must bename = "John". - Case Sensitivity: Calling
print(Score)when you definedscore(lowercase) will fail. - Using Keywords: You cannot name a variable
if,for, orclass, as these are reserved by Python.
Knowledge Check: Apply Your Skills
Let's simulate a simple shopping cart calculation. In the editor below, define variables for:
- An
item_name(e.g., "Laptop") - An
item_price(e.g., 500) - A
tax_rate(e.g., 0.10)
Then, create a new variable called total_cost that calculates the price plus tax (Price * Tax Rate + Price). Finally, print a sentence summarizing the order.
Here is how a professional developer would write this:
# --- Data Variables ---
item_name = "Laptop"
item_price = 1000
tax_rate = 0.05 # 5% tax
# --- Logic / Calculation ---
# We use variables to do the math
tax_amount = item_price * tax_rate
total_cost = item_price + tax_amount
# --- Output ---
print(f"Order: {item_name}")
print(f"Subtotal: {item_price}")
print(f"Tax: {tax_amount}")
print(f"Total Due: {total_cost}")
Your output will appear here...
Congratulations! You've Completed This Lesson
You now understand how to store, name, and manipulate data using Python variables. This is the foundation for all future coding.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, real-world projects and get a Certificate.
Enroll Now