Imagine you are moving into a new house. To keep things organized, you place your belongings into cardboard boxes and write a label on the outside of each box—like “Kitchen Supplies” or “Winter Clothes.”
In programming, your computer has a massive warehouse called Memory (RAM). When you write a program, you constantly need to store, retrieve, and update pieces of data. Variables are simply the labeled boxes in your computer’s memory. You put data inside them, write a descriptive name on the outside, and whenever you need that data again, you just ask Python for the label.
Without variables, our programs would not be able to remember anything!
Table of Contents
Syntax & Basic Usage
Creating a variable in Python is incredibly straightforward. You write the name you want to give your variable, followed by an equals sign =, and then the data you want to store.
This equals sign is known as the Assignment Operator. It does not mean “equal to” in a mathematical sense; instead, it means “take the value on the right, and assign it to the label on the left.”
# Create variables and assign them values
player_character_name = "Arthur"
player_health_points = 100
# Retrieve the data by using the variable names
print("Hero:", player_character_name)
print("Health:", player_health_points)
# Expected Output:
# Hero: Arthur
# Health: 100
Code language: PHP (php)
Deep Dive: Dynamic Typing & Memory Allocation
Dynamic Typing
If you have ever used older programming languages like C++ or Java, you know that you have to explicitly tell the computer exactly what kind of box you are making before you can use it (e.g., an “integer box” or a “text box”). This is called static typing.
Python, however, uses Dynamic Typing. Python is smart enough to instantly look at the data you are trying to store and automatically create the correct type of box in memory.
Furthermore, because it is dynamic, you can completely change the contents of the box later on, and Python will adjust the box automatically.
# Initially, we store an integer (a whole number)
current_user_status = 1
print("Status is:", current_user_status)
# Later in the code, we change it to a String (text)
current_user_status = "Active User"
print("Status changed to:", current_user_status)
# Expected Output:
# Status is: 1
# Status changed to: Active User
Code language: PHP (php)
How Memory Works (The id() Function)
Under the hood, when you create a variable, Python finds an empty slot in your computer’s RAM and drops the data there. It then attaches your variable name to that exact location like a sticky note. You can actually see the unique “warehouse address” of your data using Python’s built-in id() function.
# Create a variable
secret_passcode = 80085
# Find out exactly where this data lives in the computer's memory
print("The data is stored at memory address:", id(secret_passcode))
# Expected Output:
# The data is stored at memory address: 140733857342600
# (Note: Your output number will be different, as your RAM is unique!)
Code language: PHP (php)
Real-World Practical Examples
Variables are the foundation of all software logic. Here is how they are used in real-world scenarios to hold and calculate data.
Scenario 1: E-Commerce Shopping Cart
When a user adds items to a digital shopping cart, variables calculate the final price behind the scenes.
# Define the cost of a single item and the quantity ordered
laptop_unit_price = 1200.50
laptops_ordered_quantity = 3
# Calculate the total by multiplying the variables
total_order_cost = laptop_unit_price * laptops_ordered_quantity
# Output the final receipt
print("Checkout Total: $", total_order_cost)
# Expected Output:
# Checkout Total: $ 3601.5
Code language: PHP (php)
Scenario 2: User Profile Formatting
Applications often take separate pieces of user data from a database and combine them into a single, readable format.
# Data retrieved from a database
customer_first_name = "Elena"
customer_last_name = "Rodriguez"
# Combine (concatenate) the variables to create a full name
# Notice the " " adds a space between the first and last name!
customer_full_name = customer_first_name + " " + customer_last_name
print("Currently logged in as:", customer_full_name)
# Expected Output:
# Currently logged in as: Elena Rodriguez
Code language: PHP (php)
Best Practices & Common Pitfalls (Naming Conventions)
When naming your variables, you cannot just type whatever you want. Python has strict rules, and the Python developer community has established strong Best Practices (known as PEP 8) to keep code readable.
The Golden Rules of Naming:
- Use
snake_case: Python programmers write variable names in all lowercase letters, separating words with underscores (e.g.,account_balance, notAccountBalance). - Be Descriptive: Never use single letters like
x,y, ora. A variable namedctells you nothing. A variable namedcustomer_counttells you exactly what it holds. - No Numbers at the Start: A variable name can contain numbers, but it cannot begin with a number. (
player_1is valid;1st_playerwill crash your code). - No Spaces: Spaces are strictly forbidden in variable names. Use underscores instead.
- Avoid Reserved Keywords: You cannot name a variable after a word Python already uses for its own internal logic (like
print,if,for, orTrue).
# ❌ BAD EXAMPLES (Do not do this)
x = 25 # What does x mean?
user name = "John" # Spaces will cause a SyntaxError
1st_place = "Sarah" # Cannot start with a number
print = "Hello" # Overwrites Python's built-in print command!
# ✅ GOOD EXAMPLES (Do this)
user_age = 25
user_full_name = "John"
first_place_winner = "Sarah"
welcome_message = "Hello"
Code language: PHP (php)
Summary
- Variables act like labeled boxes that store data inside your computer’s memory.
- We use the assignment operator (
=) to assign data to a variable. - Python uses Dynamic Typing, meaning it automatically detects the data type and allows you to change it at any time.
- You can view the actual memory address of your data using the
id()function. - Always follow snake_case naming conventions and give your variables highly descriptive, readable names.
