Instructor: Welcome back.
Let's talk about a important term
when it comes to programming,
and that is operator precedence.
Now, what does that mean?
Again, we're using our knowledge from math class.
It's the idea that if I do
20
plus
3
times
4,
it means that different operators
have precedents over different ones.
That is, we multiply this first, equals to 12,
and then 20 gets added to 12,
because the Python interpreter is going
to follow a basic set of rules.
For example, if I do 20 minus 3 here,
and let's print this out,
click run,
I get 8.
because 3 times 4 gets evaluated first,
which equals to 12, and then subtract it from 20.
It's not that this goes first
and then we multiply 17 by 4,
and this operator precedence shouldn't be new, right?
If you've taken any sort of math class
you understand that this is how, well, math works,
and this works the same in all programming languages.
So the order is this:
First, we follow the brackets.
So, whatever is wrapped in a bracket, we do first.
The second is we do the power of.
So whatever is done in the power of
will be after parentheses.
For example, if I do parentheses here
and then to the power of, let's say, 2
or let's do plus 2 to the power of 2,
well the first operation that will happen is 20 minus 3
because that's the first precedence,
and then we do 2 to the power of 2,
because that's the next precedence,
and then only after we do that, we add these two together.
If I click run,
I get 21.
Which again makes sense, 17 plus 4.
After the power of we have the multiplication and division.
And then finally the last precedence
is addition and subtraction.
Now, this isn't going to be a math class.
Some people really love math, some people really don't.
But I want to just give you this basic understanding
of operator precedence when you see it.
We'll do some exercises here
to get accustomed to it, but trust me,
this is as mathy as we're gonna get in this course.
The math part of the course is, well, just here.
It's gonna get a lot more interesting
as we learn about different data types.
I'll see you in the next one.
Bye bye.