Python operator mod()

The operator.mod function in Python's operator module performs a modulo operation on two numbers. It is equivalent to using the % operator but allows the modulo operation to be used as a function, which can be useful in functional programming and higher-order functions.

Table of Contents

  1. Introduction
  2. operator.mod Function Syntax
  3. Examples
    • Basic Usage
    • Using with Lists
    • Using in Functional Programming
  4. Real-World Use Case
  5. Conclusion

Introduction

The operator.mod function is part of the operator module, which provides a set of functions corresponding to standard operators. The operator.mod function specifically performs a modulo operation on two numbers. This can be particularly useful when you need to pass the modulo operation as a function to other functions or use it in places where a function is required.

operator.mod Function Syntax

Here is how you use the operator.mod function:

import operator

result = operator.mod(a, b)

Parameters:

  • a: The dividend (a number).
  • b: The divisor (a number).

Returns:

  • The result of a % b, which is the remainder when a is divided by b.

Examples

Basic Usage

Perform a modulo operation using operator.mod.

Example

import operator

a = 10
b = 3
result = operator.mod(a, b)
print(f"mod({a}, {b}) = {result}")

Output:

mod(10, 3) = 1

Using with Lists

Perform element-wise modulo operation on two lists using map and operator.mod.

Example

import operator

list1 = [10, 20, 30]
list2 = [3, 4, 5]
result = list(map(operator.mod, list1, list2))
print(f"Element-wise modulo of {list1} and {list2} = {result}")

Output:

Element-wise modulo of [10, 20, 30] and [3, 4, 5] = [1, 0, 0]

Using in Functional Programming

Use operator.mod with filter to find elements in a list that are multiples of a given number.

Example

import operator

numbers = [10, 15, 20, 25, 30]
divisor = 5
result = list(filter(lambda x: operator.mod(x, divisor) == 0, numbers))
print(f"Multiples of {divisor} in {numbers} = {result}")

Output:

Multiples of 5 in [10, 15, 20, 25, 30] = [10, 15, 20, 25, 30]

Real-World Use Case

Determining Even or Odd Numbers

In data processing, you might need to determine if numbers are even or odd. The operator.mod function can be used to perform this check.

Example

import operator

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: operator.mod(x, 2) == 0, numbers))
odd_numbers = list(filter(lambda x: operator.mod(x, 2) != 0, numbers))

print(f"Even numbers: {even_numbers}")
print(f"Odd numbers: {odd_numbers}")

Output:

Even numbers: [2, 4, 6]
Odd numbers: [1, 3, 5]

Conclusion

The operator.mod function is used for performing modulo operations in a functional programming context in Python. It provides a way to use the modulo operation as a function, which can be passed to other functions or used in higher-order functions. By understanding how to use operator.mod, you can write more flexible and readable code that leverages functional programming techniques and efficiently performs modulo operations.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare