total = 4 + 3Line-by-line explanation
4is the first number.+means add.3is the second number.- The result
7is stored intotal.
Python uses + for addition, - for subtraction, * for multiplication, and / for ordinary division.
pieces = 12 // 5Line-by-line explanation
12is divided by5.//means whole-number division.- The result is
2; the leftover part is removed.
% gives the remainder, and ** raises a number to a power. Parentheses make the order of a calculation clear.
result = (2 + 3) * 4Line-by-line explanation
- The parentheses make
2 + 3happen first. - That result is multiplied by
4. resultstores20.
Tiny Practice
Calculate the cost of 3 items that each cost 5.
Starter template
cost = CHANGE_ME
print(cost)Line-by-line explanation
- The first line must calculate and store the cost.
- The second line displays it.
Hint
- Use
*. - Multiply 3 by 5.
- Do not use quote marks.
Show Solution
cost = 3 * 5
print(cost)Line-by-line explanation
3 * 5calculates15.- The result is stored in
cost. - The output is
15.