📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Math and Random Libraries

2021-07-08 •Youtube

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
0.46

[Music]

26.32

well hello and welcome back

27.84

we're continuing our exploration of

30.88

python in the attempt to get to a

33.36

certification

34.8

working our way through the certified

36.64

associate

38.239

in python system or their syllabus

41.44

and this episode and actually probably

43.36

the next couple we're gonna just

44.64

crank through some commands uh we've

46.8

gone through several

48

uh logical flows and things like that

49.92

and essentially one of the first set of

51.68

objectives

52.8

and now we're going to wrap sort of

54.32

swing back around and catch some

56.559

commands this episode we're going to

58

focus on math and random

59.84

related stuff now these are

64.32

we're going to have to import math this

66.08

is coming out of the math module

67.84

so we're going to have to import this

70.56

and the first one we're going to look at

71.68

is

72.24

seal or ceiling and let's just go ahead

75.84

and take a look at those so for those

77.439

two let me do

79.119

clear and so if i run these

82.88

so ceiling of 2.4 is gonna be a three

87.2

ceiling of three point seven is going to

89.04

be four now you may be able to guess

91.52

uh now actually let me do these

95.36

because this has to do and both these

96.96

most of these do with um

100

non-integers so if we're dealing with

101.439

floats but let's do this

105.119

just to be so the ceiling of three and

107.36

the ceiling of

108.24

two

112

so notice here that uh basically this is

115.52

a red ceiling is a rounding

117.28

up even if 2.4 it's not rounding it to

120.56

like 2.5 it's gonna round it all the way

122.079

to three 3.7 it's gonna round it to four

124.88

two it's gonna round to two and three

128.399

it's gonna round to three and notice

131.2

that two

131.76

if it's 2.0 then the ceiling is going to

135.04

be

136.72

two but if we make it 2.001

140.239

now it's going to be three because it's

142.8

something as long as it's

144.4

at all higher than that number it's

147.28

going to jump to the next one

150.08

now the reverse of that

153.36

is floor

157.76

and we can take that

162.16

i guess maybe not exactly the reverse

164.08

but

165.28

so ceiling is going to no matter what go

167.36

down to the

169.04

go up to the next level essentially into

171.12

the next integer

172.239

pure full integer value if we do floor

175.519

oops

176.64

i need to make sure i actually call

178

those and not just

186.319

let's say i'm going to call them so now

188.959

whoop

190.319

so if we go here so floor we can see 2.4

193.2

it's essentially rounding down so it's

194.72

going to go down to the nearest integer

196.319

value so 2.4 goes

197.84

two 3.7 goes three two goes to two

201.599

three goes to three and that's even here

206

if we do something above it's still

207.84

going to be

209.28

down to that floor

213.12

now let's look at

217.12

trunk the trunks a little different

222.239

so a trunk we're going to take it here

233.76

and we're going to go through here

237.2

change all these over

240.4

and we're going to see in this case

243.2

whoop

244.239

where'd that go there we go now here

246.959

we're going to see with trunk

248.4

2.4 becomes 2 3.7 comes 3

251.92

2 becomes 2 and three becomes three and

254.159

actually that's that two point zero zero

255.76

one

256.88

and the reason why what trunk does is it

259.44

just basically

260.799

cuts it off it's almost it's basically

263.52

like floor

264.32

i'm not sure where floor and trunk would

267.759

not

268.32

have the same uh effect

272.32

but essentially what's happening floor

273.68

is going to round down to an integer

275.6

and trunk instead of rounding down what

277.52

it does is it just truncates

279.199

to the integer portion so anything from

282.32

the oops

283.04

from the decimal and then to the right

285.199

just gets dropped off so that's what a

287.12

trunk

287.6

is going to do for you and now if we go

291.28

to

292

factorial

295.44

i'm not going to do this quite the same

296.96

way factorial is

299.84

um let's do something a little let's do

302.479

three and let's do

303.6

five

307.52

just in case you don't well

311.6

in case you don't know what a factorial

314.16

is

315.84

i'm going to go ahead and run this first

317.68

and then i'll show you how it gets to

319.039

the answer

328.479

um let's do four while we're at it just

331.36

because that'll make it

332.72

a little more obvious i think

336.639

so now if we run factorial

340.32

so factorial of 3 is 6 4 is 24

344

5 is 120. so if you don't know this

348.16

factorial is just basically take every

350.4

integer underneath it and multiply them

352.32

together so three

353.44

is gonna be three times two times one

356

three times two is six

356.96

times one is six four is

360.24

four times three times two times one so

362.88

four

363.36

times so three times 2 times 1 is 6

366.88

times 4 it's 24 and then 5 is just going

370.56

to be 4 factorial times 5

372.4

because it's 5 times 4 times 3 times 2

374.16

times 1 and 5 times 24 happens to be

378.84

120. it's important

381.12

these are the kinds of things you're

383.36

going to see pop up here and there

385.28

uh so it is useful to know what they

388.08

mean

388.88

and probably how to

393.039

oh let me grab that guy how to calculate

395.759

them

399.759

and these are there are other math

401.6

functions i'm going to flip over that

403.199

just a second just to

404.56

show you that there are a ton but these

407.28

seem to be the ones that are most

409.12

often

412.479

used uh let's see that show up in a

416.16

so if you look uh and i'll probably

417.919

throw this link out there but

419.599

there this is within the math math

421.84

functions and you see there's a ton

423.52

here's ceiling uh we can do a comment

428.319

what is this this is a combinatorial

432.56

copy sign factorial which we just looked

436.24

at

436.639

floor uh f sums greatest common

440.24

denominator gcd

442.08

is closed infinite so there's a lot of

444.72

ises in there

445.84

but these are things we're going to run

447.039

into including uh there's trunk

452.24

and what are we doing here high pot

455.039

which is i'm not sure why they use it

456.96

but here we go

459.12

this is the euclidean norm

462.479

and it's the length of the vector from

464.08

the origin to the point given by the

465.44

coordinates which is

467.28

interesting enough because it's going to

469.28

be uh coordinates one

471.28

so hypot is uh distance hypothesis

474.72

hypoten

476.96

distance to zeros basically

480.08

so if we come in here and we do two

482.879

point four three point seven

484.24

and three

487.36

what we're gonna see here is that we've

491.12

got

491.52

2.4 is that's the distance to the 0.0 to

495.12

the

496.56

uh to the origin basically which

499.919

doesn't really make sense it would be

501.759

like an absolute value so if we do

506.879

a negative of these

513.36

then we're going to see that look that's

514.88

an absolute value so it's how far is

516.959

that from

518.32

the origin and it's going to give you

521.44

back

521.919

a float

525.6

so it's basically an absolute uh and

528.64

then of course we get square

530

root um let's just take

536.519

sqrt

542.29

[Music]

544.399

and let's do square root of 100

547.44

and uh 625.

552.72

oh let's just do 1024.

557.279

just cuz

565.2

and we're going to go a square root here

567.279

and whoop now we're going to do a square

568.8

root

569.519

there

574.08

and we're going to see that here we go

576.08

so square root of 100 is 10

577.6

because 10 times 10 was 100 square root

579.36

of 10 24 is 32 because 32 times 32

583.04

is 1024.

586.24

now the other one that's really uh good

589.12

to know

591.44

is random

595.6

and i think i need two so first let's do

604.079

one's going to get fun because we're

605.6

going to do a

607.2

random

608.14

[Music]

616.839

10.

618.24

random dot random

622.47

[Music]

627.6

and we're gonna do that four times uh

629.68

we'll do it five times just cuz

634.37

[Music]

636.72

uh what did i do

641.519

oh i'm sorry that's right this one

644.48

doesn't take an argument

649.2

what you're going to get and this is

651.44

where

652.32

things like uh ceiling and floor and

654.88

some of that may come in

656

handy so now if i do this

659.279

i've got these are the random numbers

661.6

that i've got

662.72

now if i wanted to do um

665.92

let's say a random from one to a hundred

671.12

then i'm gonna do this

674.82

[Music]

678.56

so i'm gonna generate five numbers from

680.56

one to a hundred

682.13

[Music]

684.56

and then i can do uh let's just do floor

690.8

of that

694.399

well actually let's do

697.68

we're gonna do math.ceilings it'll be at

700.839

least

702.959

so it'll be 1 to 100

711.92

and we're going to whoops close that off

719.2

pardon my typing and so now we're going

720.48

to do five numbers five random numbers

723.04

from one to 100

724.32

and there we go so we just

727.6

generated some number some random

729.36

numbers very easily done

731.68

we also have seed choice and sample

737.6

and let's do this

743.44

oh um the wrong didn't i get that

750.72

now watch if i do

757.36

um this is not gonna do so if i do

759.68

random

762

dot seed and i give it a number

766.24

uh just one two three four five

772.48

now what we're gonna see okay well let's

774.24

do this first

775.839

let's do this so if i run this

779.44

several times you should be able to see

780.88

that i'm getting different random

782.16

numbers

783.92

uh each time so i get

787.519

different sets but if i seed it

793.4

[Music]

795.44

now note that i'm getting the exact

799.12

same response every time so seed says

803.04

force a specific seed and that's it's

806.959

gets a little complicated into how

808.16

random numbers are generated here

810

but basically it says that all right

812.24

you're going to set up get a set of

813.36

random numbers

814

but if i set the seed it's always going

816.079

to be

817.36

the same set because it's based off of

819.44

that seed

821.519

and that's good to know um let's see if

824.56

i do sample well let's do choice first

830

um random dot

833.6

choice is actually gonna go from a

836.48

sequence so let's go back and pull

839.6

um my morning array

842.03

[Music]

845.279

and so now if i do this and i do

848.48

a random choice from morning array

852.24

and i'm just going to print

855.36

that

859.92

so i'm gonna pick three random choices

862

from that

864

i think i'll print those it will so now

866.959

it's gonna be ten one and two let's do

868.399

it again oh that's because i have a seed

870.639

in there somewhere if i get rid of a

872.399

seed

875.12

um what did i do oh i just closed that

880.16

file here so if i go let me do this let

882.839

me

887.839

let's say forces the same

892.56

because i don't want to break this

893.68

anymore

895.68

random values or choices

900.56

let's do this again so now without the

903.199

seed

904.959

i can see that it's just going to pick a

906.399

choice out of this

908.56

so i could also do

911.76

colors equals

916.399

let's do red green

923.199

blue uh white black

929.199

let's just throw some primary colors in

937.07

[Music]

938.839

there

945.519

we can now make this colors

949.92

so this is really useful

951.96

[Music]

953.279

when you need to just randomly grab some

956.639

data or something like that

959.199

so now see white green black

962.32

white red green

965.6

green red black so it's just randomly

967.36

picking out of so i can do a choice i

969.279

can randomly get a choice out of a set

971.04

and what was the last oh

973.12

sample

976.959

and then let's do sample from here

981.68

i'm gonna do it from our morning array

991.48

[Music]

993.36

and if you note

997.759

this one i can actually instead of doing

999.92

this three times i can say like

1001.839

i'm gonna pull out of here i'm gonna

1003.36

pull three

1007.12

uh let me pull four and let me pull one

1014.399

and this is out of morning array so i

1015.839

can see what i'm getting here three four

1017.759

and one

1018.639

is it's going to give me three choices

1020.16

it gives you an array which three

1021.36

choices out of

1023.44

that sample and note so if i do

1026.559

um it's not going to give me

1030.16

duplicates so if i do that same thing

1034

and i did the morning array which was

1035.36

what ten of them one two five six ten

1039.12

i do ten

1043.52

it's in random order but if you note

1046.48

it's everything one two three four

1048.48

five six so it's not going to duplicate

1050.88

so if i go beyond

1052

that then i get an error

1055.6

because and it tells me i think uh yeah

1058.64

sample larger than the population or is

1060.64

a negative so you can't have a negative

1062.96

and so you're randomly going to pull

1066.16

out of here without duplicates you're

1068

going to randomly pull a certain number

1069.6

so if you wanted to pull

1071.44

random colors

1075.36

and you say okay i need just two i want

1077.12

two colors and i don't want the same

1078.559

color twice

1080.799

and come in i'm going to do whoop

1084.16

that and i'm going to get red and black

1089.12

so there is a lot of power actually

1091.6

built in

1092.799

in a random sense with the random

1095.679

library and so that's one of them why

1097.039

they want you to have an idea because we

1098.4

do see this

1100.32

i've run into this more than a few times

1101.679

where this has been useful particularly

1104.08

choice and random seed depends on

1107.44

how you want to do stuff if you need to

1109.2

repeat it particularly like say unit

1111.12

testing or something like that you may

1112.559

want to verify

1114.16

um random choices based on a certain

1116.64

seed

1117.679

or you may also want to have like one

1119.6

seed and verify that if you do a

1121.2

different seat you don't

1122

get the same set of random source

1124.32

choices and then samples the same way

1126.559

note that c does impact choice and

1129.44

sample

1131.039

that being said i think that's a good

1132.96

point to stop for now

1134.96

we will have this in our github

1137.12

repository as always with this latest

1139.679

and uh next time around we're going to

1141.12

continue looking at some

1143.12

some of these commands some of these

1144.799

built-in things as we are just getting

1147.12

more comfortable with python as always

1150

go out there and have yourself a great

1151.12

day

1151.52

a great week and we will talk to you

1154.16

next time

1171.6

you