Variables and arithmetic
Name values with variables, calculate with the operators, and read input.
16 min
A variable is a name for a value. You create one with =, which means store this value under this name, not "equals" as in maths:
price = 4
quantity = 3
total = price * quantity
print(total) # 12You can change what a name points to at any time: quantity = quantity + 1 reads the old value, adds one and stores the result back. Good names say what the value is: total_cost, not x. Use lowercase with underscores.
Python does arithmetic the way a calculator does, and obeys the usual order: brackets, then powers, then * / // %, then + -.
/ always gives a float (7 / 2 is 3.5). // divides and throws away the remainder (7 // 2 is 3). % gives only the remainder (7 % 2 is 1). Together they answer "how many whole groups, and what is left?"
input("Question? ") pauses, shows the question and returns what the user typed as a string, even if they typed digits. Wrap it in int() or float() to convert:
age = int(input("Age? "))
print(age + 1)The arithmetic operators
+
- Meaning
- add
- Example
7 + 2- Result
9
-
- Meaning
- subtract
- Example
7 - 2- Result
5
*
- Meaning
- multiply
- Example
7 * 2- Result
14
/
- Meaning
- divide (always a float)
- Example
7 / 2- Result
3.5
//
- Meaning
- floor divide
- Example
7 // 2- Result
3
%
- Meaning
- remainder
- Example
7 % 2- Result
1
**
- Meaning
- power
- Example
7 ** 2- Result
49
Brackets always win: `(1 + 2) * 3` is `9`, but `1 + 2 * 3` is `7`.
A shop sells pens at $1.50 each. You buy 6 and pay with a $20 note. How much change do you get?
- 1
pens = 6 price = 1.5
Name the two facts so the numbers are not scattered through the code.
Change one input at a time. Each one is a variable: the program is the same formula, and only the values change. Set the tax to 0 and check the total equals price × quantity.
Total to pay
—
Write total_cost(price, qty, tax_percent) that returns the price of qty items, plus tax, rounded to 2 decimal places.
For example total_cost(10, 3, 10) is 33.0: three items cost 30, and 10% tax adds 3.
Your code must satisfy
- total_cost(10, 3, 10)→33ten percent tax
- total_cost(19.99, 2, 0)→39.98no tax
- total_cost(5, 1, 20)→6one item
- total_cost(3, 3, 7)→9.63rounds
def total_cost(price, qty, tax_percent):
passRead a number of days from input and print it as whole weeks and leftover days, in this form:
2 weeks, 3 daysSo 17 days prints 2 weeks, 3 days. Always use the plural words.
Your code must satisfy
- 17 days
- exact weeks
- under a week
days = int(input())- A variable is a name for a value;
=stores, it does not compare. /always returns a float,//drops the remainder,%keeps only the remainder.- Brackets and the usual order of operations decide which part is calculated first.
input()always returns a string: convert withint()orfloat()before doing sums.
Work these out by hand first.
What is 17 // 5?
What is 17 % 5?
What is 2 ** 10?
After x = 4 then x = x + 1, what is x?
- A4
- B5
- C41
- DAn error
A user types 12 at n = input(). What is n + n?
- A24
- B"24"
- C"1212"
- DAn error
To turn the text the user typed into a whole number, wrap it in (...).
Capitalisation, spacing and punctuation are ignored when grading.
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of Python for Complete Beginners is planned around your week. Free.