HomeCourseModule 03 › Variables — Python's version of a named cell

Variables — Python's version of a named cell

Module 03 · Variables and Data Types8 min readBeginner

What you'll learn

  • Create variables and reassign them
  • Follow Python's variable-naming rules
  • Understand the difference between a variable and a value

What's a variable?

A variable is a name that points to a value. In Excel, you might put 100 in cell B2 and refer to it as =B2 from then on. In Python you write:

price = 100

From now on, anywhere you write price, Python substitutes in 100.

price = 100
tax = price * 0.2
print(price + tax)   # 120

The single equals sign means "store this"

This trips people up at first: = in Python is not the same as = in algebra. It doesn't mean "equals." It means "take what's on the right and store it under the name on the left."

x = 5      # store 5 in x
x = x + 1  # take whatever x is (5), add 1, store back in x
print(x)   # 6

The naming rules

Style: snake_case

Python convention is lowercase with underscores: monthly_total, customer_name, tax_rate. Not monthlyTotal (that's Java) or MonthlyTotal (that's a class). Stick to snake_case and your code will look professional.

Walkthrough: build a tiny invoice

Set up the inputs

customer_name = "Acme Co."
quantity = 3
unit_price = 19.99
tax_rate = 0.08

Compute

subtotal = quantity * unit_price
tax = subtotal * tax_rate
total = subtotal + tax

Print the receipt

print("Customer:", customer_name)
print("Subtotal:", subtotal)
print("Tax:     ", tax)
print("Total:   ", total)

Output:

Customer: Acme Co.
Subtotal: 59.97
Tax:      4.7976
Total:    64.7676
📝 Don't panic about the long decimal
We'll fix the rounding (round(total, 2) → 64.77) in the next lesson.

Key takeaways

  • A variable is a name attached to a value: name = value.
  • = means "store," not "is equal to."
  • Use snake_case. Case matters.
  • Reassigning a variable just overwrites whatever was there.

Build your own pocket calculator

  1. Create variables for: a meal price, a tip percentage, the number of people splitting.
  2. Compute total with tip.
  3. Compute per-person share.
  4. Print everything with labels.
📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.