📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Bitwise Operators Part 2 and String Operators

2021-06-17 •Youtube

Detailed Notes

This episode looks into bitwise operators. We wrap up this two-part series of tutorials and move into string operators.

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/

Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries

Transcript Text
[Music]
welcome back
we are continuing working through our
series on certification in
python and we left off last episode
talking about
operators in particular we were looking
at some of the
bitwise operators we had looked at
basically the knot which is flipping the
bits
and we did a bit bit wise and
this time we're going to go ahead and
let's do a bit wise
or basically
say take the same ones and so or whereas
and is that ampersand or is just a pipe
so if we do it like this
and we'll do this
[Music]
now and says that when we're comparing
these
bytes these bits basically
the and says if it is if in a single
position it's whatever value it is so if
it's
you know this is zero and zero is zero
zero zero zero zero zero zero zero and
zero zero
zero and zeros one one and one is one
zero and zero is zero one and zero
is zero
the or is going to be an or there so
zero
or zero it's zero zero zero we go all
the way down
here to here one or one it's going to be
one
zero or zero zero and one or one
one or zero is one so this one's going
to give us
this number and
uh won't worry about the reversed order
and this one's gonna be
let's see so this one let's get down to
end so we get our zeros we get one
or zero so that's a one uh
and then the rest because they're all
ores then we're gonna get this
so if we
flip back over here
somewhere in here
all right let's just do this
python 3 operators bitwise
and so we're going to see down here
the bitwise or so 4 see we turn into
101 and here
we turn into that one
and then uh which is actually which is a
31 i'm sorry
that's the that's the bitwise version of
it
now the xor is similar to ore
and this one uses the i don't know what
you call it the hat
basically and so when we use the hat
then it is only if uh when you're
comparing the two that they are
an or that so you can't have both of
them the same
the only time you're going to go one to
come out true is if
one is one value and the other is the
other so for
doing four and five when we compare
those
uh the zeros are always going to be zero
uh one and if you've got a one and a one
then that's going to be
a zero
you've got a zero and a zero that's
going to be zero
one two three four five and if you have
a one or a zero
then it actually is an xor so that's
going to be the only one that you have
now this other one here so you have one
or zero so you get it there
um and then with the zeros and so that
one's going to be something like this
and you can see that
so here it just ends up being that one
at the end
and then when you do the 17 and the 15
notice that
the one place where because there's a
one finishing in both so that's going to
be a zero
and so you get all those where they were
alternatings
and then a zero so that's xor and this
is
again you probably have a question or
two but it's important to remember that
the and
the n and or should be easy enough to
remember because we're going to talk
about logicals as well so you have a
ampersand is your and pipe is your ore
and the hat is your xor which is
probably the toughest thing to remember
now shifting
these next two we're going to take a
little different
so what a shift does is if it's a
bitwise left shift
then what we're going to do is it's
going to be
let's do it this way the first number
the first is your bits
and the second one is how far you're
gonna shift it
so if i do a four shift left shift two
it's going to take that one and shift it
over two so i'm gonna end up getting
oh let's see
i probably should do this but
so that instead of that one being in the
third position that one is now going to
be
in the fifth position
one two three oh i need one more
[Music]
and so you can see actually so let's do
this um
if we take for example
let's take a five and we're going to
shift them one
two and three let's say in four
and so let's see how this guy looks and
first let's just do
a five
don't care about you okay so five let's
just do five
and we'll watch it sort of walk along
here
and so now the 17
uh it's going to be a little different
but that's a good one as well
[Music]
go away
see if i can do that um let's see so if
i'm gonna do
17 by two
[Music]
i don't care
all right let's do that let's get rid of
that maybe that'll leave me alone
so this one
is going to shift each of those
two so it'll be instead of zero zeros or
one
it's going to move that so here's where
we start
so we start here so it's going to turn
both of them too so this one here
goes to this spot and this one here
goes to this spot and then it's just
going to be zeros
and so let's do
same thing with the 17 except i do want
to play with it a little bit
so i'm going to show you what happens
when you get a little crazy so if we go
two
four six eight
and ten
and we're gonna see something similar we
go uh right shift but
for right now we're we're gonna do the
more complicated we're gonna do
uh spend more time here doing this shift
so if you look at the
left shift so 4 starts off
oh wait i'm sorry so yeah so 4 becomes
this but now this other one here this is
a five so notice that the five
we start left shifting it and go one two
three four five and it's just gonna keep
piling on the zeros
now let's take 17 out to
30.
oh let's do 60. uh let's do
let's do 62. let's see what that does
that'll give me what i want i think
well and what i'm looking at is if
okay come on
all right it's just going to keep on
shifting it so it's not going to
wrap at least at this point because our
numbers are so
big so it's just going to keep we can
see here when we go all the way out
it just keeps going all the way out but
you see here again
where you're going to start pushing
stuff along
and so if you did a
um let's pick a bigger number
so let's do like uh i don't know a 257
then again you're just going to see it
start marching to the left
so let's take the same thing
and go right
[Music]
and let's see
so if we take him instead to the right
then it's going to be this
now let's watch what happens when we
take the
these larger numbers
and start shifting them and so
this one let's watch what happens here
so our four we had that so this is our
five uh let's see so five
starts out 101
and then it starts right shifting and
it's just going to shift those
completely off
and you're basically going to have the
same thing here in the uh
256 oops stood this way so you can see
it when it starts
as you're going to see that it just
starts shifting and it just shifts right
off the number so if you start shifting
out it basically just drops off
now you may in some cases you may have
seen stuff where this would actually
where shift would actually
uh wrap back around you're not going to
see this in this shifts
so you're just basically moving those
things those
bits back and forth either to the left
or to the right
so now let's do
let's jump into our booleans
[Music]
and here we're going to be have we're
going to have and
or and not
and these are all going to be based off
of
boolean values which are going to be
true or false so we could do true
and true
true
let's just do each of these show true
and false
[Music]
and then false and false
so if i take that
let's go take a look at that one
[Music]
and so see here so true and true and
this we sort of saw before
and hopefully have run into this before
and
means that we have to have both of those
values
uh it's gonna be if both of those values
are the same
it's to be the value otherwise it's
going to be false so
true and true gives you true
but if it's true and it's false well
then it must be false
but if it's false and false then it must
be false so with an and the only way you
get a true is if all of the values are
true
and so this would even work
if i get a longer string so if i do true
and true
and false
so the trues outnumber the falses but
i'm still going to get
false because once i get a false it's
false
basically if there's anything in the
stream that is false
it's going to return false or if
everything is
it's going to return false so or
[Music]
let's do
let's do the same thing here and let's
switch it to
ores
this is going to be quite is sort of the
opposite of it well actually it's not
sort of it is the opposite of it because
this is going to be
[Music]
true
as soon as you find a true
where the oars i'm sorry with the and
it's false as soon as you find a false
or is it's true
as soon as you find a true so if we flip
back over here
and so we'll see here with the or so
true or true
we found it true right away so it's true
true or true or false we found it true
right away that's true
true or false we found the true right
away that's true false or false is false
and if we wanted to just change the
order
then it's not going to matter so if we
do false
or true or true
it's still going to be true so false are
true true as soon as we find a true
it comes back true and then the last one
is the not
and so let's actually
so it does it reverses a value
so true or true
so if i do true or let's just do
let's do first let's do not true
and then let's do false or
not true
or oh or not true
[Music]
and then let's do not true or false
and then false or not false let's do it
that way
let's see that's going to be oh
there we go not true false or
not true or not true
okay so let's see what this one does for
us so when we get to the not
so not true it's gonna be false
false or not true which is false or not
true which is false
it's false so that's false or false or
false and then not true or false
let's say not true or false so not true
it's false and you do or false and then
false or not false is true because it's
false or
not false which is true false or true is
true now you can also do this
uh let's see let's pick one like this
so we can also put some parentheses here
so what you're going to see so true or
true is true
not going to reverse it to false you're
going to have false or false and so it's
going to end up being
false so there you go false or not true
or true
so you can either do it directly to one
value or you can do it to
you can use parentheses to do it to a
whole series so i could even do
like this one
so this would be uh true or false
is true but not that it's going to be
false
[Music]
and so you're still going to see that um
there
there we go so those are
our uh bitwise and our boolean operators
we're gonna get more complex as we go
further into this but
that's a that's going to be our initial
start
uh there's some there are some more
complicated ones but
we will sort of jump into those
uh also a little further on um let me do
i do want to do before we wrap this one
up let me do string real quick so
there's two string operators
and i'm just going to do these in with
the numerix
and so we have the plus string operator
and we have the
star string operator
so plus is is going to be pretty simple
so if i do
uh the string a plus b
[Music]
then it's going to concatenate
and let's do the same thing
and do it as a star
and see what happens and so this
is the multiply because it has to be a
non-in so the second one has to be
an integer when we see the plus
so plus comes in a plus b just gives us
a b
but what we here get here is this is
actually a multiplier so if i do that
and it's gonna be a number
so if i'm gonna do five
then what we're going to see is it's
going to do multiplier so i get 5 8.
so if i wanted to do
a plus
b star five let's start three
then i get the three b's or i can do a
plus b and again
so now if i do a plus b and then stars
notice it's going to do a b a b a b
so there you go so we threw in our
string operator as well i think that
will
be a good stopping point for this time
around and then we will
uh step into the next couple of pieces
in our next episode
uh as always it's going to be out there
in github you can play around with it
there
ask any questions info
developernewer.com and
until next time go out there and have
yourself a great day
and a great week because we will talk to
you
next time
you
Transcript Segments
0.46

[Music]

26.32

welcome back

27.519

we are continuing working through our

29.76

series on certification in

31.679

python and we left off last episode

34.719

talking about

35.84

operators in particular we were looking

38.079

at some of the

39.04

bitwise operators we had looked at

43.2

basically the knot which is flipping the

45.2

bits

46.48

and we did a bit bit wise and

51.12

this time we're going to go ahead and

52.96

let's do a bit wise

54.719

or basically

58.239

say take the same ones and so or whereas

61.199

and is that ampersand or is just a pipe

64.72

so if we do it like this

70.479

and we'll do this

73.91

[Music]

75.84

now and says that when we're comparing

78.56

these

79.68

bytes these bits basically

82.72

the and says if it is if in a single

86

position it's whatever value it is so if

88.88

it's

89.6

you know this is zero and zero is zero

92

zero zero zero zero zero zero zero and

94.079

zero zero

95.439

zero and zeros one one and one is one

98.88

zero and zero is zero one and zero

102.32

is zero

105.52

the or is going to be an or there so

108.399

zero

108.799

or zero it's zero zero zero we go all

112.159

the way down

112.88

here to here one or one it's going to be

116.88

one

117.52

zero or zero zero and one or one

121.28

one or zero is one so this one's going

123.2

to give us

124.799

this number and

128.08

uh won't worry about the reversed order

130.319

and this one's gonna be

131.68

let's see so this one let's get down to

133.599

end so we get our zeros we get one

135.76

or zero so that's a one uh

138.8

and then the rest because they're all

140.84

ores then we're gonna get this

142.8

so if we

146.319

flip back over here

151.68

somewhere in here

154.879

all right let's just do this

157.92

python 3 operators bitwise

162.959

and so we're going to see down here

166.16

the bitwise or so 4 see we turn into

170

101 and here

173.519

we turn into that one

178.319

and then uh which is actually which is a

181.76

31 i'm sorry

182.879

that's the that's the bitwise version of

185.28

it

186.879

now the xor is similar to ore

193.44

and this one uses the i don't know what

196.159

you call it the hat

197.28

basically and so when we use the hat

205.04

then it is only if uh when you're

208

comparing the two that they are

209.84

an or that so you can't have both of

213.12

them the same

214.159

the only time you're going to go one to

215.92

come out true is if

217.519

one is one value and the other is the

219.2

other so for

220.64

doing four and five when we compare

222.72

those

223.84

uh the zeros are always going to be zero

227.599

uh one and if you've got a one and a one

230.48

then that's going to be

231.599

a zero

234.72

you've got a zero and a zero that's

236.64

going to be zero

238

one two three four five and if you have

240.4

a one or a zero

241.92

then it actually is an xor so that's

244

going to be the only one that you have

245.92

now this other one here so you have one

247.84

or zero so you get it there

250

um and then with the zeros and so that

253.36

one's going to be something like this

258.16

and you can see that

262.24

so here it just ends up being that one

264.639

at the end

266.72

and then when you do the 17 and the 15

269.44

notice that

270.32

the one place where because there's a

272.479

one finishing in both so that's going to

273.919

be a zero

275.04

and so you get all those where they were

277.68

alternatings

279.12

and then a zero so that's xor and this

282.479

is

284.08

again you probably have a question or

285.44

two but it's important to remember that

287.12

the and

287.68

the n and or should be easy enough to

290.16

remember because we're going to talk

291.28

about logicals as well so you have a

292.96

ampersand is your and pipe is your ore

296.08

and the hat is your xor which is

298.24

probably the toughest thing to remember

300.479

now shifting

304.8

these next two we're going to take a

306.639

little different

308.24

so what a shift does is if it's a

311.36

bitwise left shift

313.039

then what we're going to do is it's

314.88

going to be

317.84

let's do it this way the first number

320.56

the first is your bits

322.16

and the second one is how far you're

324

gonna shift it

325.199

so if i do a four shift left shift two

328.72

it's going to take that one and shift it

330.639

over two so i'm gonna end up getting

335.44

oh let's see

341.36

i probably should do this but

350.8

so that instead of that one being in the

353.039

third position that one is now going to

354.72

be

355.039

in the fifth position

358.16

one two three oh i need one more

360.08

[Music]

362.8

and so you can see actually so let's do

365.039

this um

368.56

if we take for example

373.28

let's take a five and we're going to

375.919

shift them one

381.44

two and three let's say in four

385.919

and so let's see how this guy looks and

387.84

first let's just do

389.199

a five

393.28

don't care about you okay so five let's

396.4

just do five

397.68

and we'll watch it sort of walk along

399.68

here

405.52

and so now the 17

409.599

uh it's going to be a little different

410.96

but that's a good one as well

412.93

[Music]

415.759

go away

419.68

see if i can do that um let's see so if

423.039

i'm gonna do

423.759

17 by two

427.18

[Music]

430.319

i don't care

437.12

all right let's do that let's get rid of

438.96

that maybe that'll leave me alone

441.36

so this one

446.319

is going to shift each of those

450.16

two so it'll be instead of zero zeros or

453.12

one

453.52

it's going to move that so here's where

455.68

we start

458

so we start here so it's going to turn

459.28

both of them too so this one here

461.84

goes to this spot and this one here

465.52

goes to this spot and then it's just

467.759

going to be zeros

472.96

and so let's do

476.96

same thing with the 17 except i do want

480.319

to play with it a little bit

481.84

so i'm going to show you what happens

483.36

when you get a little crazy so if we go

485.039

two

491.039

four six eight

497.919

and ten

502.8

and we're gonna see something similar we

504

go uh right shift but

506

for right now we're we're gonna do the

509.28

more complicated we're gonna do

510.96

uh spend more time here doing this shift

514.08

so if you look at the

517.919

left shift so 4 starts off

527.279

oh wait i'm sorry so yeah so 4 becomes

531.04

this but now this other one here this is

533.76

a five so notice that the five

536

we start left shifting it and go one two

539.2

three four five and it's just gonna keep

542.16

piling on the zeros

545.92

now let's take 17 out to

552.16

30.

560.959

oh let's do 60. uh let's do

568.08

let's do 62. let's see what that does

570.32

that'll give me what i want i think

575.04

well and what i'm looking at is if

583.2

okay come on

587.04

all right it's just going to keep on

588.16

shifting it so it's not going to

591.36

wrap at least at this point because our

593.44

numbers are so

594.8

big so it's just going to keep we can

597.519

see here when we go all the way out

598.959

it just keeps going all the way out but

601.76

you see here again

603.04

where you're going to start pushing

604.72

stuff along

606.079

and so if you did a

610.48

um let's pick a bigger number

614.72

so let's do like uh i don't know a 257

623.76

then again you're just going to see it

625.12

start marching to the left

629.12

so let's take the same thing

632.959

and go right

636.8

[Music]

641.36

and let's see

646.24

so if we take him instead to the right

649.12

then it's going to be this

653.839

now let's watch what happens when we

655.6

take the

664.16

these larger numbers

667.6

and start shifting them and so

674.32

this one let's watch what happens here

681.279

so our four we had that so this is our

684.399

five uh let's see so five

688.56

starts out 101

692.079

and then it starts right shifting and

694

it's just going to shift those

695.12

completely off

697.2

and you're basically going to have the

698.24

same thing here in the uh

700.839

256 oops stood this way so you can see

704.8

it when it starts

710

as you're going to see that it just

711.36

starts shifting and it just shifts right

713.839

off the number so if you start shifting

715.6

out it basically just drops off

718.72

now you may in some cases you may have

720.16

seen stuff where this would actually

721.2

where shift would actually

722.8

uh wrap back around you're not going to

724.72

see this in this shifts

726.24

so you're just basically moving those

728.56

things those

729.6

bits back and forth either to the left

732.16

or to the right

736

so now let's do

740.8

let's jump into our booleans

745.77

[Music]

756.16

and here we're going to be have we're

757.519

going to have and

759.279

or and not

766.88

and these are all going to be based off

768.639

of

771.44

boolean values which are going to be

772.959

true or false so we could do true

776.32

and true

785.68

true

788.72

let's just do each of these show true

790.959

and false

796.51

[Music]

798.48

and then false and false

814.16

so if i take that

818.88

let's go take a look at that one

823.1

[Music]

825.519

and so see here so true and true and

827.76

this we sort of saw before

829.839

and hopefully have run into this before

832.56

and

833.76

means that we have to have both of those

835.68

values

836.8

uh it's gonna be if both of those values

839.12

are the same

840.72

it's to be the value otherwise it's

842.48

going to be false so

843.839

true and true gives you true

847.36

but if it's true and it's false well

849.68

then it must be false

850.959

but if it's false and false then it must

852.959

be false so with an and the only way you

854.72

get a true is if all of the values are

856.959

true

857.76

and so this would even work

862.079

if i get a longer string so if i do true

865.04

and true

865.76

and false

872.8

so the trues outnumber the falses but

874.959

i'm still going to get

877.12

false because once i get a false it's

878.959

false

880.72

basically if there's anything in the

882.56

stream that is false

884.079

it's going to return false or if

886.079

everything is

887.199

it's going to return false so or

890.78

[Music]

891.839

let's do

900.24

let's do the same thing here and let's

902.48

switch it to

903.92

ores

910

this is going to be quite is sort of the

911.839

opposite of it well actually it's not

913.36

sort of it is the opposite of it because

915.6

this is going to be

919.19

[Music]

920.48

true

923.839

as soon as you find a true

929.04

where the oars i'm sorry with the and

932.639

it's false as soon as you find a false

935.04

or is it's true

936.16

as soon as you find a true so if we flip

939.68

back over here

941.12

and so we'll see here with the or so

943.519

true or true

944.639

we found it true right away so it's true

946.24

true or true or false we found it true

947.759

right away that's true

949.12

true or false we found the true right

950.639

away that's true false or false is false

953.12

and if we wanted to just change the

954.72

order

956.8

then it's not going to matter so if we

958.16

do false

960.639

or true or true

970.72

it's still going to be true so false are

972.16

true true as soon as we find a true

973.92

it comes back true and then the last one

978.72

is the not

986.24

and so let's actually

990.32

so it does it reverses a value

994

so true or true

997.6

so if i do true or let's just do

1002.079

let's do first let's do not true

1008.56

and then let's do false or

1011.92

not true

1015.12

or oh or not true

1019.43

[Music]

1022.16

and then let's do not true or false

1026.88

and then false or not false let's do it

1029.52

that way

1035.12

let's see that's going to be oh

1038.559

there we go not true false or

1042.24

not true or not true

1045.919

okay so let's see what this one does for

1048.24

us so when we get to the not

1049.76

so not true it's gonna be false

1052.799

false or not true which is false or not

1055.76

true which is false

1057.12

it's false so that's false or false or

1058.799

false and then not true or false

1061.679

let's say not true or false so not true

1065.28

it's false and you do or false and then

1069.2

false or not false is true because it's

1071.52

false or

1072.32

not false which is true false or true is

1074.24

true now you can also do this

1077.76

uh let's see let's pick one like this

1089.2

so we can also put some parentheses here

1101.039

so what you're going to see so true or

1102.64

true is true

1104.64

not going to reverse it to false you're

1106

going to have false or false and so it's

1107.6

going to end up being

1108.559

false so there you go false or not true

1111.039

or true

1111.84

so you can either do it directly to one

1113.52

value or you can do it to

1115.679

you can use parentheses to do it to a

1117.2

whole series so i could even do

1121.2

like this one

1124.559

so this would be uh true or false

1128

is true but not that it's going to be

1129.679

false

1131.33

[Music]

1132.799

and so you're still going to see that um

1135.6

there

1141.44

there we go so those are

1144.559

our uh bitwise and our boolean operators

1148.32

we're gonna get more complex as we go

1150.88

further into this but

1152.24

that's a that's going to be our initial

1154.24

start

1156.08

uh there's some there are some more

1157.76

complicated ones but

1159.12

we will sort of jump into those

1162.32

uh also a little further on um let me do

1165.76

i do want to do before we wrap this one

1169.12

up let me do string real quick so

1172.32

there's two string operators

1177.039

and i'm just going to do these in with

1178.48

the numerix

1181.84

and so we have the plus string operator

1185.76

and we have the

1188.799

star string operator

1192.559

so plus is is going to be pretty simple

1202.4

so if i do

1206.48

uh the string a plus b

1209.8

[Music]

1213.2

then it's going to concatenate

1216.799

and let's do the same thing

1223.039

and do it as a star

1226.32

and see what happens and so this

1229.36

is the multiply because it has to be a

1231.2

non-in so the second one has to be

1233.52

an integer when we see the plus

1236.559

so plus comes in a plus b just gives us

1239.28

a b

1240.08

but what we here get here is this is

1241.679

actually a multiplier so if i do that

1243.36

and it's gonna be a number

1244.24

so if i'm gonna do five

1249.84

then what we're going to see is it's

1252.08

going to do multiplier so i get 5 8.

1255.679

so if i wanted to do

1261.919

a plus

1266.48

b star five let's start three

1271.2

then i get the three b's or i can do a

1274.72

plus b and again

1280.48

so now if i do a plus b and then stars

1283.28

notice it's going to do a b a b a b

1287.2

so there you go so we threw in our

1288.96

string operator as well i think that

1290.559

will

1290.88

be a good stopping point for this time

1292.96

around and then we will

1294.64

uh step into the next couple of pieces

1297.28

in our next episode

1298.799

uh as always it's going to be out there

1300.4

in github you can play around with it

1301.76

there

1302.159

ask any questions info

1303.48

developernewer.com and

1306.48

until next time go out there and have

1308

yourself a great day

1309.919

and a great week because we will talk to

1313.12

you

1313.76

next time

1329.6

you