Arithmetic Operators
Arithmetic operators work on values and give one result. Addition uses the + operator and combines two values into a single total. Subtraction uses - and finds the difference between two values. Multiplication uses * and finds the product of two values.
| Operator | Name | What it does | Example |
|---|---|---|---|
+ | Addition | Combines two values into one total | 46 + 37 gives 83 |
- | Subtraction | Finds the difference between two values | 46 - 37 gives 9 |
* | Multiplication | Finds the product of two values | 6 * 14 gives 84 |
In Python you write the operator between the two values it works on, and it produces one result. The order of the values does not matter for addition or multiplication, but it does for subtraction: 46 - 37 gives 9 while 37 - 46 gives -9. A negative answer shows that the second value is the larger of the two.
The + operator does different jobs depending on the type of the values either side of it. With two numbers it adds them, but with two strings it joins them end to end into one longer string. So "Good " + "morning" gives "Good morning". A space counts as a character too, so without it "Good" + "morning" gives "Goodmorning"; to keep the gap between the words you must put the space inside one of the strings.
points_home = 34
points_away = 51
print(points_home + points_away) # total points scored
print(points_away - points_home) # winning margin
trays = 12
cakes_per_tray = 8
print(trays * cakes_per_tray) # 12 trays of 8 cakes
print("Good " + "morning")Output:
85
17
96
Good morning