Welcome to Engineering Python.
This is a Python programming course for engineers.
In this video, I'll give you a brief overview of the math library.
Python has many useful modules called libraries.
A Python library contains some useful definitions and statements that can be re-used by other
programs.
For example, we can find many math functions in the math library.
To use a library, we need to import it into our program first using the keyword followed
by the library's name.
In this case, it is math.
The math library has some constants we can use in our own programs.
For example, we can access pi's value in the math library using math.pi.
The output is the irrational number 3.1415.
This is case sensitive.
You cannot write it in this way, with a capital letter P.
The error says module math has no attribute capital letter Pi.
Another constant is Euler's number.
We can access its value using math.e.
The output is the irrational number 2.718.
This statement shows, the sum of pi and e is about 5.859.
There is also a constant inf, which represents the positive infinity.
Infinity plus any number will still be infinity.
Infinity minus infinity is not defined.
So the answer is nan, which is short for not a number.
The math library contains some functions that can do power and logarithmic operations.
exp four means e to the power of 4.
log two means the natural logarithm of 2 with base e.
We can specify the base as well.
log two ten means the logarithm of 2 with base 10.
The second parameter is the base.
sqrt nine means the square root of 9.
The ceil function will round the number up to the nearest integer.
So, 4.3 will be rounded up to 5.
The floor function will round the number down to the nearest integer.
So, 4.7 will be rounded down to 4.
The factorial function is self-explanatory.
Four factorial is equal to 4 times 3 times 2 times 1.
The result is 24.
Gcd will find the greatest common divisor of two integers.
The greatest common divisor of 35 and 49 is 7.
We can use the trigonometric functions defined in the math library.
If you don't have a math background and want to learn what trigonometric functions are,
Wikipedia is your best friend.
This is the sine function.
The sine of pi over 2 radians is 1.
The cosine of pi over 2 is 0.
The output is a scientific representation, 6 times 10 to the power of -17, which is really
close to zero.
This is because Python float data type is just an approximation of the true value.
The tangent of pi over 4 is 1.
Again, the output is 0.999, very close to 1.
Note there is no cotangent function in the math library.
If you want to calculate the cotangent, you need to do that using the reciprocal of the
tangent.
asin is the reverse sine function.
This statement returns the arc sine of 1.
The result is 1.57 radians, which is pi/2.
acos is the reverse cosine function.
This statement returns the arc cosine of 0.
The result is also about pi/2.
atan is the reverse tangent function.
This is the arc tangent of 1.
The result is about pi/4.
We can convert angles from radians to degrees and vice versa.
math dot degrees will convert pi/2 radians to 90 degrees.
math dot radians will do the reverse, convert 90 degrees to pi/2 radians.
More advanced hyperbolic functions like hyperbolic sine, cosine, tangent, and inverse hyperbolic
sine, cosine, and tangent can also be calculated.
I will not go through them one by one.
But just in case you need them, you can use this Jupyter Notebook as a reference.
Some people find it really annoying because they have to type math dot every time they
try to use a function in the math library.
Well, they don't have to.
They can import the math library in a different way to make their life a little bit easier.
Instead of import math, we use this statement, from math import star.
Star means everything in the library.
After you have done this, you don't have to type in math dot every time.
The constant pi is just pi.
Euler's number is just e, the sine function is just this, and so on.
This is really convenient.
But there could be some conflicts if you already used these name as other variables in your
program.
Be aware of that when you are importing the library in this way.
Okay, that was a brief overview of the math library.
The course materials are available on YouTube and GitHub.
You can watch the course videos in sequence.
If you like this video, please subscribe and share.
I'm Yong Wang.
Thanks for watching.