Introduction Python fmod() Method with Examples

Introduction Python fmod() Method with Examples

The math.fmod() is a library method of math module, it is used to find the modulus or given numbers, where the first parameter is a dividend and the second parameter is divisor. It accepts two arguments and returns modulus in float type.

The math.fmod() is a library method of math module, it is used to find the modulus or given numbers, where the first parameter is a dividend and the second parameter is divisor. It accepts two arguments and returns modulus in float type.

Note: math.fmod() can be used to get the modules/remainder of positive and negative integers, positive and negative floats.

Python fmod()

Syntax

math.fmod(x,y)

Parameter(s):

  • x, y – numbers (positive or negative, integer or float).

Return value:

  • float – it returns a float value, which is a remainder (modulus) of given numbers x, y.

The fmod() function returns a floating-point number value after calculating the module of the given two numbers.

Please note that,

  • If x and y are both zero, then this function returns a ValueError.
  • If the second argument means y is zero, then also it returns a ValueError.
  • If any of x or y is not a number, then this function returns a TypeError.
Examples of Python fmod() method

Example 1


# import math library
import math
print(math.fmod(25,5))
print(math.fmod(20,3))
print(math.fmod(-20,12))
print(math.fmod(-13,6))

Output


0.0
2.0
-8.0
-1.0

Note that in output all the numbers whether they are negative or positive are converted into a floating point number after calculating module through fmod() in Python programming language.

Example 2

Python code to demonstrate example of math.fmod() method


# app.py

# Importing math library
import math
# Demonstrating working of fmod()
# Using different values of x and y

# When both are positive
x = 12
y = 9
print("Module of ", x, " and ", y, "is: ", math.fmod(x, y))

# When any one of them are negative
x = -16
y = 3
print("Module of ", x, " and ", y, "is: ", math.fmod(x, y))

# When both are negative
x = -65
y = -31
print("Module of ", x, " and ", y, "is: ", math.fmod(x, y))

# When second argument (y) is 0
x = 10
y = 0
print("Module of ", x, " and ", y, "is: ", math.fmod(x, y))

Output


Module of  12  and  9 is:  3.0
Module of  -16  and  3 is:  -1.0
Module of  -65  and  -31 is:  -3.0
Traceback (most recent call last):
  File "fmod.py", line 24, in <module>
    print("Module of ",x," and ",y, "is: ",math.fmod(x,y))
ValueError: math domain error

In the above code, we have declared two variables x and y and given their different values in different cases.

After that, we have a printed module of each case, we can see that the answer is positive only when both x and y are positive, and except that in all cases, the answer is negative.

Also, we can see that in each case, the answer is in floating-point.

However, in the last case, when we gave the value of y is 0, we got a ValueError.

Thanks for reading !

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Learn Python Programming

Learn Python Programming

Learn Python Programming

Description
Learn Python Programming

Learn Python Programming and increase your python programming skills with Coder Kovid.

Python is the highest growing programming language in this era. You can use Python to do everything like, web development, software development, cognitive development, machine learning, artificial intelligence, etc. You should learn python programming and increase your skills of programming.

In this course of learn python programming you don't need any prior programming knowledge. Every beginner can start with.

Basic knowledge
No prior knowledge needed to learn this course
What will you learn
Write Basic Syntax of Python Programming
Create Basic Real World Application
Program in a fluent manner
Get Familiar in Programming Environment