Use Python as a calculator: the seven arithmetic operators, the order they run in, and whole versus decimal results.
About 25 minFirst steps4 exercisesFree
You will be able to
Calculate with + - * / // % and **
Predict the order Python evaluates a calculation in, and change it with parentheses
Use // and % to split a quantity into whole groups and a remainder
Tell when a result will be a whole number (int) and when it will be a decimal (float)
Totals on a receipt, boxes needed for a shipment, the average of a class's marks: a huge amount of everyday programming is arithmetic. Python is an excellent calculator, and unlike a calculator it never mistypes a number it has already worked out.
In this lesson you'll learn Python's arithmetic operators (symbols such as + that combine two numbers), the order Python applies them in, and the two kinds of number you'll meet most often.
The arithmetic operators
Operator
Meaning
Example
Result
+
add
7 + 2
9
-
subtract
7 - 2
5
*
multiply
7 * 2
14
/
divide
7 / 2
3.5
//
divide, keep only the whole part
7 // 2
3
%
remainder after dividing
7 % 2
1
**
power
7 ** 2
49
Multiplication uses *, not x, and powers use **. The spaces around operators are optional, but they make calculations easier to read.
A grocery order, worked out by Python
print("Apples:",4*3)print("Milk:",2*6)print("Total:",4*3+2*6)print("Change from 50:",50-(4*3+2*6))
Try it: Change the number of apples from 4 to 7. Every line that depends on it updates automatically.
Whole groups and leftovers: // and %
// (called floor division) divides and rounds down to a whole number. For the positive numbers you'll use here, that means throwing away anything after the decimal point. % (called modulo, or remainder) gives what is left over. Together they answer questions like "how many full boxes, and how many items left over?"
Packing 50 eggs into boxes of 12
print("Full boxes:",50//12)print("Eggs left over:",50%12)
Try it: Try 48 eggs. What is the remainder when the eggs divide exactly?
50 eggs make 4 full boxes of 12 (that's 48 eggs), with 2 left over. When a number divides exactly, the remainder is 0.
The order of operations
When a calculation has several operators, Python follows the same order you learned in maths:
** first,
then *, /, // and %,
then + and -.
Operators at the same level run left to right. Parentheses override the order: whatever is inside ( ) is worked out first.
A float (floating-point number) has a decimal point: 12.5, 0.0, -3.75.
Two rules cover most situations:
/always gives a float, even when the division is exact: 10 / 2 is 5.0.
If either number in a calculation is a float, the result is a float: 3 * 2.5 is 7.5, and 3 * 2.0 is 6.0.
+, -, *, // and % on two ints give an int, and so does ** as long as the power is not negative (2 ** -1 is 0.5).
int and float results
print(10/2)print(10//2)print(3*2.5)print(0.1+0.2)
Try it: Change 10 / 2 to 10 / 4, then to 10 // 4. Which one keeps the .5?
The last line prints 0.30000000000000004, not 0.3. Computers store decimals in binary, and a few decimals, like 0.1, cannot be stored exactly, so tiny rounding errors appear. It is not a mistake in your code. In Printing in depth you'll learn how to show a sensible number of decimal places.
Guided practice · ●difficulty 1 of 5 · about 4 min
Total a cafe order
A customer at Jasmine Cafe orders 3 coffees at 14 SAR each and 2 croissants at 9 SAR each. Print the cost of each part of the order and the total, exactly like this:
Coffees: 42
Croissants: 18
Total: 60
Let Python do the arithmetic: write calculations such as 3 * 14 in your code instead of typing the answers.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●difficulty 2 of 5 · about 5 min
Pack water bottles into boxes
A warehouse has 183 water bottles. Each box holds 24 bottles. Print how many boxes can be filled completely and how many bottles are left over:
Full boxes: 7
Bottles left over: 15
Use // and % with the numbers 183 and 24 so that Python works out both answers.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Debugging · ●●difficulty 2 of 5 · about 4 min
Fix the average mark
A teacher wants the average of three quiz marks: 17, 15 and 19. The program below prints:
Average: 38.333333333333336
That can't be right: the average of three marks out of 20 can't be more than 20. The correct output is:
Average: 17.0
Fix the calculation.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Edge cases · ●●difficulty 2 of 5 · about 5 min
Split a bill exactly and in whole riyals
Four friends share a restaurant bill of 250 SAR. Print three lines:
the exact share for each person,
the share in whole riyals (dropping the halalas),
how many riyals are still unpaid if everyone pays only the whole-riyal share.
Exact share: 62.5
Whole riyals each: 62
Left to cover: 2
Use the numbers 250 and 4 in your calculations.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
Python's arithmetic operators are + - * / // % and **.
// gives the whole part of a division and % gives the remainder: together they split things into groups and leftovers.
** runs first, then * / // %, then + and -. Parentheses change the order.
Whole numbers are ints; numbers with a decimal point are floats. / always gives a float.