This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Operators [July 12, 2012, 6:29 a.m.]



Operators work with one or more objects and can perform tasks such as math, comparison, and inspection.

Math

There are standard operators for arithmetic:

Math Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Division
** Exponent

Additionally, you can modify a named value and assign the output of an operator to the name in one line with inline assignment operators.

>>> a_number = 1
>>> a_number += 1
>>> a_number
2
>>> a_number *= 8
>>> a_number
16
>>> a_number **= 2
>>> a_number
256
>>> a_number /= 2
>>> a_number
128
>>> a_number %= 3
>>> a_number
2

Assignment

  1. Run Python commands to add, subtract, multiply and divide numbers from the Python interpreter command line.
  2. Create simple Python script program to perform numeric calculations and output results (e.g. calculating the area of a rectangle or circle).

Further Reading