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
[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