A variable is a name pointing at a value. You create one by assigning to it — no keyword, no type declaration, no ceremony.
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.
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
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * | add, subtract, multiply | 3 * 4 | 12 |
/ | true division | 7 / 2 | 3.5 |
// | floor division | 7 // 2 | 3 |
% | remainder (modulo) | 7 % 2 | 1 |
** | power | 2 ** 10 | 1024 |
// and % are the pair you reach for when you need whole units and a
leftover — minutes and seconds, pages and rows, coins and change.
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:
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.
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 ==.
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.
bill = 137.50
people = 3
tip_rate = 0.18
# Your code hereShow 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 = valuebinds a name to a value; no declaration needed.intcounts,floatmeasures, andtype()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.