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
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
Price, price, and PRICE are three different variables.if, for, def, class, True, None…). Your editor will warn you.snake_casePython 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.
customer_name = "Acme Co."
quantity = 3
unit_price = 19.99
tax_rate = 0.08
subtotal = quantity * unit_price
tax = subtotal * tax_rate
total = subtotal + tax
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
round(total, 2) → 64.77) in the next lesson.
name = value.= means "store," not "is equal to."snake_case. Case matters.