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.

# Your code here! # 1. Create 'greeting' variable # 2. Create 'year' variable # 3. Print them both
# 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.

# Your code here! # 1. Set current_status to "Pending" # 2. Print it # 3. Update current_status to "Complete" # 4. Print it again
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...
Analysis: When you assign "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 Exercise

Naming 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 (age and Age are different).
  • The Conventions (Best Practice): Use snake_case. This means using all lowercase letters and separating words with underscores.
  • Be Descriptive: Use user_email instead of ue. 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.

data = 100 print(type(data)) data = "One Hundred" print(type(data)) data = True print(type(data))
# 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...
Analysis: Notice how the type changed from 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 = John causes an error because Python thinks John is another variable. It must be name = "John".
  • Case Sensitivity: Calling print(Score) when you defined score (lowercase) will fail.
  • Using Keywords: You cannot name a variable if, for, or class, 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.

# 1. Define item_name, item_price, and tax_rate # 2. Calculate total_cost # 3. Print the result # Example: "Total for Laptop is: 550.0"

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
📝

Next Lesson

Move on to Data Types.

Next Lesson →
Scroll to Top