Variables & Numbers

The Course
•
January 5, 2026
•
6 min read

A variable is a name pointing at a value. You create one by assigning to it — no keyword, no type declaration, no ceremony.

Names and values Python
bill = 40
tip_rate = 0.18

tip = bill * tip_rate
print(tip)
print(bill + tip)

Read = as “gets”, not as “equals”: bill = 40 means the name bill now refers to 40. That is why count = count + 1 makes sense, which as a mathematical claim would be nonsense.

Naming things

Python names use lower_snake_case. They may contain letters, digits and underscores, and may not start with a digit.

total_price = 19.99      # good
totalPrice = 19.99       # works, but not the Python style
total price = 19.99      # SyntaxError - no spaces
2nd_price = 19.99        # SyntaxError - cannot start with a digit

Names are worth thinking about for a moment. n, data and temp tell a reader nothing; unread_count, sales_rows and celsius tell them everything. You will read your own code far more often than you write it.

The number types

Python has two everyday number types: int for whole numbers and float for decimals. type() tells you which you have.

int and float Python
print(type(7))
print(type(7.0))
print(type(7 + 0.5))

# Dividing always produces a float, even when it divides evenly.
print(10 / 2, type(10 / 2))

Notice the last line: 10 / 2 is 5.0, not 5. Plain / is true division and always returns a float.

Arithmetic operators

OperatorMeaningExampleResult
+ - *add, subtract, multiply3 * 412
/true division7 / 23.5
//floor division7 // 23
%remainder (modulo)7 % 21
**power2 ** 101024

// and % are the pair you reach for when you need whole units and a leftover — minutes and seconds, pages and rows, coins and change.

Whole units and leftovers Python
total_seconds = 227

minutes = total_seconds // 60
seconds = total_seconds % 60

print(minutes, "minutes and", seconds, "seconds")

# % is also the standard "is this divisible?" test:
print(10 % 2, 11 % 2)

Operator precedence follows normal maths: ** first, then * / // %, then + -. When in doubt, use parentheses — they cost nothing and save the reader a moment of doubt.

Updating a variable

x = x + 1 works, but Python offers a shorthand for every operator:

Augmented assignment Python
score = 10
score += 5      # same as score = score + 5
score -= 3
score *= 2
print(score)

message = "Hi"
message += " there"   # works on strings too
print(message)
0.1 + 0.2 is not 0.3

Floats are stored in binary, and most decimal fractions have no exact binary form — exactly as 1/3 has no exact decimal form. So tiny errors accumulate. This is not a Python quirk; it is how nearly every language on nearly every processor works.

See it for yourself Python
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)

# For money, count in the smallest unit - cents - using ints.
price_cents = 1999
quantity = 3
print("Total: $", (price_cents * quantity) / 100)

# Or round when displaying.
print(round(0.1 + 0.2, 2))

The rule of thumb: use int for anything counted, float for anything measured, and never compare two floats with ==.

Before you write your own

Predict. What does each print show? Decide all three lines first.

What will this print? Python
coffees = 3
price = 4
coffees += 2
print(coffees * price)
print(17 // 5, 17 % 5)
print(8 / 4)
Check your prediction
20
3 2
2.0

coffees += 2 makes it 5, so the first line is 5 * 4. 17 // 5 is how many whole fives fit (3), and 17 % 5 is what’s left over (2). And / always gives a float, even when it divides evenly — so 2.0, not 2.

Fix. This should say how many whole weeks and leftover days are in 45 days — 6 weeks and 3 days — but it doesn’t. Run it, see what it does print, and fix the two operators.

Weeks and days Python
days = 45
weeks = days / 7
leftover = days // 7
print(weeks, "weeks and", leftover, "days")
Check your fix
days = 45
weeks = days // 7
leftover = days % 7
print(weeks, "weeks and", leftover, "days")
6 weeks and 3 days

Whole units come from // and the leftover from % — the same pair as the minutes-and-seconds example above. / gave 6.428571428571429 because it doesn’t stop at whole weeks.

Exercise

Split the bill

Three friends share a $137.50 restaurant bill and want to leave an 18% tip. Print the total including tip, and what each person owes, rounded to two decimal places. Your output should be:

Total with tip: 162.25
Each person pays: 54.08
bill = 137.50
people = 3
tip_rate = 0.18

# Your code here
Hint 1: the idea

The total with tip is the bill plus 18% of the bill. Each person’s share is that total divided by the number of people. round(value, 2) gives two decimal places.

Hint 2: a first step

Work out the total first, in its own variable, and print it with round(total, 2). Check it says 162.25 before you start on the per-person share — then you know which half any later mistake is in.

Show one solution
bill = 137.50
people = 3
tip_rate = 0.18

total = bill * (1 + tip_rate)
each = total / people

print("Total with tip:", round(total, 2))
print("Each person pays:", round(each, 2))

bill * (1 + tip_rate) avoids computing the tip separately — a small habit that keeps arithmetic in one readable expression.

What you learned

  • name = value binds a name to a value; no declaration needed.
  • int counts, float measures, and type() tells you which you have.
  • / always gives a float; // and % give whole units and leftovers.
  • += and friends update a variable in place.
  • Float arithmetic is approximate — round for display, use ints for money.
Last updated on January 5, 2026

Was this article helpful?

Your response is saved on this device.