9. Python operators
Oct 8, 2023
Operators in python can be Arithmetic, Assignment, Comparison, Logical, Identity, Membership, and Bitwise. In this video we go over the syntax for some of these operations.
All right, we've gone through, we've introduced most of the interesting types of variables
and data types that we're going to be using in Python.
Now let's talk about operators.
Operators are things that you can use on these variables and their values to modify them
or change them or do math or learn from them in some way, right?
So there's lots of different types of operators that we could be talking about.
There's arithmetic, right?
There's assignment, there's comparison, logical, identity, membership, and bitwise operators.
We're going to go through a few of these now.
We're going to go through these now in sequence, right?
Let's start first with the arithmetic operators.
If you've got variables, you know, x and y, you can add them together, subtract, multiply,
divide, take the modulus, exponentiation, or floor division.
Let's show these a little bit.
So let's start by declaring something, right?
So let's make a variable.
We'll call it just x equals 5.
Now when we first run this, what's that going to be?
It's going to be an integer type, okay?
So let's make another one that's a float.
Let's say that y is equal to 2.5, okay?
When we run that, we know that it's a float.
So let's try and do some math with these things.
Let's say that z is equal to x plus y.
When we run that, what will the integer type, what will the variable type be?
Well, you're adding 5 plus 2.5, so it's going to be 7.5.
That better be a float, okay?
Same thing if we multiply it, right?
So if you take an integer and do, you know, a lot of these math operations with them,
it's going to turn it into a float if your other variable's a float, right?
So the sums can be multiplied, so we're going to do a little star for multiplication.
We run that, and now z is 12.5, it's a float, okay?
We can do complex numbers, right?
Let's take, we'll call this numComplex, right?
And this is going to be equal to, let's say, 1 plus 3j, right?
By adding that second term that has the j in it, it's going to know that that's a complex
number, right?
So it knows that's a complex number where you have the real and the imaginary component
to it.
If we take this complex number, right, so num underscore complex, and we add it to something
else, like x, right?
So now we're saying that z is equal to a complex number plus a real number.
What's that going to do to z?
z used to be a float, but when we run it now, now z is going to be a complex number, right?
Where it's added, it's added those two things, okay?
So what else can we say about these?
Multiplication, division, those are pretty straight forward.
What about modulus?
What does modulus do?
Well, let's try it.
Let's take 5 divided by 3, right, let's take that.
So let's change our y value to 3 up here, and now for our modulus, we're going to have
z is equal to x, what's the symbol here?
We're going to do percent sign y, okay?
When we run that now, z is going to be an integer of 2.
So what is that?
5 divided by 3, what modulus is, is it's the leftover, right?
The remnant, right?
When you take the division of something, it's what's left over between a whole number.
So you can do 3, and then there's 2 left over when you divide it.
So that's what that is.
There's floor division, which will tell you how many whole numbers can divide into something.
So it ignores the remnant and tells you just how many numbers.
Let's try that.
So z is now going to be equal to x, the nomenclature is 2 slashes, slash slash y.
So let's try that.
It's going to be 1, right?
Because how many times does 3 go into 5?
1, and then you have 2 as your remnant.
So that's the difference between modulus and this floor division.
And then if you want to raise something to the exponent, you do 2 of these stars, right?
So if we wanted to raise 5 and cube it, right?
So we would say that z equals x star star y.
We run that.
Now it's going to be cubed, so it's 125, 5 times 5 times 5, okay?
So those are your arithmetic operators.
And the key is, yeah, remember that the type will change.
If you use something like complex and you do math with anything else, that new thing
is going to turn into a complex and float similarly most of the time with integers.
What else can we say?
There's these cool assignment operators.
In other languages, like C++, the reason we call C++ is that in a loop, which we'll talk
about soon, each time you go through, maybe you want to increment the size of your variable.
If it was 1 the first time, when you go through the loop again, it should become 2.
So they have a code called C++ to do that, right?
There's a shorthand for doing that.
We don't have that in Python.
Instead, we have this one here.
So first off, there's the equal sign.
That's straightforward.
If you want to make a variable equal something, you just use the equal sign.
But then there's this, this plus equal.
So if you have x plus equals 3, what it does is it takes the previous x value, adds it
3, and equals that to the new x.
So we would say like x plus equals 5.
So currently, or let's do 4, currently x starts out as 5, so it's going to take that previous
value of 5, add 4 to it.
So this should be 9 by the time we run this code.
X should now be, and it is, it's now 9.
So that's the plus equals.
So instead of having C++, what you would do is that.
Every time you run it through, x is just going to increment by 1.
It went from 5, where we started, and now it's up to 6 over here.
There's other things though.
There's minus equals, which now you're going to go down by 1 unit.
There's multiply equals, which you're going to take the previous and multiply it by that
number.
So this is all the same stuff as before, it's just acting it on itself, and then reassigning
it to itself.
So that's all pretty straightforward.
What about this one?
These are Python comparison operators, right?
Lots of times in coding, we need to make a comparison.
Is this thing bigger than that?
Is it equal to that?
Is it greater than, whatever?
So we can do that with these here.
For example, let's come down here in our console, and let's ask, is x equal to y?
Well, right now, stored in memory, it's 6 and 3, so that better come back false.
And it does.
It comes back false.
However, we could ask, is x greater than, what's our symbol here?
Greater than y, and it's going to come back true.
Is y greater than x?
It's going to come back false.
What else can we do?
How about not equal to?
You're asking, is x not equal to y?
So we do the exclamation point, equal y, and it's going to say true.
Of course, they're not equal to each other.
And then you've got greater than or equal to.
You just include the equal sign afterwards.
So is x greater than or equal to y?
It's going to say no.
But what if we said that x was equal to 3?
We modify it in the memory.
Now let's try that.
x, is it greater than or equal to y?
Now it's going to say true.
So those are your comparison operators.
We're going to need those once we get to logic.
There's other logical operators, and, or, and not.
So for example, this one says it's going to return true if both statements are true.
For example, if x is less than 5, and x is less than 10, it will return true.
So let's try this.
So x is less than y, that's not true.
And x is greater than 1.
So it is greater than 1, but it's not less than y.
It's equal to y, so this is going to come back false.
So you can combine these things together.
You could do or, though.
How about this one?
Let's do the exact same thing, but instead of and, let's do an or statement.
So this time it's saying, is x less than y?
We know that it's not, because they're equal to one another.
Or is x greater than 1?
It is greater than 1, so this is going to come back true.
And then you can have the opposite, not.
So it's going to reverse the result.
It returns false if the result is true.
So we could say, not x is not equal to y.
So we know that x is equal to y.
So if it's saying that it's not, this should come back as true.
So these are all the different logical tools that you can use
when you're building your arguments in your code.
Which, again, we'll get to soon.
We'll actually start using these things.
There's these interesting identity operators.
We've been using them a little bit.
So you can say, is something, or is it not something?
So right now, let's do x is y.
Currently, that should return true, because they're equal to one another.
And so we could say, x is not y, and it's going to come back false.
Because they are, in fact, currently equal to each other.
But we could say, y is not z, and it's going to come back true,
because 3 is not the same as 125.
There's membership operators, where you can say,
is something present in an object?
So we have individual values right now.
But let's add a list. Let's create a list over here.
So let's say that our new list is going to be equal to,
again, we're using square brackets,
and we'll do cat, let's do dog, and we'll do bird.
So now let's run that.
So we have this new list, which has those three things in it.
We can come down here and we can ask, right?
So in, right?
So let's say, fish in new list, right?
And it's going to say no, that's not in there.
But if we modified that and included something that we know is in it,
like cat, let's run that again, and now it's going to say true.
So these are interesting, they call them membership operators.
And you can do, again, the opposite, not in.
It will tell you true if it's not present in that object.
And then you can do operators also at the bit level,
but I don't think we're going to bother with that.
So these are the operators we can use.
You can see how they interact with the variables that we've created.
Okay, let's move on.

