Detailed Notes
We cover some Math and Random functions that will likely be on the certification exam in this episode.
Helpful links: https://docs.python.org/3/library/math.html
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] well hello and welcome back we're continuing our exploration of python in the attempt to get to a certification working our way through the certified associate in python system or their syllabus and this episode and actually probably the next couple we're gonna just crank through some commands uh we've gone through several uh logical flows and things like that and essentially one of the first set of objectives and now we're going to wrap sort of swing back around and catch some commands this episode we're going to focus on math and random related stuff now these are we're going to have to import math this is coming out of the math module so we're going to have to import this and the first one we're going to look at is seal or ceiling and let's just go ahead and take a look at those so for those two let me do clear and so if i run these so ceiling of 2.4 is gonna be a three ceiling of three point seven is going to be four now you may be able to guess uh now actually let me do these because this has to do and both these most of these do with um non-integers so if we're dealing with floats but let's do this just to be so the ceiling of three and the ceiling of two so notice here that uh basically this is a red ceiling is a rounding up even if 2.4 it's not rounding it to like 2.5 it's gonna round it all the way to three 3.7 it's gonna round it to four two it's gonna round to two and three it's gonna round to three and notice that two if it's 2.0 then the ceiling is going to be two but if we make it 2.001 now it's going to be three because it's something as long as it's at all higher than that number it's going to jump to the next one now the reverse of that is floor and we can take that i guess maybe not exactly the reverse but so ceiling is going to no matter what go down to the go up to the next level essentially into the next integer pure full integer value if we do floor oops i need to make sure i actually call those and not just let's say i'm going to call them so now whoop so if we go here so floor we can see 2.4 it's essentially rounding down so it's going to go down to the nearest integer value so 2.4 goes two 3.7 goes three two goes to two three goes to three and that's even here if we do something above it's still going to be down to that floor now let's look at trunk the trunks a little different so a trunk we're going to take it here and we're going to go through here change all these over and we're going to see in this case whoop where'd that go there we go now here we're going to see with trunk 2.4 becomes 2 3.7 comes 3 2 becomes 2 and three becomes three and actually that's that two point zero zero one and the reason why what trunk does is it just basically cuts it off it's almost it's basically like floor i'm not sure where floor and trunk would not have the same uh effect but essentially what's happening floor is going to round down to an integer and trunk instead of rounding down what it does is it just truncates to the integer portion so anything from the oops from the decimal and then to the right just gets dropped off so that's what a trunk is going to do for you and now if we go to factorial i'm not going to do this quite the same way factorial is um let's do something a little let's do three and let's do five just in case you don't well in case you don't know what a factorial is i'm going to go ahead and run this first and then i'll show you how it gets to the answer um let's do four while we're at it just because that'll make it a little more obvious i think so now if we run factorial so factorial of 3 is 6 4 is 24 5 is 120. so if you don't know this factorial is just basically take every integer underneath it and multiply them together so three is gonna be three times two times one three times two is six times one is six four is four times three times two times one so four times so three times 2 times 1 is 6 times 4 it's 24 and then 5 is just going to be 4 factorial times 5 because it's 5 times 4 times 3 times 2 times 1 and 5 times 24 happens to be 120. it's important these are the kinds of things you're going to see pop up here and there uh so it is useful to know what they mean and probably how to oh let me grab that guy how to calculate them and these are there are other math functions i'm going to flip over that just a second just to show you that there are a ton but these seem to be the ones that are most often used uh let's see that show up in a so if you look uh and i'll probably throw this link out there but there this is within the math math functions and you see there's a ton here's ceiling uh we can do a comment what is this this is a combinatorial copy sign factorial which we just looked at floor uh f sums greatest common denominator gcd is closed infinite so there's a lot of ises in there but these are things we're going to run into including uh there's trunk and what are we doing here high pot which is i'm not sure why they use it but here we go this is the euclidean norm and it's the length of the vector from the origin to the point given by the coordinates which is interesting enough because it's going to be uh coordinates one so hypot is uh distance hypothesis hypoten distance to zeros basically so if we come in here and we do two point four three point seven and three what we're gonna see here is that we've got 2.4 is that's the distance to the 0.0 to the uh to the origin basically which doesn't really make sense it would be like an absolute value so if we do a negative of these then we're going to see that look that's an absolute value so it's how far is that from the origin and it's going to give you back a float so it's basically an absolute uh and then of course we get square root um let's just take sqrt [Music] and let's do square root of 100 and uh 625. oh let's just do 1024. just cuz and we're going to go a square root here and whoop now we're going to do a square root there and we're going to see that here we go so square root of 100 is 10 because 10 times 10 was 100 square root of 10 24 is 32 because 32 times 32 is 1024. now the other one that's really uh good to know is random and i think i need two so first let's do one's going to get fun because we're going to do a random [Music] 10. random dot random [Music] and we're gonna do that four times uh we'll do it five times just cuz [Music] uh what did i do oh i'm sorry that's right this one doesn't take an argument what you're going to get and this is where things like uh ceiling and floor and some of that may come in handy so now if i do this i've got these are the random numbers that i've got now if i wanted to do um let's say a random from one to a hundred then i'm gonna do this [Music] so i'm gonna generate five numbers from one to a hundred [Music] and then i can do uh let's just do floor of that well actually let's do we're gonna do math.ceilings it'll be at least so it'll be 1 to 100 and we're going to whoops close that off pardon my typing and so now we're going to do five numbers five random numbers from one to 100 and there we go so we just generated some number some random numbers very easily done we also have seed choice and sample and let's do this oh um the wrong didn't i get that now watch if i do um this is not gonna do so if i do random dot seed and i give it a number uh just one two three four five now what we're gonna see okay well let's do this first let's do this so if i run this several times you should be able to see that i'm getting different random numbers uh each time so i get different sets but if i seed it [Music] now note that i'm getting the exact same response every time so seed says force a specific seed and that's it's gets a little complicated into how random numbers are generated here but basically it says that all right you're going to set up get a set of random numbers but if i set the seed it's always going to be the same set because it's based off of that seed and that's good to know um let's see if i do sample well let's do choice first um random dot choice is actually gonna go from a sequence so let's go back and pull um my morning array [Music] and so now if i do this and i do a random choice from morning array and i'm just going to print that so i'm gonna pick three random choices from that i think i'll print those it will so now it's gonna be ten one and two let's do it again oh that's because i have a seed in there somewhere if i get rid of a seed um what did i do oh i just closed that file here so if i go let me do this let me let's say forces the same because i don't want to break this anymore random values or choices let's do this again so now without the seed i can see that it's just going to pick a choice out of this so i could also do colors equals let's do red green blue uh white black let's just throw some primary colors in [Music] there we can now make this colors so this is really useful [Music] when you need to just randomly grab some data or something like that so now see white green black white red green green red black so it's just randomly picking out of so i can do a choice i can randomly get a choice out of a set and what was the last oh sample and then let's do sample from here i'm gonna do it from our morning array [Music] and if you note this one i can actually instead of doing this three times i can say like i'm gonna pull out of here i'm gonna pull three uh let me pull four and let me pull one and this is out of morning array so i can see what i'm getting here three four and one is it's going to give me three choices it gives you an array which three choices out of that sample and note so if i do um it's not going to give me duplicates so if i do that same thing and i did the morning array which was what ten of them one two five six ten i do ten it's in random order but if you note it's everything one two three four five six so it's not going to duplicate so if i go beyond that then i get an error because and it tells me i think uh yeah sample larger than the population or is a negative so you can't have a negative and so you're randomly going to pull out of here without duplicates you're going to randomly pull a certain number so if you wanted to pull random colors and you say okay i need just two i want two colors and i don't want the same color twice and come in i'm going to do whoop that and i'm going to get red and black so there is a lot of power actually built in in a random sense with the random library and so that's one of them why they want you to have an idea because we do see this i've run into this more than a few times where this has been useful particularly choice and random seed depends on how you want to do stuff if you need to repeat it particularly like say unit testing or something like that you may want to verify um random choices based on a certain seed or you may also want to have like one seed and verify that if you do a different seat you don't get the same set of random source choices and then samples the same way note that c does impact choice and sample that being said i think that's a good point to stop for now we will have this in our github repository as always with this latest and uh next time around we're going to continue looking at some some of these commands some of these built-in things as we are just getting more comfortable with python as always go out there and have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
well hello and welcome back
we're continuing our exploration of
python in the attempt to get to a
certification
working our way through the certified
associate
in python system or their syllabus
and this episode and actually probably
the next couple we're gonna just
crank through some commands uh we've
gone through several
uh logical flows and things like that
and essentially one of the first set of
objectives
and now we're going to wrap sort of
swing back around and catch some
commands this episode we're going to
focus on math and random
related stuff now these are
we're going to have to import math this
is coming out of the math module
so we're going to have to import this
and the first one we're going to look at
is
seal or ceiling and let's just go ahead
and take a look at those so for those
two let me do
clear and so if i run these
so ceiling of 2.4 is gonna be a three
ceiling of three point seven is going to
be four now you may be able to guess
uh now actually let me do these
because this has to do and both these
most of these do with um
non-integers so if we're dealing with
floats but let's do this
just to be so the ceiling of three and
the ceiling of
two
so notice here that uh basically this is
a red ceiling is a rounding
up even if 2.4 it's not rounding it to
like 2.5 it's gonna round it all the way
to three 3.7 it's gonna round it to four
two it's gonna round to two and three
it's gonna round to three and notice
that two
if it's 2.0 then the ceiling is going to
be
two but if we make it 2.001
now it's going to be three because it's
something as long as it's
at all higher than that number it's
going to jump to the next one
now the reverse of that
is floor
and we can take that
i guess maybe not exactly the reverse
but
so ceiling is going to no matter what go
down to the
go up to the next level essentially into
the next integer
pure full integer value if we do floor
oops
i need to make sure i actually call
those and not just
let's say i'm going to call them so now
whoop
so if we go here so floor we can see 2.4
it's essentially rounding down so it's
going to go down to the nearest integer
value so 2.4 goes
two 3.7 goes three two goes to two
three goes to three and that's even here
if we do something above it's still
going to be
down to that floor
now let's look at
trunk the trunks a little different
so a trunk we're going to take it here
and we're going to go through here
change all these over
and we're going to see in this case
whoop
where'd that go there we go now here
we're going to see with trunk
2.4 becomes 2 3.7 comes 3
2 becomes 2 and three becomes three and
actually that's that two point zero zero
one
and the reason why what trunk does is it
just basically
cuts it off it's almost it's basically
like floor
i'm not sure where floor and trunk would
not
have the same uh effect
but essentially what's happening floor
is going to round down to an integer
and trunk instead of rounding down what
it does is it just truncates
to the integer portion so anything from
the oops
from the decimal and then to the right
just gets dropped off so that's what a
trunk
is going to do for you and now if we go
to
factorial
i'm not going to do this quite the same
way factorial is
um let's do something a little let's do
three and let's do
five
just in case you don't well
in case you don't know what a factorial
is
i'm going to go ahead and run this first
and then i'll show you how it gets to
the answer
um let's do four while we're at it just
because that'll make it
a little more obvious i think
so now if we run factorial
so factorial of 3 is 6 4 is 24
5 is 120. so if you don't know this
factorial is just basically take every
integer underneath it and multiply them
together so three
is gonna be three times two times one
three times two is six
times one is six four is
four times three times two times one so
four
times so three times 2 times 1 is 6
times 4 it's 24 and then 5 is just going
to be 4 factorial times 5
because it's 5 times 4 times 3 times 2
times 1 and 5 times 24 happens to be
120. it's important
these are the kinds of things you're
going to see pop up here and there
uh so it is useful to know what they
mean
and probably how to
oh let me grab that guy how to calculate
them
and these are there are other math
functions i'm going to flip over that
just a second just to
show you that there are a ton but these
seem to be the ones that are most
often
used uh let's see that show up in a
so if you look uh and i'll probably
throw this link out there but
there this is within the math math
functions and you see there's a ton
here's ceiling uh we can do a comment
what is this this is a combinatorial
copy sign factorial which we just looked
at
floor uh f sums greatest common
denominator gcd
is closed infinite so there's a lot of
ises in there
but these are things we're going to run
into including uh there's trunk
and what are we doing here high pot
which is i'm not sure why they use it
but here we go
this is the euclidean norm
and it's the length of the vector from
the origin to the point given by the
coordinates which is
interesting enough because it's going to
be uh coordinates one
so hypot is uh distance hypothesis
hypoten
distance to zeros basically
so if we come in here and we do two
point four three point seven
and three
what we're gonna see here is that we've
got
2.4 is that's the distance to the 0.0 to
the
uh to the origin basically which
doesn't really make sense it would be
like an absolute value so if we do
a negative of these
then we're going to see that look that's
an absolute value so it's how far is
that from
the origin and it's going to give you
back
a float
so it's basically an absolute uh and
then of course we get square
root um let's just take
sqrt
[Music]
and let's do square root of 100
and uh 625.
oh let's just do 1024.
just cuz
and we're going to go a square root here
and whoop now we're going to do a square
root
there
and we're going to see that here we go
so square root of 100 is 10
because 10 times 10 was 100 square root
of 10 24 is 32 because 32 times 32
is 1024.
now the other one that's really uh good
to know
is random
and i think i need two so first let's do
one's going to get fun because we're
going to do a
random
[Music]
10.
random dot random
[Music]
and we're gonna do that four times uh
we'll do it five times just cuz
[Music]
uh what did i do
oh i'm sorry that's right this one
doesn't take an argument
what you're going to get and this is
where
things like uh ceiling and floor and
some of that may come in
handy so now if i do this
i've got these are the random numbers
that i've got
now if i wanted to do um
let's say a random from one to a hundred
then i'm gonna do this
[Music]
so i'm gonna generate five numbers from
one to a hundred
[Music]
and then i can do uh let's just do floor
of that
well actually let's do
we're gonna do math.ceilings it'll be at
least
so it'll be 1 to 100
and we're going to whoops close that off
pardon my typing and so now we're going
to do five numbers five random numbers
from one to 100
and there we go so we just
generated some number some random
numbers very easily done
we also have seed choice and sample
and let's do this
oh um the wrong didn't i get that
now watch if i do
um this is not gonna do so if i do
random
dot seed and i give it a number
uh just one two three four five
now what we're gonna see okay well let's
do this first
let's do this so if i run this
several times you should be able to see
that i'm getting different random
numbers
uh each time so i get
different sets but if i seed it
[Music]
now note that i'm getting the exact
same response every time so seed says
force a specific seed and that's it's
gets a little complicated into how
random numbers are generated here
but basically it says that all right
you're going to set up get a set of
random numbers
but if i set the seed it's always going
to be
the same set because it's based off of
that seed
and that's good to know um let's see if
i do sample well let's do choice first
um random dot
choice is actually gonna go from a
sequence so let's go back and pull
um my morning array
[Music]
and so now if i do this and i do
a random choice from morning array
and i'm just going to print
that
so i'm gonna pick three random choices
from that
i think i'll print those it will so now
it's gonna be ten one and two let's do
it again oh that's because i have a seed
in there somewhere if i get rid of a
seed
um what did i do oh i just closed that
file here so if i go let me do this let
me
let's say forces the same
because i don't want to break this
anymore
random values or choices
let's do this again so now without the
seed
i can see that it's just going to pick a
choice out of this
so i could also do
colors equals
let's do red green
blue uh white black
let's just throw some primary colors in
[Music]
there
we can now make this colors
so this is really useful
[Music]
when you need to just randomly grab some
data or something like that
so now see white green black
white red green
green red black so it's just randomly
picking out of so i can do a choice i
can randomly get a choice out of a set
and what was the last oh
sample
and then let's do sample from here
i'm gonna do it from our morning array
[Music]
and if you note
this one i can actually instead of doing
this three times i can say like
i'm gonna pull out of here i'm gonna
pull three
uh let me pull four and let me pull one
and this is out of morning array so i
can see what i'm getting here three four
and one
is it's going to give me three choices
it gives you an array which three
choices out of
that sample and note so if i do
um it's not going to give me
duplicates so if i do that same thing
and i did the morning array which was
what ten of them one two five six ten
i do ten
it's in random order but if you note
it's everything one two three four
five six so it's not going to duplicate
so if i go beyond
that then i get an error
because and it tells me i think uh yeah
sample larger than the population or is
a negative so you can't have a negative
and so you're randomly going to pull
out of here without duplicates you're
going to randomly pull a certain number
so if you wanted to pull
random colors
and you say okay i need just two i want
two colors and i don't want the same
color twice
and come in i'm going to do whoop
that and i'm going to get red and black
so there is a lot of power actually
built in
in a random sense with the random
library and so that's one of them why
they want you to have an idea because we
do see this
i've run into this more than a few times
where this has been useful particularly
choice and random seed depends on
how you want to do stuff if you need to
repeat it particularly like say unit
testing or something like that you may
want to verify
um random choices based on a certain
seed
or you may also want to have like one
seed and verify that if you do a
different seat you don't
get the same set of random source
choices and then samples the same way
note that c does impact choice and
sample
that being said i think that's a good
point to stop for now
we will have this in our github
repository as always with this latest
and uh next time around we're going to
continue looking at some
some of these commands some of these
built-in things as we are just getting
more comfortable with python as always
go out there and have yourself a great
day
a great week and we will talk to you
next time
you