- [Craig] In this short video, we're briefly going to go over
some of the common arithmetic and comparison operators you need to know when programming.
(uplifting piano jingle)
So before we dive in, let's just think about what the arithmetic logic unit performs.
It performs arithmetic operation, it performs logical comparison,
and it also performs binary shifts.
And because of this obviously, there's a number of things you expect it to be able to do.
You expect to be able to perform addition and subtraction,
you expect it to be able to compare if one item is greater than or less than the other.
And these type of actions performed by the ALU are supported by
operators that you can use in all common programming languages.
Let's take a look at some of them now.
So we'll start with the common arithmetic operators.
Well there's ones you'll be very familiar with from maths.
There's the plus symbol.
For example, x equals seven plus two.
So that's addition.
There's the minus or subtraction operator,
x equals seven minus two.
There's the multiplication operator.
Now notice here we use the star or asterisk symbol
and not the letter X on the keyboard.
So x equals seven times two.
And of course to round off, there's the division symbol.
So for example,
x equals 7 divided by 2, 3.5.
We then have exponentiation.
So x equals seven to the power of two.
So that will be 49.
Now the next two arithmetic operators are ones which candidates often get confused with.
The first is known as modulus.
Now in Python, modulus is represented with the per cent symbol.
So when we see seven per cent two,
it means seven modulus two.
How many whole times can we fit the number on the right, two, into the number on the left?
We can see here, I can fit it in three whole times,
what's left over is one so that's what modulus returns.
It returns one, it returns what's left over.
So going alongside modulus is an arithmetic operator called integer division.
Now unlike normal division,
we represent this with two backslashes.
So when you see seven backslash backslash two,
it means seven integer divided by two.
So let's have a look how that works.
So as we can see from the previous example,
the number on the right, two, fits three whole times into the number on the left.
So seven DIV two is three.
Now that's important.
Integer division is very different from normal division.
With normal division,
seven divided by two is three and a half.
With integer division, so using whole numbers,
seven DIV two is three.
There are also common comparison operators.
So we have equals.
So is one number equal to another number?
Is this value equal to this value?
And you'll notice in your exams and in most programming languages,
the equals operator is a double equals,
and that's because the single equals is typically used for assignment.
The opposite to that is not equals to.
So is the value on the left not equal to the value on the right?
Now there are certainly different ways of showing this in programming languages.
For your exam, not equals to is an exclamation mark followed by an equals.
We then have the less than symbol, and you'll be used to this from maths.
And we have the less than or equals symbol.
On the flip side, we have the greater than symbol,
and we have the greater than or equals symbol.
Candidates also can trip up here in exams
when what they mean to use is greater than or equals to,
but they accidentally forget and just use greater to.
So just be careful here that you're using exactly the correct comparison operator
when you're constructing statements.
(uplifting piano jingle)