PythonLesson 3 of 16
Numbers and operators in Python
Python is a handy calculator that also keeps results in variables. Let's add up a café bill and share a pizza fairly.
Key points
- +, -, * and / work like on a calculator
- / always returns a decimal: 780 / 4 = 195.0
- // divides and drops the remainder, % gives the remainder
- ** raises to a power: 2 ** 3 = 8
- round(x, 2) keeps two digits after the point
Example
coffee = 180croissant = 120total = 3 * coffee + 2 * croissantprint("Total:", total)print("Each of 4 pays:", total / 4)print("Squared:", 12 ** 2)print(round(10 / 3, 2))Multiplication runs before addition, just like in school. Dividing with / gives 195.0, with a decimal point even when it divides evenly.
Task
How to use the editor⌘ Enter or Ctrl Enter runs the code, ⇧ ⌘ Enter checks the task.Tab and Shift Tab indent the line or the selection; Esc, then Tab leaves the editor.The check compares what the program printed with the task; spaces at line ends do not matter.After two failed checks you may open the solution. Your code is kept in this browser.17 pizza slices, 5 friends. Work out how many whole slices each friend gets (each) and how many are left (left).
Output
What your program prints appears here.