Hello guys. Welcome to Learnvern. We saw variables in the last topic...we saw how to work with variables.
We will see how to work with Operators.
To begin with, we should get to know about Operators.
First things first, what is Operator? Operators are used to perform operations.
And they are used on variables and values in Python.
If you want to use any operation such as addition, greater than or less than operation, you will apply it on that specific variable or value.
There are various types of Operators...such as Arithmetic operators, Assignment operators, Comparison operators, Logical operators, Identity operators & Membership operators.
We will begin with Arithmetic operators.
Arithmetic Operator is used to perform all the mathematical operations.
Such as addition, subtraction, multiplication...all of these operations can be done through Arithmetic Operators.
On the same path is Assignment Operators.
We use the symbol of = (equal to) to assign the value to the variables.
That means we assigned them...okay
This is what it means when we say we assigned the value to the variable.
That comes under Assignment operators.
Such as shorthand operators, plus, equal to...you added a value by using + and then store it under the same variable, shorthand operators are used for that.
All of these are called Assignment Operators.
Next up is Comparison Operators.
Comparison Operators help us in decision making.
Decision making means it will give you an answer only in 2 ways, either True or False.
Less than, greater than, greater than or equal to…all of these are comparison operators. Alright.
Now we will talk about Logical Operators. What does Logical Operators mean?
If there is a specific condition, suppose the condition is True…
You have to use 2 conditions at a time and check if 1 is true from both, then we will use Logical operators.
There are Logical operators such as And, Or, Not in Python.
In the same way, there are Identity Operators.
What Identity Operators do?
Identity Operators check whether one object matches another object or not.
Even Identity Operators will give only and only 2 answers...that is True or False.
Now comes Membership Operators.
Membership operators check your particular sequence.
Suppose I have a sequence & I want to check if that word is available or not, you can use Membership Operators for that.
This was theoretical part but I will show you practically as well to make the picture about Operators easier & clearer for you.
Let’s move on to the practical part.
I have opened Jupyter. We will start with Arithmetic Operators.
Which operators? Arithmetic operators.
I’ll write down a comment for Arithmetic operators.
I'll increase the size to help us see clearly.
I wrote down Arithmetic Operators here.
I’ll first show you addition operators from Arithmetic operators.
How to use addition operators.
For instance I have a variable X under which I passed the value of 10.
Another variable Y under which the value is 20.
Then we have Z beside which I wrote X + Y.
This plus is our addition operator. What is our addition operator? + sign.
I’ll write down the message “answer” here and further print Z here.
It will give answer 30 here.
Our addition operation will work like this.
Now suppose I want to apply subtraction under Arithmetic…
I’ll write subtraction. Subtraction operator.
Subtraction operator is used when you want to deduct anything.
Suppose I have 2 variables and we want to subtract them.
Suppose I take a variable X. And its value is 30.
And Y. Y’s value will be 10. Okay? I took 10 here.
Next I took the Z variable where I want to use the operator.
So I’ll type X - Y (minus).
Now I’ll get my answer printed. How?
Input Z here. As soon as we clicked on run, it gave me an answer after subtraction.
Which means you can use subtraction in this way as well.
Now suppose I have another operator such as multiplication.
Which operator? Multiplication. I’ll write down the operator as well.
Now I have the value of X, 10. Y is 2.
I want the operation to be X multiplied by Y.
So I’ll write Z as a variable under which I’ll perform this multiplication operation.
So what will we do? X multiplied by Y.
And get our answer printed here, which is Z.
It will give us the answer 20.
So you can use Arithmetic operators like this.
Clear?
You must have understood all of this more during practicals because in theory, we only saw how the operators are supposed to be used.
But now that I’ve shown you practically, you were able to understand it more easily.
These Arithmetic operators will be used much more in future in Python. Okay?
Moving further ahead…
Our next operator is the Assignment operator.
Suppose we want to use = (equal to) in the Assignment Operator.
We will use the Equal to operator first.
How to use equal to operator? Suppose we want to assign a value into a variable.
For example I took this variable X = (equal to)
This equal to sign we used is the sign of our Assignment Operator.
Now if we print the value of the value of X, then what will it do?
It will assign the value of 5 under X. And then when we run it, it will be printed here.
What happened here is, the value we entered of 5, got assigned to the X variable.
= (equal to) is our Assignment Operator.
Moving forward to other operations under Assignment Operations.
Suppose I want to use Addition and Equal to operation.
So understand how to move ahead next…
Suppose I have a value of X, 5. And I want to add 5 more as well as assign the value of X as well.
How will I do it? There is a short, simple & easy way.
Write down X += 5. How will the calculation work?
The value of X is equal to X + 5.
This is how the equation ends with shorthand operators.
Now when I print X...the value of X was 5 until now but the value should be 10 now.
The 5 we took here, got added into the equation and the addition came out 10.
So this is how the addition equal to equation works.
Next up is subtraction which will work in the same manner.
Subtraction & equal to operator.
How will we do it?
Suppose I have the value of 6 in X.
I’ve already assigned the value of 6.
Now I have to subtract 2 from 6 as well as assign that value to X.
How will we write? X - = (minus equal to) 2.
How is the equation formed? The value of X will remain. X =.
And it will be X - 2. This is our equation.
Now when we print, the value of X should be 4.
Let’s run it & see.
The value of X is 4.
So this is how Assignment operators work in Python.
Next up is the Comparison operator.
Which operator? Comparison operator.
The first & foremost Comparison operator is Equal to. Equal to, Equal to.
Equal to, Equal to. So what does this operator do?
This operator does the Comparison.
It compares if the first variable matches the second’s value or not.
Let’s see.
Suppose X has the value of 5 & Y has 3.
X and Y both have different values. Now I have to check it.
How will I check with the Comparison operation?
I’ll simply write X == (equal to equal to) Y.
If it is not...see the values of X & Y are not matching right now.
So the answer was False.
Comparison operator will only result in 2 answers either False or True.
Now suppose I changed the value of Y to 5.
When I click on Run, the result will also change to True.
So this is how the Equal to Equal to Comparison works.
Next operator we will check under Comparison is, Not Equal to.
Which one? Not Equal to. Not Equal to operation.
How will we use Not Equal to here? Let’s see.
I’ll take X = 5 here & Y = 3.
Both of their values are not the same. But only we know that, not the system.
To let the system know, we will use the Not Equal to operation.
Let’s see how to use it.
Print and use an exclamatory sign for Not.
Which sign? Exclamatory sign.
Let’s check it.
Exclamatory for Not and =. Meaning, it will neither match nor become equal to.
Let’s see if it is happening or not.
We will write here Not equal to Y and run it. The result is True.
Meaning X and Y does not match that is why it resulted in True.
Our next operator works in the same way that is greater than equal to.
Let’s see how to check them.
Greater than Equal to.
We will take X 5. We will take Y 3.
The value of X should be more than Y or the same as Y.
Let’s see how to do that.
We will Print. X >= Y.
This is the sign of greater than. Greater than equal to Y.
We will run it. Yes, it is True.
Now if I change it to less than. Right now it is greater than.
Which means Y’s value does not match X. The value of X is greater than Y.
Which is true as shown in the result.
So this is how Comparison operators work.
Let’s go with the Logical operator. How does a Logical operator work?
Let’s see ahead. We will write down Logical Operators.
But what does Logical operator mean? They should be logically true or false.
First operator I’ll take is And operator. Which operator? And operator.
Let’s see how to use them.
Suppose I took X = 5. I took X = 5. Initialising a value already.
Now I’ll print… I want to check 2 conditions. Which? That is X greater than 3 or not.
And another condition is, X is less than 3.
Now I will run it. The answer is False. Why?
Because here, 1 condition is true but the other one is false.
That the value of X is indeed greater than 3 but it is not less than 3 so it returned the answer as false.
If both the conditions had been true, then it would have returned with the answer true.
Now if we use another operator such as Or operator. Or operator.
Let’s see how the Or operator works.
What was the concept of And? You had to match the two conditions.
In Or, you have an option of either this should match or that.
Any 1 condition should be true from both.
Let’s see. X = 5. I’ll print it as X > 5. Or X < 3.
We checked this, right?
Let’s see what the answer is.
It’s False. Why so?
We made a mistake. We should have taken 3 here. Now it’s True.
Here, 1 condition is True. How? Let’s see.
We will move forward & work with Identity operators.
How do Identity operators work?
I’ll give a heading here Identity Operator.
What does Identity operator mean? That too will answer in True or False.
We have to check according to the objects.
Whether our 1 object matches with the other object or not, how does it work etc etc.
I’ll show you practically…
Our first operator is Is operator.
Which operator? Is operator.
I have made a list under Is operator where I wrote Apple. And Mango.
I’ve kept Mango as it is the season of Mango. Okay?
And Y =.. I’ll write Apple and Mango here too.
I have made two separate lists here.
I’ll take one more variable, Z = X.
What does Z = X mean? That means my values of X are the same as Z.
Now see, I’ll just print Z. The values of Z should be the same as X.
Let’s see.
As you can see, Z’s value came ditto as X. right?
Now I want to check....I had done this just to show you.
Now we have to check whether X is Y.
We will check it now.
The answer will be False. Why False?
I’ll tell you...first of all the value of X is the same as Y.
Meaning X’s value is matching Y’s value.
But X is not equal to Y. Which further means that X as an object is not matching with Y.
Only the value is matching. Right?
I’ll write a comment here saying “the object is not matching that’s why it gives us False”
Now I’ll check it in the same way again...whether X is Z?
Meaning X is like Z or not?
We will check it now.
We will run it & it showed us True. Why?
Because we had written Z = X. Because we had written Z = X.
Here, the object is matching, that's why it returned True.
So this is how the Identity operator works in Python.
Our next operator is Membership operator.
We will check that as well.
Membership operator.
There are 2 types of Membership operators. 1 is In & 2 is Not In.
Let’s see how to check them.
I’ll take a variable X here. In which I wrote Apple and Mango.
We will check it now by Print. Banana in X.
Run it & the answer is False. Why False? Because our list has only Apple & Mango.
Not Banana. That is why the answer is False.
So this is how it is used with Membership Operators.
Now how to use Not In? Let’s see.
I’ll take another print here only. I’ll write Grapes here.
And further write Not In X. It will return us with True as Grapes are not in X.
It has shown us true because Grapes is not in X. That is why it showed us True.
Operator Precedence. What does Operator Precedence mean?
Which operator evaluates first can be confusing. So we have some rules for this too.
Operator Precedence means which operator will be executed first.
Meaning what orders will get executed first, which operator will be called first.
All of this is called Operator Precedence.
Which works left to right. I’ll explain Operator Precedence to you through a table.
Suppose I have a particular operator.
For instance, I’ve kept a table here for Operator Precedence where I have entered Operators from highest to lowest.
First comes Exponentiation which is the power of the value, is executed.
Then multiplication. Followed by others.
In this way, the execution starts taking place.
Meaning the operator who is the first priority will get executed first & its value is to be taken first & then other operators will be executed.
That is called Operator Precedence.
The operator who is the first priority will get executed first.
That operator is given the first priority.
I’ll show you a practical to make it easier for you to understand, okay?
I have a value of 1+1 in a bracket, multiplied by 2…
2’s power. This is called power of 4.
And then flow division...then 3 addition 4 and -1.
I have this expression which I’ve stored under value.
Now first of all, which operator...all of these are operators which are used here.
All the addition, multiplication, brackets..all of them are operators which are used under this expression.
Now let’s start solving them.
Which operator has been given the first priority?
The brackets. Meaning the values inside the bracket will get solved at first.
How will it get solved? Let’s see.
This 1+1 under this bracket will come here... see…
Means it will get added first, right?
That means 2 multiplied by 2, power of 2 is 4. Then flow division of 3 + 4 - 1.
Which further means that I solved only the brackets value that is the parenthesis’ value which resulted in 2.
I wrote the remaining value in the same way as before.
I hope you understood why I took that step.
Now which operator will get priority?
Now this operator will be solved. That is 2, power of 4.
Now what kind of expression will be formed?
Now, the expression that will be formed will be 2 multiplied by 16. Okay?
Then division. 4 was a power of 2 which resulted in 16.
Now again the remaining expression 3 + 4 - 1. Same expression as before.
What did we solve right now? 2, power of 4 got solved.
Clear?
Now what will be the priority? Multiplication, division etc will get priority.
I’ll do multiplication first. So 2 multiplied by 16 that is 32.
Back to division 3 + 4 -1. I wrote this expression again.
So we started by solving the brackets, then we solved the power of 2.
And then 2 multiplied by 16 for which we got 32.
Now, we will solve 32 flow division by 3.
Meaning we will solve these two particular operators by flow division.
The double slash is called flow division.
Now our expression after solving will be…
10 + 4 - 1.
Which values are left? 10 + 4 - 1.
Now we will add 10 & 4 operator as it is the priority. So we will solve it first.
14 - 1. So what is our last & remaining expression?
14 - 1. Right?
After solving this, we get our final value that is 13.
The value which got assigned under the value variable is 13.
Here, each operator gets precedence in such a way.
Meaning that a particular operator’s priority is set in a computer which flows from left to right.
I hope you understood Precedence.
So guys, this is Operator Precedence.
Moving forward, we will see another example of Operators in Operator Precedence.
Suppose I have certain variables here under which I’ll assign some values.
I'll add a D variable 5.
Here I have 4 variables, right? Now I want to check its operator precedence,
How to check it? I’ll create an equation here.
Suppose I created an equation here.
Took a variable X to be (a + b) in a bracket.
And added a multiplication sign and wrote c divided by d.
So this is our equation.
But how will the equation work? I’ll show you in a comment.
A’s value is 20. What is B’s value? B’s value is 10.
So these two will get added here which is 30.
Now both of these will get multiplied with what? With C.
We got C 15. Why? Because what have we written in our equation?
20 + 10 multiplied by C and divided by D.
What is C’s value? C is 15. And the division is by D that is 5.
So this is how the equation is formed.
Now if I print the value...printing the answer in X.
It will print us the value here. Clear?
I’m going to make another equation in the same manner where I’ll out double brackets,
a + b, multiplication by C and put D here.
I’ll explain it like before.
First of all 30, multiplication by 15 and then division by 5.
Now that I print it...value of Y. I got the answer to the equation here as well.
Here, firstly the addition that is the values inside the brackets took place, and then it will solve the outer division values.
I’ll show you one more equation…
For instance I have a variable Z here.
I’ll write a + b multiplied by C in a bracket and then divided by D.
How will the equation form here?
First will be 20 that is the value of A. + B multiplied by C that is 10 multiplied by 15 that is 150. That will go inside the bracket.
Division by D that is 5.
Now when I print, value of Z. We have got the result.
So this is how Operator Precedence takes place.
In the next video, we will work with Conditional Statement.
Share a personalized message with your friends.