Chapter 3

Numbers, Operators, and Expressions

Programming turns useful once values can be combined, compared, and transformed.

Chapter 3, Numbers, Operators, and Expressions

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.

Python
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.

Python
print(2 + 3 * 4)
print((2 + 3) * 4)

Back to Python the TPRM Way