15 min read Fundamental Concept Lesson 3 of 16
Course 03 · Part 1 — Thinking & Basics

Variables & Data

The Art of Storing, Referencing, and Mutating State in Memory

In the previous lesson, we wrote our very first Python script and used print() to display values on the screen. But real-world software does far more than print static text. Applications must hold a user's name, track their account balance, calculate order discounts, and modify information dynamically as circumstances change. To retain and manage data over time, we rely on programming's most fundamental instrument: Variables.

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:

name = "Salim"
age = 25

Here:

  • name is the variable name (identifier), and its assigned value is "Salim".
  • age is another variable, and its value is the number 25.

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:

print("Salim")
print("Salim")
print("Salim")

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:

name = "Salim"
print(name)
print(name)
print(name)

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:

user_name
user_email
product_price
discount
shipping_cost
total_bill
order_status
is_paid

03 Creating Variables: The Assignment Operator (`=`)

In Python, creating a variable follows an elegant and straightforward syntax:

variable_name = value

For example:

name = "Salim"
age = 25
price = 99.50
Critical distinction: Code equality is not mathematical equality!

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:

variables_demo.py
name = "Salim"
age = 25
print(name)
print(age)
Output:
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:

age = 25
print(age) # Outputs: 25
age = 26 # Reassigned to new value
print(age) # Outputs: 26

At any given moment, a standard variable holds exactly one current value:

score = 50
score = 80
print(score) # Output is 80; the previous 50 has been overwritten

07 & 08 Is a Variable a Box? Bridging to Computer Memory

To help beginners visualize variables, educators often describe them as labeled boxes:

Box Model (Intuitive Metaphor)
🏷️ age
25
Reference Model (Python Reality)
age
──refers to──>
Object: 25

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.

Variable Identifier → Python Runtime → Memory Address (RAM)

09 Variable Naming Rules (Identifiers)

When naming variables in Python, the interpreter enforces strict grammatical rules:

Valid Variable Names:
  • name = "Salim" (letters)
  • user_age = 25 (underscores)
  • score_2 = 100 (numbers at the end)
  • _private_token = "abc" (starting underscore)
Invalid Syntax (Will Crash):
  • 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 = "Salim"
Name = "Rahim"
print(name) # Outputs: Salim
print(Name) # Outputs: Rahim

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:

Cryptic Code (Poor Naming):
x = 1000
y = 20
z = x - (x * y / 100)
Two months later, even the original author will struggle to understand x, y, and z.
Clean Engineering (Descriptive Naming):
price = 1000
discount = 20
final_price = price - (price * discount / 100)
Anyone reading the code immediately grasps the underlying business logic.

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:

price = 100
quantity = 3
# Multiply the values of two variables to produce a third
total = price * quantity
print(total) # Outputs: 300

Variables can also depend on textual values:

first_name = "Salim"
greeting = "Hello " + first_name
print(greeting) # Outputs: Hello Salim

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:

balance = 1000
# Customer withdraws 200
balance = balance - 200
print(balance) # Outputs: 800

Python evaluates the right side first (balance - 200, which evaluates to 1000 - 200 = 800), and then assigns that result back into balance.

# Accumulating points in a game:
score = 0
score = score + 10
score = score + 20
score = score + 30
print(score) # Final accumulated score: 60

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:

age = 25 # Inferred as Integer (whole number)
name = "Salim" # Inferred as String (text)
price = 99.5 # Inferred as Float (decimal number)

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:

invoice.py
# Product catalog information
product = "Mechanical Keyboard"
price = 3500
quantity = 2
shipping_charge = 120
# Commercial arithmetic
subtotal = price * quantity
total_bill = subtotal + shipping_charge
# User-facing invoice output
print("Product:", product)
print("Subtotal ($):", subtotal)
print("Total Bill ($):", total_bill)
Output:
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:

Instruction → Program → Variable → Data → Data Types

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:

Challenge 1: Profile State & Birthday Mutation Create variables for name, age, and city. Print them out, increment age by 1, and print the updated value.
name = "Salim"
age = 25
city = "Dhaka"
print(name, age, city)
age = age + 1
print("Next year age:", age)
Challenge 2: Cart Subtotal Calculation Assign price = 500 and quantity = 4 to compute and display the subtotal.
price = 500
quantity = 4
total = price * quantity
print("Total Price:", total)
Challenge 3: Discount Adjustment Introduce a variable discount = 50 and calculate the final payable price.
discount = 50
final_price = total - discount
print("Final Price after discount:", final_price)

22 🧠 Key Takeaways & Summary

Variable: A named identifier used to reference and manipulate values stored in computer memory.
Assignment (=): Copies the evaluated right-hand expression into the left-hand variable identifier.
Reassignment: Variables can mutate over time, always maintaining exactly one current value.
Naming Rules: Cannot start with numbers, cannot contain spaces or hyphens, and are strictly case-sensitive.
snake_case: The official PEP 8 style convention for multi-word variables in Python.

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.