What you will learn
- Use arithmetic operators correctly.
- Understand /, //, %, and **.
- Use operator precedence and parentheses.
- Build small formulas from input.
The code from this chapter
Type these programs yourself. The predictions below are the exercises; the explanations are in the book.
3.1 Arithmetic Operators
TPRM Lab 3.1: Seconds into Minutes
Type this into a file, predict the output, then run it.
total_seconds = 367
minutes = total_seconds // 60
seconds = total_seconds % 60
print(minutes)
print(seconds)
3.2 Precedence
TPRM Lab 3.2: Same Numbers, Different Meaning
Type this into a file, predict the output, then run it.
print(2 + 3 * 4)
print((2 + 3) * 4)