Operators in Python
Let us explain it.
You can read or watch the explanation
Let us know about operators. Now, Just open your python IDLE and write your code as.
Can you say what output we will get? Obviously 3. Let’s play with this code. Now let’s write some more code by using some more operators.
Run it and check the output. You will be getting the appropriate results according to the mathematical calculations.
By this we can understand that Python can be used as a calculator. But, if we consider it seriously we are now going to enter into the new origin called operators and expressions.
Let us understand about the operator.
What is an operator?
An operator is a symbol, which is able to operate on the values.
For example, We use + sign to add the numbers and minus to subtract the numbers in maths. If we connect data and operations together then it is called an expression. The best example is a literal itself which we have learned in the previous tutorial. If you wanna learn more about it then the link will be in the description.
Not all Python operators are as obvious as the plus or minus sign. So let's go through some of the operators in Python, and we’ll also understand which rules are used and how to interpret those operations.
Operators are also called data manipulation tools. By the end of this video we will understand why they are called data manipulation tools.
We’ll begin with the operators which are according to arithmetic operations. i.e.,
plus
minus
multiplication
division
modulo
Integer Division Operator:
You know all of these operators already. In python there are 2 more special symbols added as operators. They are,
double division
double asterisk
we will not call them that way actually in python. In Fact we will call them “Integer division” and “exponentiation” in Python. But what do we do with them? Why are they added in python? We already have the basic mathematical symbols by which we can do almost all calculations.
Let’s write code and understand what they exactly do. Take 2 integers and try to use these operators in between them and try to print them.
Output :
1
Let’s add some more code to compare them with basic operators i.e., basic division /. So the code would be
Output :
1.5
By this you would understand what those special operators exactly do.
If you compare integer division with normal division. Integer division is throwing away the decimal part. That means you are getting a round figure of the given 2 numbers. If you wanna delete any fractional part during calculations then you can use this operator.
Let us take an example and give a float this time.
Output :
1
1.0
The output of the first line is 1 because both are integers, whereas the output of the second line will be 1.0 as you kept the numerator in float.
What if you use basic division instead of integer division with the same set of numbers?
The output will be the same in both the cases i.e., 1.5
Here we have to understand what is the key difference between 2 operators?. By using basic division we will get the exact number , we will not get any modified result.
Coming to integer division, Here the result will always be rounded to the nearest integer which is nothing but, floor of the real value means the value which is value is less than the real value. So, Integer division is also called floor division.
As you can see in the example. The result that we got using basic division is 1.5 and the result that we got using integer division or floor division is 1.0 which is less than the basic and real value. I hope you understand.
What if you alter the code by using negative integers and negative float numbers ?. See what happens.Now alter your code to
If we run it we got the output of -2 and -2.0
Try using basic division.
you are getting -1.5 as output. Coming to the negative side as you know -2.0 or -2 lesser than -1.5 right?
So, By this we understood that, we will get the output which is less than the real value if we use integer division or floor division instead of basic division.
Do not try to:
- perform a division by zero;
- perform an integer division by zero;
- find a remainder of a division by zero.
Because that will not work. You will get errors and exceptions. There is a lot of difference between error and exceptions we will learn about in coming tutorials.
Exponentiation Operator:
There is also one more special symbol which is called exponentiation. You’ve gone through this concept already in maths. For example, We want to find the value of 2 cube.To get that value.
we get the output as 8. The left argument is base whereas the right argument is an exponent. When both the arguments are integers then you will get the result in integer and if any one of them is in float then you will get the result in float.
For example use the same example and this time change any of the numbers to float. Here I’m changing base from integer to float, you can change exponent to float or you can keep both base and exponent in float and the result will also be in float if you save and run it.
Modulo Operator :
There is one more operator called modulo which is common in all programming languages where you will get a reminder left after division as output. Let us understand in general.
For example, you have 6 chocolates and you have to share your 6 chocolates equally with 2 members. How much can you share equally with both of them? You can share exactly 3 to each of them. How many do you have left? 0 - you have nothing so 0 will remain.
What if you have 7 chocolates and you have to share equally to 2 members? You will give both of them equally 3 chocolates and how many you left this time? 1 and this one is nothing but a reminder.
The thing you left after division is called reminder and modulo uses this logic to get the desired result. Same story goes even here which means if you give any of the arguments to float the result will also be in float.
See this example
we will get the output in float:
0.5 and 1.5
Unary and Binary Operators :
Let’s
You will get 0 as you know
No matter whatever operator you use, if you give any of the arguments in float the result will also be in float.
output: 0.0
subtraction part:
if you subtract like this for eg:
output: -3
The numbers are added in general but the result will be in negative sign since it obeys the sign of the greatest number.
You can also give float numbers if you wanna subtract float numbers and the result will be in float.
output: -4.0
This is a great opportunity to present a very important concept called unary and binary operators. If you have little knowledge of c or c++ then you would know about them.
Unary operator :
According to programming, Unary operator is an operator which takes only one number or integer and it will perform only either of the 2 things.
- increment - it increases the value by adding 1 and the symbol of increment operator is ++.
- decrement - it decreases the value by subtracting it by 1, the symbol for decrementing is -- where by these symbols compiler will understand whether to increment or decrement a number.
So, by using unary operators you will perform either increment or decrement. This is done in other programming languages. But, In python, the unary operator will not work this way and there will not be any symbols like ++ or --. Here it just represents the sign of the given number.
For eg if you give
then, it will not increment the value by one and then it will not print. It just prints +2 by which we can understand that 2 is a positive integer or number.
But, such construction is syntactically correct and also using it doesn’t make much sense. So it is not that important.
Binary Operator :
The operator which needs exactly 2 numbers to complete its calculation is called a binary operator.
For eg, In subtracting applications, the minus operator expects two arguments: the left and right arguments where left side is called as minuend and right argument is called as subtrahend in mathematical terms.
For this reason, the subtraction operator is considered to be one of the binary operators, the addition, multiplication and division operators are also called binary operators .
Till now we understand what each and every operator is doing in Python. Let’s build the code using more than one operator.
output: 17
Priority of Operators :
How do we know the operations are done in which order?
You probably remember from school that multiplications are given higher priority than additions. So, you would first multiply 3 by 5 and, keeping the 15 in your memory, you would add it to 2,then you would be getting the result of 17.
The same thing is done in python which is known as the priority of operators. Means operators having higher priority will perform first and lower priority operators will perform next. if you know that * has a higher priority than +, you will get the obvious result.
This is the order of priority placed from top to to bottom that is from higher priority to lower priority.
- unary
**
* / %
- binary
What if we had operators of equal priority?
The binding of the operator determines the order of computations performed by some operators with equal priority, put side by side in one expression.
Then you will first perform the calculation of 9 % 2 and then you store the result and you will make % 2 with the result. Since we have the same priority of operators you will go from left to right this is called left-side binding.
Most of Python’s operators have left-sided binding. You can also go from right to left but what happens if you go from right to left in this case.
first 6 % 2 gives 0, and then 9 % 0
Can we do 9 % 0 ? No, because 9 % 0 is completely against mathematical rules which we learned a moment ago. So, the interpreter gives us some error But, sometimes the binding goes from right to left as well.
Let’s try to use the same exponent operator between 3 numbers and check which binding it is using.
output: 256
Here first it will work on getting the result of 2 ** 3 which is 8 and then it will calculate 2 ** 8 which is 256. If it goes from left to right that is
2 ** 2 → 4 ** 3 → 64 (you would get 64 as output).
One more thing, you’re always allowed to use parentheses, which can change the natural order of a calculation.According to arithmetic rules, subexpressions in parentheses are always calculated first. You can use as many parentheses as you need.
Let’s build code as
Here first 25 % 13 is calculated then it is added with 100 and then it is multiplied with 5.
With parentheses though + is not having higher priority than multiplication plus is calculated first and multiplication is done later. So, parentheses change the priority of operators. Parentheses also helps in predicting the output because here the interpreter will not follow any type of binding and it will just follow those parentheses and as a programmer we will not have any ambiguity in which way calculation is done.
So far you learned about
- types of operators
- priority of operators
- Importance of parentheses
By this we can say that operators are data manipulation tools because we are getting the result the way we want by using our own operators.
Operators in Python :
Let us know about operators. Now, Just open your python IDLE and write your code as.
Can you say what output we will get? Obviously 3. Let’s play with this code. Now let’s write some more code by using some more operators.
Run it and check the output. You will be getting the appropriate results according to the mathematical calculations.
By this we can understand that Python can be used as a calculator. But, if we consider it seriously we are now going to enter into the new origin called operators and expressions.
Let us understand about the operator.
What is an operator?
An operator is a symbol, which is able to operate on the values.
For example, We use + sign to add the numbers and minus to subtract the numbers in maths. If we connect data and operations together then it is called an expression. The best example is a literal itself which we have learned in the previous tutorial. If you wanna learn more about it then the link will be in the description.
Not all Python operators are as obvious as the plus or minus sign. So let's go through some of the operators in Python, and we’ll also understand which rules are used and how to interpret those operations.
Operators are also called data manipulation tools. By the end of this video we will understand why they are called data manipulation tools.
We’ll begin with the operators which are according to arithmetic operations. i.e.,
plus
minus
multiplication
division
modulo
Integer Division Operator:
You know all of these operators already. In python there are 2 more special symbols added as operators. They are,
double division
double asterisk
we will not call them that way actually in python. In Fact we will call them “Integer division” and “exponentiation” in Python. But what do we do with them? Why are they added in python? We already have the basic mathematical symbols by which we can do almost all calculations.
Let’s write code and understand what they exactly do. Take 2 integers and try to use these operators in between them and try to print them.
Output :
1
Let’s add some more code to compare them with basic operators i.e., basic division /. So the code would be
Output :
1.5
By this you would understand what those special operators exactly do.
If you compare integer division with normal division. Integer division is throwing away the decimal part. That means you are getting a round figure of the given 2 numbers. If you wanna delete any fractional part during calculations then you can use this operator.
Let us take an example and give a float this time.
Output :
1
1.0
The output of the first line is 1 because both are integers, whereas the output of the second line will be 1.0 as you kept the numerator in float.
What if you use basic division instead of integer division with the same set of numbers?
The output will be the same in both the cases i.e., 1.5
Here we have to understand what is the key difference between 2 operators?. By using basic division we will get the exact number , we will not get any modified result.
Coming to integer division, Here the result will always be rounded to the nearest integer which is nothing but, floor of the real value means the value which is value is less than the real value. So, Integer division is also called floor division.
As you can see in the example. The result that we got using basic division is 1.5 and the result that we got using integer division or floor division is 1.0 which is less than the basic and real value. I hope you understand.
What if you alter the code by using negative integers and negative float numbers ?. See what happens.Now alter your code to
If we run it we got the output of -2 and -2.0
Try using basic division.
you are getting -1.5 as output. Coming to the negative side as you know -2.0 or -2 lesser than -1.5 right?
So, By this we understood that, we will get the output which is less than the real value if we use integer division or floor division instead of basic division.
Do not try to:
- perform a division by zero;
- perform an integer division by zero;
- find a remainder of a division by zero.
Because that will not work. You will get errors and exceptions. There is a lot of difference between error and exceptions we will learn about in coming tutorials.
Exponentiation Operator:
There is also one more special symbol which is called exponentiation. You’ve gone through this concept already in maths. For example, We want to find the value of 2 cube.To get that value.
we get the output as 8. The left argument is base whereas the right argument is an exponent. When both the arguments are integers then you will get the result in integer and if any one of them is in float then you will get the result in float.
For example use the same example and this time change any of the numbers to float. Here I’m changing base from integer to float, you can change exponent to float or you can keep both base and exponent in float and the result will also be in float if you save and run it.
Modulo Operator :
There is one more operator called modulo which is common in all programming languages where you will get a reminder left after division as output. Let us understand in general.
For example, you have 6 chocolates and you have to share your 6 chocolates equally with 2 members. How much can you share equally with both of them? You can share exactly 3 to each of them. How many do you have left? 0 - you have nothing so 0 will remain.
What if you have 7 chocolates and you have to share equally to 2 members? You will give both of them equally 3 chocolates and how many you left this time? 1 and this one is nothing but a reminder.
The thing you left after division is called reminder and modulo uses this logic to get the desired result. Same story goes even here which means if you give any of the arguments to float the result will also be in float.
See this example
we will get the output in float:
0.5 and 1.5
Unary and Binary Operators :
You will get 0 as you know
No matter whatever operator you use, if you give any of the arguments in float the result will also be in float.
output: 0.0
subtraction part:
if you subtract like this for eg:
output: -3
The numbers are added in general but the result will be in negative sign since it obeys the sign of the greatest number.
You can also give float numbers if you wanna subtract float numbers and the result will be in float.
output: -4.0
This is a great opportunity to present a very important concept called unary and binary operators. If you have little knowledge of c or c++ then you would know about them.
Unary operator :
According to programming, Unary operator is an operator which takes only one number or integer and it will perform only either of the 2 things.
- increment - it increases the value by adding 1 and the symbol of increment operator is ++.
- decrement - it decreases the value by subtracting it by 1, the symbol for decrementing is -- where by these symbols compiler will understand whether to increment or decrement a number.
So, by using unary operators you will perform either increment or decrement. This is done in other programming languages. But, In python, the unary operator will not work this way and there will not be any symbols like ++ or --. Here it just represents the sign of the given number.
For eg if you give
then, it will not increment the value by one and then it will not print. It just prints +2 by which we can understand that 2 is a positive integer or number.
But, such construction is syntactically correct and also using it doesn’t make much sense. So it is not that important.
Binary Operator :
The operator which needs exactly 2 numbers to complete its calculation is called a binary operator.
For eg, In subtracting applications, the minus operator expects two arguments: the left and right arguments where left side is called as minuend and right argument is called as subtrahend in mathematical terms.
For this reason, the subtraction operator is considered to be one of the binary operators, the addition, multiplication and division operators are also called binary operators .
Till now we understand what each and every operator is doing in Python. Let’s build the code using more than one operator.
output: 17
Priority of Operators :
How do we know the operations are done in which order?
You probably remember from school that multiplications are given higher priority than additions. So, you would first multiply 3 by 5 and, keeping the 15 in your memory, you would add it to 2,then you would be getting the result of 17.
The same thing is done in python which is known as the priority of operators. Means operators having higher priority will perform first and lower priority operators will perform next. if you know that * has a higher priority than +, you will get the obvious result.
This is the order of priority placed from top to to bottom that is from higher priority to lower priority.
- unary
**
* / %
- binary
What if we had operators of equal priority?
The binding of the operator determines the order of computations performed by some operators with equal priority, put side by side in one expression.
Then you will first perform the calculation of 9 % 2 and then you store the result and you will make % 2 with the result. Since we have the same priority of operators you
will go from left to right this is called left-side binding.
Most of Python’s operators have left-sided binding. You can also go from right to left but
what happens if you go from right to left in this case.
first 6 % 2 gives 0, and then 9 % 0
Can we do 9 % 0 ? No, because 9 % 0 is completely against mathematical rules which we learned a moment ago. So, the interpreter gives us some error But, sometimes the binding goes from right to left as well.
Let’s try to use the same exponent operator between 3 numbers and check which binding it is using.
output: 256
Here first it will work on getting the result of 2 ** 3 which is 8 and then it will calculate 2 ** 8 which is 256. If it goes from left to right that is
2 ** 2 → 4 ** 3 → 64 (you would get 64 as output).
One more thing, you’re always allowed to use parentheses, which can change the natural order of a calculation.According to arithmetic rules, subexpressions in parentheses are always calculated first. You can use as many parentheses as you need.
Let’s build code as
Here first 25 % 13 is calculated then it is added with 100 and then it is multiplied with 5.
With parentheses though + is not having higher priority than multiplication plus is calculated first and multiplication is done later. So, parentheses change the priority of operators. Parentheses also helps in predicting the output because here the interpreter will not follow any type of binding and it will just follow those parentheses and as a programmer we will not have any ambiguity in which way calculation is done.
So far you learned about
- types of operators
- priority of operators
- Importance of parentheses
By this we can say that operators are data manipulation tools because we are getting the result the way we want by using our own operators.
Post a Comment
0Comments