1. Arithmetic
Operators are symbols that combine values. The values they combine are operands, and the whole expression produces a result.
Python's basic arithmetic operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | addition | 7 + 3 | 10 |
- | subtraction | 10 - 4 | 6 |
* | multiplication | 6 * 7 | 42 |
/ | division | 10 / 4 | 2.5 |
+, -, * and / are binary operators: they need one operand on each side.
/ always returns a float, even when the division is exact:
+, - and * keep the type of their operands: two int values produce an int, and an operand that is a float produces a float.
Variables can be operands too:
Add two numbers
Use the addition operator (+) to add 7 and 3, and print the result.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Subtract two numbers
Use the subtraction operator (-) to subtract 4 from 10, and print the result.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Multiply two numbers
Use the multiplication operator (*) to multiply 6 by 7, and print the result.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Divide two numbers
Use the division operator (/) to divide 8 by 2, and print the result. Division always returns a float, even when the result is exact.
Print exactly:
Create a free account to save your progress and unlock chapter 2.
Multiply with variables
Store 12 in a variable named price and 3 in a variable named quantity. Print the result of multiplying them with *.
Print exactly:
Create a free account to save your progress and unlock chapter 2.