The Necessity of Retaining State
Consider an online store: customer name, shipping address, unit price, quantity, discount percentage, and total payable amount. If each item of data could not be saved under a distinct, identifiable name, software could never evaluate logic, compute taxes, or make business decisions. Variables are the meaningful labels that let programs remember.
01 What Truly is a Variable?
In simple, precise terms:
A variable is a named reference that allows a program to store, retrieve, and manipulate a value in computer memory.
Observe this basic Python example:
Here:
nameis the variable name (identifier), and its assigned value is"Salim".ageis another variable, and its value is the number25.
In natural language, we can say: name holds "Salim", and age holds 25.
02 Why Do We Need Variables?
Suppose we needed to display the same person's name multiple times across a program:
Here, the value is manually repeated across every line. If we later need to change the user to "Rahim", we must hunt down and modify every single occurrence. By introducing a variable:
Now we have a single source of truth. Changing the value on line 1 immediately propagates across every instruction referencing that name!
Real-world data tracked by an application:
03 Creating Variables: The Assignment Operator (`=`)
In Python, creating a variable follows an elegant and straightforward syntax:
For example:
In mathematics, the = symbol asserts that two expressions have equal value. In programming, this symbol represents the Assignment Operator. It instructs the computer: “Take the evaluated value on the right-hand side and assign it to the variable name on the left-hand side.”
04 How to Use a Variable's Value
Once a value has been assigned, you can reference the variable anywhere you would normally write that value:
Salim
25
Notice that in print(name), we did not use quotation marks. Because there are no quotes, Python recognizes name as an identifier, searches memory for its current value, and outputs "Salim".
05 & 06 Value Mutation & A Single Current Value
The word Variable stems from vary (changeable). During the execution of a program, you can reassign a variable to hold a new value at any point:
At any given moment, a standard variable holds exactly one current value:
07 & 08 Is a Variable a Box? Bridging to Computer Memory
To help beginners visualize variables, educators often describe them as labeled boxes:
Connecting this back to Course 01 & Course 02: computer RAM consists of billions of indexed bytes. In Python, when you write age = 25, the Python runtime allocates memory for the integer object 25 and binds the label age as a reference (pointer) to that memory address.
09 Variable Naming Rules (Identifiers)
When naming variables in Python, the interpreter enforces strict grammatical rules:
name = "Salim"(letters)user_age = 25(underscores)score_2 = 100(numbers at the end)_private_token = "abc"(starting underscore)
2age = 25(cannot start with a number)user name = "Salim"(no spaces allowed)user-age = 25(hyphen parsed as subtraction)class = 10(cannot use reserved keywords)
10 Python is Case-Sensitive
Python treats uppercase and lowercase characters as completely separate entities:
name and Name are two completely independent variables. Inadvertently mixing cases is a frequent cause of subtle bugs for newcomers.
11 & 12 Descriptive Naming & `snake_case` Convention
Programs must be read by humans as fluently as they are executed by machines. Compare cryptic names with self-explanatory names:
The official Python community standard (PEP 8) dictates snake_case for variable names—where letters are all lowercase and words are separated by underscores (first_name, total_price, user_order_count).
13 & 14 Computing with Variables & Data Dependencies
The true utility of variables surfaces when you compose existing values into brand-new data:
Variables can also depend on textual values:
15 & 16 Updating a Variable with Its Previous Value (State Mutation)
In commercial software, one ubiquitous pattern is updating a variable based on its existing state:
Python evaluates the right side first (balance - 200, which evaluates to 1000 - 200 = 800), and then assigns that result back into balance.
17 & 18 Dynamic Typing: Why Python Needs No Type Declarations
In languages like C, C++, or Java, you must explicitly declare the data type before using a variable (e.g., int age = 25;). Python, by contrast, automatically infers the type from the value assigned:
This behavior is known as Dynamic Typing & Type Inference. The Python runtime determines the type of object at runtime without manual syntactic ceremonies.
19 Real-World Example: E-Commerce Invoice Calculator
Let us combine these principles to build a commercial shopping cart invoice:
Product: Mechanical Keyboard
Subtotal ($): 7000
Total Bill ($): 7120
20 Where Does the Programming Journey Lead Next?
Let us map out how our core conceptual building blocks connect:
Notice that not all data behaves alike. Adding numbers produces mathematical sums (10 + 20 = 30), while adding text joins characters together. And boolean conditions evaluate true or false. To govern what operations are legal on each category of information, we must understand Data Types.
21 Hands-On Practice Challenges
Open variables_practice.py in your editor and solve these hands-on challenges:
name, age, and city. Print them out, increment age by 1, and print the updated value.
age = 25
city = "Dhaka"
print(name, age, city)
age = age + 1
print("Next year age:", age)
price = 500 and quantity = 4 to compute and display the subtotal.
quantity = 4
total = price * quantity
print("Total Price:", total)
discount = 50 and calculate the final payable price.
final_price = total - discount
print("Final Price after discount:", final_price)
22 🧠 Key Takeaways & Summary
Next Lesson: Data Types
What category of data is inside each variable? Unpack Integers, Floats, Strings, Booleans, and None, and discover how Python enforces data integrity.