Python · basics

Arithmetic Operations

Python as a calculator: the seven operators, and the order it applies them in.

The operators

OperatorMeaningExample
+ - *add, subtract, multiply10 * 5 → 50
/division, always a float10 / 5 → 2.0
//floor division: round down10 // 3 → 3
%modulus: the remainder10 % 3 → 1
**power2 ** 3 → 8

Basic arithmetic


        # addition
        10 + 5
        # ans: 15
        # subtraction
        10 - 5
        # ans: 5
        # multiplication
        10 * 5
        # ans: 50
      

Division operations


        # division (always returns float)
        10 / 5
        # ans: 2.0
        10 / 3
        # ans: 3.3333...
        # floor division
        10 // 3
        # ans: 3
      

Floor division keeps the whole part: 10 / 3 is 3.33…, rounded down to 3.

Modulus


          # modulus (remainder)
          10 % 3
          # ans: 1
          7 % 3
          # ans: 1
          15 % 4
          # ans: 3
        

Make as many full groups as you can; the modulus is what is left over.

Exponentiation


          # power/exponentiation
          10 ** 2
          # ans: 100
          2 ** 3
          # ans: 8
          5 ** 0
          # ans: 1
        
  1. 10 ** 2
  2. 10 × 10
  3. 100
  1. 2 ** 3
  2. 2 × 2 × 2
  3. 8
  1. 5 ** 0anything to the power 0
  2. 1

Operator precedence


          # PEMDAS rules apply
          10 + 5 * 2
          # ans: 20 (multiply first)
          (10 + 5) * 2
          # ans: 30 (parentheses first)
          2 + 3 * 4
          # ans: 14
        
  1. 10 + 5 * 2
  2. 10 + 10
  3. 20
  1. (10 + 5) * 2
  2. 15 * 2
  3. 30
  1. 2 + 3 * 4
  2. 2 + 12
  3. 14

Parentheses, then **, then * / // %, then + -. The highlighted part runs next.

Exercises

Work each one out before pressing →.

Exercises · part 1


        # what is the result?
        7 % 3
        # ans: 1
        # division types
        10 / 3
        # ans: 3.3333...
        10 // 3
        # ans: 3
      

Exercises · part 2


          # negative modulus?
          -10 % 3
          # ans: 2
          # what is this?
          2 ** 3 ** 2
          # ans: 512 (right associative: 2^(3^2) = 2^9)
        
  1. -10 = 3 × (-4) + 2the remainder takes the sign of 3
  2. 2
  1. 2 ** 3 ** 2right to left
  2. 2 ** 9
  3. 512

Exercises · part 3


        # operator precedence?
        10 + 5 * 2
        # ans: 20
        # what happens?
        5 / 0
        # ans: ZeroDivisionError
      

Exercises · part 4


          # floor division with negative?
          -10 // 3
          # ans: -4
          # modulus with float?
          10.5 % 3
          # ans: 1.5
        
  1. -10 / 3 = -3.33…
  2. round down (towards −∞)
  3. -4
  1. 10.5 = 3 × 3 + 1.5
  2. 1.5

Exercises · part 5


        # what is result?
        100 // 10
        # ans: 10
        # power of zero?
        5 ** 0
        # ans: 1
        # negative exponent?
        2 ** -1
        # ans: 0.5
      

Exercises · part 6


          # order of operations?
          2 * 3 ** 2
          # ans: 18 (exponent first)
          # mixing operations?
          10 - 5 + 3
          # ans: 8 (left to right)
          # division by float?
          10 / 2.0
          # ans: 5.0
        
  1. 2 * 3 ** 2
  2. 2 * 9
  3. 18
  1. 10 - 5 + 3
  2. 5 + 3
  3. 8

Remember

/ vs //

/ always gives a float; // rounds down, even for negatives (-10 // 3 is -4).

% is the remainder

What is left after making as many full groups as possible.

Precedence

Parentheses, **, then * / // %, then + -; equal ranks go left to right, except **.

No dividing by zero

5 / 0 raises ZeroDivisionError.