Hello guys. Welcome to LearnVern. In the last topic we saw a random module.
Today’s topic is the Math Module.
What is a Math Module?
Math module is used to do a mathematical task in Python.
Python gives this facility in the Math module.
If you want to find any factorial, round off, square root...you can do all of it through the Math Module.
These are some of the operations of the Math module.
Such as Factorial, if you want to find out Factorial, you can use this function available in the Math module.
Second is the Ceil (pronounced as in Ceiling) function which rounds up the value.
Next is Floor. Floor will round down the value. Means it will take the nearest lower version.
And Ceil will take the nearest upper version.
Same is the square root. You must already be knowing square roots as we all have surely studied the same in our childhood.
Python has a sqrt function which will take the square root of the value.
Let us see how we can use this Math module practically.
First of all, you’ll have to import the math module and then only further use the math operations.
Let’s begin the practical part.
I’ll open Jupyter.
Before using any operations, you have to import the math module.
I’ll show you both the ways to import a math module.
You can directly write Import & then Math. Or the other way is…
Like we saw in Random. From math import and then the name of the function you want to use.
You can import math modules in these 2 ways, okay?
Let’s work with math operations.
First is Factorial number.
I’ll write down Factorial here.
Now how will we use it?
First of all, I’ll import the Math module.
Now we can use any math operations.
Suppose I print math here . (dot) tab.
If you want any suggestions such as factorial functions etc, there is an option in Jupyter, called Math . (dot) tab & you will get all the functions in it.
I want to use factorial right now so we got it here.
Now we have to pass the parameter.
You will pass that particular number whose factorial you want to find out.
Suppose I want to find out 9. It found the factorial out.
We will find out a couple of more number’s factorial in the same way.
I’ll write here 6.
It found out the factorial of 6.
Let’s find out the factorial of 12 as well.
As you can see, it found 12’s factorial as well.
So this is how the factorial method works in the Math module.
Now we will use the next method which is Ceil.
I’ll write Ceil here.
First of all, I’ll import my math module.
Now we will print. Remember in Ceil, the nearest upper version of the value will be found.
Math . (dot) Ceil function in which I’ll take 1.4.
It will round up all the float values.
It brought the nearest to 2.
This Ceil brought the nearest value of 1.4 to 2 but this one is upper.
We will print Math . (dot) ceil 5.3. And it brought 6.
Let’s check more such values.
Let’s go for the negative value this time. -5.3. Then it brought -5.
This is how it will bring the nearest values to the upper round up.
Let’s work with the Floor method now.
We will import math first.
I’ll print Math . floor. Suppose I write 0.6, it will bring 0.
It will bring the nearest down value.
Ceil will bring the upper value & float will bring the down value.
Clear?
I’ll copy paste it.
I’ll change the value to 1.4.
Run & it will show 1. Previously it brought 2 and in Floor, it brought 1.
Ceil upgrades & Floor downgrades.
So this is how my floor function works in the math module.
Next function is the square root function.
Import math module.
Print math . (dot) S. Will bring square root function.
Suppose I take a square root of 10. Here it is.
Let’s check other values as well.
Suppose I took 100, it gave me a square root of 10.0.
Square root function helps in finding the square root of any value.
So this is how you can use the functions of the math module.
What did we learn today?
We learned how to import math module, we checked the factorial function, we saw how to use the Ceil function as well as the Floor function.
We also worked with the square root function.
Our next topic will be File Handling..
Thank you guys.
Share a personalized message with your friends.