📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Literals and Order of Precedence

2021-06-10 •Youtube

Detailed Notes

This episode looks into literal types and order of precedence. More to come on this in the next episode.

Helpful links: https://overiq.com/python-101/numbers-in-python/ https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/

Transcript Text
[Music]
well hello and welcome back
today we are going to talk about
as we work our way through our django
certification
we are going to talk about data types
particularly numeric types
and those are going to be things like
integers and floats and scientific
notation and
good stuff like that now going back to
our interpreter we can play around a
little bit
so if we do x equals 3
we can also do y equals 3.4 so three we
get an integer
no we can do a uh three point four we
can do
uh z equals true
and uh let's go to a equals
um to
and we're gonna do it uh scientific
notation
uh which is our other type which is a
um uh here we go
so scientific notation make sure i get
it right i don't use it very often so
it's an exponent oh
so if i do that to the like
uh ninth power and sure we'll do that i
could do to the negative
uh well let's do that so a equals that
and b
equals 3.14
did i get that right
i think i did that wrong but that's okay
so to the negative nine and that one is
actually to the ninth
i'm sorry so a little bit of typo there
so if i print a so you know this is um
scientific notation is just basically
this means times 10 to the ninth so it's
basically adding
nine decimals uh pushing it over to the
right adding
nine decimals to it so we get this huge
number uh if we use the smaller one
where
it goes the other way so negatives go to
the left positives go to the right
we're going to see that this is oh and
it's actually going to print it out it's
not going to do the
000. i think if we do
a equals like negative 4 let's see what
it does there
yeah so it's there's a certain point
where it's going to actually go to
scientific notation because
it's just a pain but if we look
for if we do x
y and z
we can see and note that true and false
are capital t or capital f
and we can actually do type so we can
see what these are so if i do type of a
that's a float even though oh
yes because it is scientific notation b
is also going to be a float
uh c if i do x that's going to be an int
because it's going to take the one that
makes the most sense
y it's going to be a float because it
has that decimal
and then if we do z we're going to see
if that is a boolean
and we can add these together so if we
do uh let's do
type of a plus b which is going to be
i'm going back here i'm sorry not a plus
b let's do x plus y
x plus y which is going to be 3
plus 3.4 6.4 and it's going to be a
float
because it's going to convert it out to
what it needs to
if i were to add to do two integers
so like i can also convert so if i do
that
now it's going to be an nth and if i do
because it's adding those two ends and
now we're going to see that it converted
it
to that 3.4 i converted it just to 3
and gets us an end and so really the
things you probably want to do is note
that
remember how to do x the scientific
notation
uh the rest of it you probably are good
with but note that for example booleans
are
case sensitive and
uh i'm you can find some stuff like
numbers in python
uh this little link to find just how to
get stuff together there are some
complex things that are out there
um so if i were to do
a equals 5x
oh it doesn't like that
uh oh because it's got to be a string
and then it's going to be
uh actually it's going to be a string so
that's interesting it doesn't like that
so if i do
type of 5x
it doesn't like that oh because it's got
to be j
i bet there we go
if you want to do a complex number
then you got to use j in there
uh but basically obviously you can use
imaginary
or j to make it one so it can actually
be
so if i do a 5j
done like that it does have to have it
yeah so it doesn't like that but you can
play around with your complex numbers
i'm not sure how often you're going to
deal with those
but again it's just really because it
does show up
on our uh our types
and of course uh strings
so you can either slap double quotes
or single quotes and you're going to be
a string note
that because you probably will see this
something like this on the test so if i
do a
equals 1 b equals
2 and then i do print well let's just do
a plus
yeah let's do print
a plus b
it's going to be 3. but
if i do uh a equals a string of one
and b equals the string two and then
i do print a print a there we go print
a plus b note it combine it concatenates
those strings
i can't do minus on string so that's at
least going to tell me i have to do
that it doesn't support minus for
strings whereas
before if i go back to a equals 1
and b equals 2 and their numbers
now it's going to be negative 1.
and if i i'm going to have an issue so
let's say i do a equals 1
b equals the string 2 and then
i print a plus b
in that case we're going to see here
that's going to tell us hey one's a
string and one's an integer that i don't
support that
that doesn't work so you're going to
have to cast those out and we have seen
that in some of the code we've used in
the past
so i just wanted to make sure i you know
brought that forward that those are some
of the types that we're working with
now another one that you're going to run
into i'm not going to go through the
whole list
but generally speaking is operator
precedence so you're going to see
things like if you are given i'm going
to jump over here real quick
so there's going to be things like if i
do x
equals 1 plus 3
times 5 minus 6
divided by 3
plus 9 times 6 that's in parentheses
they may say okay well what is x equal
to
and this is going to do basic uh what i
think is sort of standard precedence
so you start with parentheses and so
which that would be uh 9 times 6 is 54.
so let's
we'll back this up a little bit um we're
going to simplify this sort of as we go
so you start with the parentheses and
then you work your way into brackets
which makes sense because if you had
like
an array um so let's say y
equals uh there's some sort of array one
comma two comma three
and you wanted to get the item in
uh the first index or uh it could be the
you know three minus two index you wanna
you have to make sure that
obviously you evaluate that to get to
that
index so brackets are going to take
precedence
precedence if you get in here i don't
know that's going to run into it more
more often you're going to see something
like this so
then we do left to right and we do uh
multiplication and division
so we're going to see here so we got our
54 but then we've got
so we do one plus
uh and so it's gonna be three times five
is first so it's gonna do 15.
and then there's a minus 6 divided by 3
and then there's a plus so we get this
and then we just go left to right so
we're going to do
16 minus 2 plus 54
16 minus 2 is 14 plus
54 and so that's going to get us to 68
and if i do this up
and this way you can play around with it
in the interpreter
there we go so it's 68 and note
that i get this which is a sort of good
thing to note at times
because i did this division it turns it
into
a float if i didn't have that so let's
say
i take that same thing and i just do
instead of 6 divided by 3
i'm just going to make that
let's just say i'm going to just switch
it over so it's going to be 1 plus 1 so
that i don't have
well let's just do 1 times 2.
so now because i didn't have any
division in there and these are all
integers then it's going to be an
integer but if
a division is automatically going to
push it into a float
so some of those kinds of automatic
conversions
are very good to know and then
note that you're going to get down into
this is going to be pretty important
also as you get into we're going to talk
probably a little later we actually
definitely are going to talk a little
later
about some of our uh positives and
negatives
uh and bitwise kinds of shifts and ands
and ores which
becomes its own little thing uh is good
to know
probably not getting a lot get a lot of
questions on that
directly uh we will have a couple on
bitwise shifts i think and ands and ors
uh we will also get obviously go very
heavily into boolean
uh ands and knots and ores so we will
talk about that
as we go further along uh i will throw
this link out in the notes
because it's just a good one to look at
or you can you could probably google or
your favorite search engine python
operator precedence and you're going to
be able to see those there
that being said i think it's a good time
to wrap this one up
so we will get out there and get to it
you guys go out have a great day
great week and we will talk to you next
time
you
Transcript Segments
0.46

[Music]

26.16

well hello and welcome back

27.68

today we are going to talk about

30.72

as we work our way through our django

32.8

certification

33.92

we are going to talk about data types

36.8

particularly numeric types

38.8

and those are going to be things like

41.28

integers and floats and scientific

43.28

notation and

44

good stuff like that now going back to

47.44

our interpreter we can play around a

48.879

little bit

50.16

so if we do x equals 3

54.719

we can also do y equals 3.4 so three we

57.92

get an integer

58.8

no we can do a uh three point four we

61.68

can do

62.48

uh z equals true

66

and uh let's go to a equals

69.68

um to

72.88

and we're gonna do it uh scientific

74.56

notation

75.92

uh which is our other type which is a

79.439

um uh here we go

83.439

so scientific notation make sure i get

86.96

it right i don't use it very often so

88.799

it's an exponent oh

90.079

so if i do that to the like

93.2

uh ninth power and sure we'll do that i

96.32

could do to the negative

97.92

uh well let's do that so a equals that

100.72

and b

101.36

equals 3.14

105.119

did i get that right

109.36

i think i did that wrong but that's okay

111.04

so to the negative nine and that one is

114.96

actually to the ninth

116.32

i'm sorry so a little bit of typo there

120.24

so if i print a so you know this is um

123.92

scientific notation is just basically

126.32

this means times 10 to the ninth so it's

129.039

basically adding

131.039

nine decimals uh pushing it over to the

133.12

right adding

134.08

nine decimals to it so we get this huge

135.599

number uh if we use the smaller one

137.92

where

138.319

it goes the other way so negatives go to

140.239

the left positives go to the right

142.08

we're going to see that this is oh and

144

it's actually going to print it out it's

145.12

not going to do the

146.64

000. i think if we do

149.76

a equals like negative 4 let's see what

152.319

it does there

153.12

yeah so it's there's a certain point

154.4

where it's going to actually go to

155.36

scientific notation because

156.8

it's just a pain but if we look

159.84

for if we do x

163.68

y and z

167.2

we can see and note that true and false

169.04

are capital t or capital f

172.4

and we can actually do type so we can

174.239

see what these are so if i do type of a

176.8

that's a float even though oh

180

yes because it is scientific notation b

182.879

is also going to be a float

184.8

uh c if i do x that's going to be an int

187.92

because it's going to take the one that

188.72

makes the most sense

189.599

y it's going to be a float because it

191.2

has that decimal

192.72

and then if we do z we're going to see

194.48

if that is a boolean

197.44

and we can add these together so if we

199.12

do uh let's do

200.959

type of a plus b which is going to be

204.959

i'm going back here i'm sorry not a plus

207.04

b let's do x plus y

211.599

x plus y which is going to be 3

215.68

plus 3.4 6.4 and it's going to be a

219.04

float

220.48

because it's going to convert it out to

222.239

what it needs to

223.599

if i were to add to do two integers

226.959

so like i can also convert so if i do

228.959

that

233.519

now it's going to be an nth and if i do

236.879

because it's adding those two ends and

238.319

now we're going to see that it converted

240.64

it

241.04

to that 3.4 i converted it just to 3

245.519

and gets us an end and so really the

248.4

things you probably want to do is note

249.76

that

250.319

remember how to do x the scientific

253.04

notation

254.239

uh the rest of it you probably are good

256.239

with but note that for example booleans

258.799

are

259.519

case sensitive and

262.56

uh i'm you can find some stuff like

264.88

numbers in python

266.24

uh this little link to find just how to

269.28

get stuff together there are some

270.72

complex things that are out there

273.199

um so if i were to do

276.479

a equals 5x

282.32

oh it doesn't like that

288.4

uh oh because it's got to be a string

291.28

and then it's going to be

292.72

uh actually it's going to be a string so

293.919

that's interesting it doesn't like that

295.12

so if i do

297.36

type of 5x

301.44

it doesn't like that oh because it's got

303.919

to be j

305.039

i bet there we go

308.639

if you want to do a complex number

311.84

then you got to use j in there

316.479

uh but basically obviously you can use

318.24

imaginary

319.52

or j to make it one so it can actually

323.28

be

324.4

so if i do a 5j

328.24

done like that it does have to have it

333.12

yeah so it doesn't like that but you can

336.4

play around with your complex numbers

338.08

i'm not sure how often you're going to

339.28

deal with those

340.88

but again it's just really because it

343.12

does show up

344.08

on our uh our types

347.84

and of course uh strings

351.68

so you can either slap double quotes

356.56

or single quotes and you're going to be

358.24

a string note

360.08

that because you probably will see this

362.319

something like this on the test so if i

363.6

do a

364.16

equals 1 b equals

367.52

2 and then i do print well let's just do

371.199

a plus

371.84

yeah let's do print

375.039

a plus b

378.319

it's going to be 3. but

381.68

if i do uh a equals a string of one

386

and b equals the string two and then

389.919

i do print a print a there we go print

393.28

a plus b note it combine it concatenates

397.68

those strings

400.8

i can't do minus on string so that's at

403.6

least going to tell me i have to do

405.44

that it doesn't support minus for

406.88

strings whereas

409.199

before if i go back to a equals 1

413.199

and b equals 2 and their numbers

417.28

now it's going to be negative 1.

420.72

and if i i'm going to have an issue so

422.4

let's say i do a equals 1

425.039

b equals the string 2 and then

428.08

i print a plus b

433.36

in that case we're going to see here

434.72

that's going to tell us hey one's a

436.08

string and one's an integer that i don't

437.84

support that

439.28

that doesn't work so you're going to

440.319

have to cast those out and we have seen

441.919

that in some of the code we've used in

443.44

the past

444.72

so i just wanted to make sure i you know

446.479

brought that forward that those are some

448

of the types that we're working with

450.639

now another one that you're going to run

452

into i'm not going to go through the

454.16

whole list

455.12

but generally speaking is operator

457.44

precedence so you're going to see

459.52

things like if you are given i'm going

461.52

to jump over here real quick

464.72

so there's going to be things like if i

466.96

do x

467.759

equals 1 plus 3

470.879

times 5 minus 6

475.12

divided by 3

479.84

plus 9 times 6 that's in parentheses

485.039

they may say okay well what is x equal

487.039

to

488.639

and this is going to do basic uh what i

491.84

think is sort of standard precedence

493.919

so you start with parentheses and so

497.039

which that would be uh 9 times 6 is 54.

499.68

so let's

500.24

we'll back this up a little bit um we're

502.8

going to simplify this sort of as we go

506.639

so you start with the parentheses and

508.56

then you work your way into brackets

511.199

which makes sense because if you had

512.8

like

513.2

an array um so let's say y

516.719

equals uh there's some sort of array one

519.36

comma two comma three

521.12

and you wanted to get the item in

525.36

uh the first index or uh it could be the

529.279

you know three minus two index you wanna

531.44

you have to make sure that

532.48

obviously you evaluate that to get to

535.12

that

535.68

index so brackets are going to take

538.56

precedence

539.2

precedence if you get in here i don't

541.519

know that's going to run into it more

542.88

more often you're going to see something

543.839

like this so

546.32

then we do left to right and we do uh

548.48

multiplication and division

550.24

so we're going to see here so we got our

551.92

54 but then we've got

553.92

so we do one plus

557.2

uh and so it's gonna be three times five

559.04

is first so it's gonna do 15.

562.8

and then there's a minus 6 divided by 3

568.16

and then there's a plus so we get this

569.92

and then we just go left to right so

571.2

we're going to do

572.399

16 minus 2 plus 54

576.56

16 minus 2 is 14 plus

579.839

54 and so that's going to get us to 68

584.399

and if i do this up

588.56

and this way you can play around with it

590.56

in the interpreter

595.36

there we go so it's 68 and note

599.12

that i get this which is a sort of good

601.92

thing to note at times

603.279

because i did this division it turns it

606.32

into

608

a float if i didn't have that so let's

610.8

say

611.92

i take that same thing and i just do

615.76

instead of 6 divided by 3

619.6

i'm just going to make that

623.839

let's just say i'm going to just switch

625.44

it over so it's going to be 1 plus 1 so

627.279

that i don't have

628.88

well let's just do 1 times 2.

633.279

so now because i didn't have any

634.959

division in there and these are all

636.8

integers then it's going to be an

638

integer but if

638.8

a division is automatically going to

640.959

push it into a float

642.64

so some of those kinds of automatic

645.279

conversions

646

are very good to know and then

649.279

note that you're going to get down into

650.959

this is going to be pretty important

653.04

also as you get into we're going to talk

654.88

probably a little later we actually

656.32

definitely are going to talk a little

657.12

later

657.92

about some of our uh positives and

660.24

negatives

660.959

uh and bitwise kinds of shifts and ands

664.48

and ores which

665.839

becomes its own little thing uh is good

668.32

to know

668.88

probably not getting a lot get a lot of

671.12

questions on that

672.64

directly uh we will have a couple on

674.399

bitwise shifts i think and ands and ors

677.2

uh we will also get obviously go very

679.44

heavily into boolean

681.44

uh ands and knots and ores so we will

683.36

talk about that

684.48

as we go further along uh i will throw

686.32

this link out in the notes

688.56

because it's just a good one to look at

690

or you can you could probably google or

692.24

your favorite search engine python

694.8

operator precedence and you're going to

696.32

be able to see those there

698.32

that being said i think it's a good time

700.079

to wrap this one up

701.519

so we will get out there and get to it

704

you guys go out have a great day

705.6

great week and we will talk to you next

714.839

time

724.399

you