How to Use the Python Math Module | Square Roots, Trigonometry and more!

LeMaster Tech
183 followers
24 Video Views·Apr 22, 2025
Python's Math module can allow for numerous complex mathematical operations to be easily programmed! Once imported, functions can be called by referencing 'math.' whatever and then inserting your variables!
This video covers the square root function, exponentials, logarithms, trigonometric functions (sin, cos, tan, arcsin, arccos, arctan), as well as using constants like Pi!! Let us know if you have any questions about what you see here or want to see covered next!!
Thanks for watching and goodluck with your projects!
hello welcome back to the channel and uh
today's video is going to be a
continuation in our python tutorial
series so today we're going to be taking
a look at using the math module which is
a really used a useful
module you can import in python to do a
lot of the more complex math operations
that aren't included in just base python
without having to write whole scripts
and routines to do the math so
as always when you add in a module
that's not just default you need to
start your program by importing it so
import module
import math right at the top and then
it's grayed out until you're using it so
we'll just get right into some of the
things it can do
hopefully you're familiar enough with
python and the different integrated
development environments like this is
pycharm that you're looking at
to know that you type the name of the
module and then dot or parentheses and
it kind of prompts you for what you can
do so if you're ever unsure if something
exists or not you can do uh type the
name of the module in and then dot and
typically the normal things that are
just associated with it are there if you
forget but i'll just highlight some of
the most common and valuable ones here
so like
one of the most useful ones i use all
the time is the sqrt function that's
square root and it basically takes the
square root of whatever you put inside
the parentheses so if i want to see the
square root of 25 boom right there it
gives you 5 it doesn't have to be an
integer it can be you know square root
21. it'll tell you 4.58 whatever
that's a nice one this sqrt function is
super common and comes in handy all the
time it's a lot easier than writing your
own um you know code
anytime you need to do something with
trigonometry all the trigonomet
trigonometric functions are in there so
sine
tangent
uh cosine even
arc sine 8 which is a sine or arc cosine
arc tangent they're all in there
the default for those i will say is
radians so you'll see if i say like
3.1415
that's radians so it's basically saying
the sign of pi
which is basically zero you see we get
nine to
ten to the negative fifth here i'll make
it just 3.14 so it's a little easier to
read
it's practically zero um
and we'll say we'll do
math cosine of the same 3.14
and we'll see yeah so cosine is
basically uh negative one at pi right so
you're probably saying well why not just
use pi and that's another super common
thing you can do with math is math dot
pi and that actually is pi so you'll see
we get exactly negative one if we came
back up here and replaced our 3.14 with
math.pi we'd get
so close to zero that it's
mathematically indistinguishable i'm not
sure why they're not comfortable with
just giving us the zero
um but there you go it's math.pi you
could always round it off to a few
digits and it basically would be zero
um another one i use it's a little less
common but it still comes in handy is a
factorial so if you're not familiar with
what a factorial is it's basically uh
you input a number here like five
and it does five times four times three
times two times one that's what a
factorial is so if i run that
okay 120 that's the same as coding five
times four
five times four times
3 times 2 times 1.
you'll see that these two are the same
but then it gets
this is super convenient if you get up
to numbers like 12
it's just doing a lot of multiplication
for you instead of having to say 12
times 11 times 10 times 9. so it's the
applications are less common than some
things like square root and using pi but
factorial is one of the built-in modes
as well uh exponential and logarithmic
you know it's just exp if you're doing
um e to the whatever so like
just exponential
one you know there you go 2.718
you can do the same with math dot
log of
whatever you give it x and then you also
give it the base so you can say like log
of 5 base 2.
you math junkies can check me on your
graphing calculators if you want but
um yeah so then another one that i
really like
is the math.pal this is one i use all
the time
you definitely could throw together a
little power loop um but what you can
say in this is you say 4
to the power of 7 and you give it the
number you want and then what power you
want it raised to so like 4 to the power
of 2
is just going to be 16 4 to the power of
4.
you know it gets up to 256 so you could
say okay well 8 to the power of 4.
um 40 96 and it's also really important
to touch on everywhere that i just put
in a number um you can replace with a
variable so if i said like my variable
is equal to
19
um
maybe i shouldn't say
variable because it's kind of a built-in
but let's say okay so we want
variable raised to the power of 4. as
long as it's the right data type you can
use variables instead of numbers so
putting these inside of functions is
really useful a lot of the time
those are really like the most common
ones i'll just scroll through the list
real quick and you guys can kind of see
like there are some variables as well
that's what the v means versus the f
which means function
so like pi was a built-in one e as a
constant is built in
um
ceiling and floor are two fairly useful
math tools they'll either round up no
matter what or they'll round down no
matter what
[Music]
i mean there's a lot of math that you
won't really need there's a lot of math
that you'll use once you can use
infinity within the math functionality
but kind of what we talked about here
today there's also radians and degrees
which convert from one to the other so
if you put a value in degrees and you
want to get it in radians or vice versa
you can use those
um again there's a a lot of built-in
math constants
and
in general it's just it's a nice tool
that has a bundle of a lot of the more
complex mathematical functions you might
use while coding in python and it's good
to know the basics so
if you have any questions about anything
you saw here today or you'd like a more
in-depth tutorial using any one or
several of these just let me know about
in the comments below and if you found
this or anything else on the channel
useful i really appreciate a like on the
video and a subscribe to the channel it
helps me out a ton and as always good
luck with your code and thanks for
watching thanks bye
