In the previous article, we have discussed Python Program for Dictionary setdefault() Method
fmod() Function in Python:
The remainder (modulo) of x/y is returned by the math.fmod() method.
The Python fmod() math function is used to compute the Modulo of the given arguments.
Syntax:
math.fmod(x, y)
Parameters:
x: This is required. It is a positive or a negative number to divide.
y: This is required. It is a positive or negative number with which to divide x
Note:
If both x and y are zero, a ValueError is returned.
If y = 0, a ValueError is returned.
If x or y is not a number, it returns a TypeError.
Return Value:
It returns a float value which indicates the remainder of x/y.
Examples:
Example1:
Input:
Given list = [1, -2, -3, 4] Given first number = 18 Given second number = 2 Given number = 3
Output:
The remainder when given first number/second number { 18 / 2 } = 0.0
The remainder when gvn_lst[2]/given number { -3 / 5 } = -3.0
The remainder of -16/5 = -1.0Example2:
Input:
Given list = [2, 4, 6, 8, 10] Given first number = 8 Given second number = 5 Given number = 4
Output:
The remainder when given first number/second number { 8 / 5 } = 3.0
The remainder when gvn_lst[3]/given number { 8 / 4 } = 0.0
The remainder of -25/-4 = -1.0Program for fmod() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the first number as static input and store it in another variable.
- Give the second number as static input and store it in another variable.
- Apply math.fmod() function to the given first and the second number to get the remainder of the given first number/given the second number.
- Store it in another variable.
- Print the above result which is the result after applying fmod() function.
- Give the number as static input and store it in another variable.
- Apply math.fmod() function to the given list element and above-given number and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [1, -2, -3, 4]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 18
# Give the second number as static input and store it in another variable.
gvn_numb2 = 2
# Apply math.fmod() function to the given first and second number to get the
# remainder of given first number/given second number.
# store it in another variable.
remaindr = math.fmod(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying fmod() function.
print(
"The remainder when given first number/second number {", gvn_numb1, "/", gvn_numb2, "} = ", remaindr)
# Give the number as static input and store it in another variable.
gvn_numb3 = 5
# Apply math.fmod() function to the given list element and above given number and print it.
print(
"The remainder when gvn_lst[2]/given number {", gvn_lst[2], "/", gvn_numb3, "} = ", math.fmod(gvn_lst[2], gvn_numb3))
# similarly do the same for other numbers and print it.
print("The remainder of -16/5 = ", math.fmod(-16, 5))
Output:
The remainder when given first number/second number { 18 / 2 } = 0.0
The remainder when gvn_lst[2]/given number { -3 / 5 } = -3.0
The remainder of -16/5 = -1.0Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give first and second numbers respectively as user input using int(), map(), input(), and split()
functions. - Store them in two separate variables.
- Apply math.fmod() function to the given first and the second number to get the remainder of the given first number/given the second number.
- Store it in another variable.
- Print the above result which is the result after applying fmod() function.
- Give the number as user input using the int(input()) function and store it in another variable.
- Apply math.fmod() function to the given list element and above-given number and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Give first and second numbers respectively as user input using int(),map(),input(),and split()
# functions and store them in two separate variables.
gvn_numb1, gvn_numb2 = map(int, input(
"Enter two random numbers separated by spaces = ").split())
# Apply math.fmod() function to the given first and second number to get the
# remainder of given first number/given second number.
# store it in another variable.
remaindr = math.fmod(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying fmod() function.
print(
"The remainder when given first number/second number {", gvn_numb1, "/", gvn_numb2, "} = ", remaindr)
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb3 = int(input("Enter some random number = "))
# Apply math.fmod() function to the given list element and above given number and print it.
print(
"The remainder when gvn_lst[3]/given number {", gvn_lst[3], "/", gvn_numb3, "} = ", math.fmod(gvn_lst[3], gvn_numb3))
Output:
Enter some random List Elements separated by spaces = 2 4 6 8 10
Enter two random numbers separated by spaces = 8 5
The remainder when given first number/second number { 8 / 5 } = 3.0
Enter some random number = 4
The remainder when gvn_lst[3]/given number { 8 / 4 } = 0.0Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.