Skip to content

2.5/6/7 Expressions

Operators

Untitled.png

Order Of Operations

201.png

Compound Operators

  • Provide shorthand way to update a variable

Untitled%202.png

Division and Modulo

The division operator / performs division and returns a floating-point number. Ex:

  • 20 / 10 is 2.0.
  • 50 / 50 is 1.0.
  • 5 / 10 is 0.5.

The floored division operator // can be used to round down the result of a floating-point division to the closest whole number value. The resulting value is an integer type if both operands are integers; if either operand is a float, then a float is returned:

  • 20 // 10 is 2.
  • 50 // 50 is 1.
  • 5 // 10 is 0. (5/10 is 0 and the remainder 5 is thrown away).
  • 5.0 // 2 is 2.0

For division, the second operand of / or // must never be 0, because division by 0 is mathematically undefined.

The modulo operator (%) evaluates the remainder of the division of two integer operands. Ex: 23 % 10 is 3.

  • 24 % 10 is 4. Reason: 24 / 10 is 2 with remainder 4.
  • 50 % 50 is 0. Reason: 50 / 50 is 1 with remainder 0.
  • 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1.

Untitled