
Code
Number Crunching
Python is a superb calculator. These symbols do the math. They are called operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
add | 7 + 2 |
9 |
- |
subtract | 7 - 2 |
5 |
* |
multiply | 7 * 2 |
14 |
/ |
divide | 7 / 2 |
3.5 |
** |
power | 7 ** 2 |
49 |
Notice that / always gives a decimal number: 8 / 2 is 4.0.
You can do math with variables, too:
price = 4
lanterns = 3
cost = price * lanterns
print(cost) # 12
Python follows normal math order: * and / happen before
+ and -. Use brackets to choose your own order:
(2 + 3) * 4 is 20.
Assignment
Pip is buying lanterns for the Guild. Use the variables, not the numbers themselves:
- Set
costtopricetimeslanterns. - Set
changetocoinsminuscost. - Set
sharetocostdivided byfriends.
Pip is buying lanterns for the Guild. Use the variables, not the numbers themselves:
- Set
costtopricetimeslanterns. - Set
changetocoinsminuscost. - Set
sharetocostdivided byfriends.
main.py Python sleeping
Press Run to try your code, then Check to test it.