Python: Math, Less Familiar

Go to Chapter 6 of A Smarter Way.

A. Sometimes you need to find the remainder of a division operation. Really. It sounds crazy, but you’ll see why you’d want this later.

That remainder is called the modulus. And the operator that you use to get it is the modulo operator.

my_modulus = 10 % 3

B. The exponent operator is how you calculate powers of a number.

my_number = 10 ** 3

C. Shorthand operators are quick ways to perform common math.

You could, for instance, do this:

my_number = my_number + 1

But you can do it more simply:

my_number += 1

Both of these operations will add 1 to my_number. And you can do subtraction this way, too.

my_number -= 1

Exercise

Go to http://www.asmarterwaytolearn.com/python/6.html and review the exercises.

  1. Return to your examples.py script.
  2. Create a variable that contains the statement “First Edition: “.
  3. Use a shorthand operator to add 1 to the variable my_year.
  4. Print these two variables on one line.