Arithmetic Operators

Nerd Cafe

What Are Arithmetic Operators?

Arithmetic operators are used to perform mathematical operations in Python like:

  • Addition

  • Subtraction

  • Multiplication

  • Division

  • etc.

These are fundamental in Python and especially important when working with data preprocessing, feature engineering, loss functions, and model evaluations in machine learning.

List of Arithmetic Operators in Python

Operator
Symbol
Description

Addition

+

Adds two values

a + b

Subtraction

-

Subtracts right from left

a - b

Multiplication

*

Multiplies two values

a * b

Division

/

Divides left by right (float result)

a / b

Floor Division

//

Divides and returns the integer part

a // b

Modulus

%

Returns remainder

a % b

Exponent

**

Raises a number to the power of another

a ** b

Let’s Explore Each One with Practical Notes

We'll define two variables and apply each operator.

Addition (+)

Use case in ML: Adding feature values, summing arrays, or updating weights in optimization.

Output:

Subtraction (-)

Use case in ML: Calculating error (y_pred - y_actual), cost reduction, or feature differences.

Output:

Multiplication (*)

Use case in ML: Element-wise product, scaling features, matrix dot product.

Output:

Division (/)

Returns float. Always remember in ML tasks, data normalization often uses floating-point division.

Output:

Floor Division (//)

Use case: Useful in splitting data or indexing (especially integer divisions like train/test split sizes).

Output:

Modulus (%)

Use case: Useful in batch processing (batch_index % batch_size) or finding remainder values in loop counters.

Output:

Exponentiation (**)

Use case: Common in ML when applying exponential decay (learning_rate ** epoch) or polynomial features.

Output:

Practical ML Examples Using Arithmetic Operators

Example 1: Feature Normalization

Uses subtraction and division.

Output:

Example 2: Gradient Descent Step

Subtraction, multiplication.

Output:

Example 3: Splitting Data into Batches

Floor division and modulus.

Output:

Notes and Tips

  • Always be mindful of data types: int, float — especially in division.

  • In Python:

    • 5 / 2 = 2.5 (float)

    • 5 // 2 = 2 (integer)

  • When using NumPy for vector operations, all these operators work element-wise. We’ll cover that in the NumPy section.

Summary Table with Examples

Operation
Code
Output

a+b

15 + 4

19

a - b

15 - 4

11

a * b

15*4

60

a / b

15 / 4

3.75

a // b

15 // 4

3

a % b

15 % 4

3

a** b

15 ** 4

50625

Keywords

arithmetic, operators, python, addition, subtraction, multiplication, division, floor division, modulus, exponentiation, machine learning, normalization, feature scaling, gradient descent, batch processing, data science, z-score, loss function, data preprocessing, numpy, nerd cafe

Last updated