3.1 Arithmetic Operators (BT101CO)

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division. They are the most common tokens used for numerical problem-solving.

1. Basic Arithmetic Operators

These operators follow the standard rules of mathematics.

  • Addition (+): Adds two operands.
    10 + 515
  • Subtraction (-): Subtracts the right operand from the left.
    10 - 55
  • Multiplication (*): Multiplies two operands.
    10 * 550
  • Division (/): Divides the left operand by the right and always returns a float (decimal).
    10 / 42.5

2. Specialized Arithmetic Operators

Python includes specific operators for handling remainders, powers, and integer-only division.

Modulus (%)

Returns the remainder of a division. It is highly useful for checking if a number is even or odd (e.g., x % 2 == 0).

10 % 31 (because 3 goes into 10 three times with 1 left over).

Exponentiation (**)

Calculates the power of a number (Left operand raised to the power of the right).

2 ** 38 (2 × 2 × 2)

Floor Division (//)

Divides the operands and discards the fractional part, returning only the whole number (integer).

  • 10 // 42 (the .5 is dropped).
  • -10 // 4-3 (it rounds toward the lower integer).

3. Arithmetic with Different Data Types

Python handles calculations between integers and floats automatically through Implicit Type Conversion.

  • Int + Int = Int (5 + 5 = 10)
  • Int + Float = Float (5 + 5.0 = 10.0)
  • Float + Float = Float (5.2 + 4.8 = 10.0)

Summary Table

Operator Name Example Result
+ Addition 5 + 2 7
- Subtraction 5 - 2 3
* Multiplication 5 * 2 10
/ Division 5 / 2 2.5
// Floor Division 5 // 2 2
% Modulus 5 % 2 1
** Exponent 5 ** 2 25

Practice Quiz