division operators in python

 





Operators in Python. 

In this post we will talk about Operators in Python. Let's start..!

Now What is operators ?  Operator is very simple concept. Operator is a special character or a symbol that represents a action. In mathematics we use ' +, -, x, /, = ' etc. operators. Each operator represents an action for ex. ' + ' ( plus ) operator represents addition operation. Like this in programming we use such operators to represents an action is going to perform. There are some types of operators in programming I have listed some of them with their use. 


1} Arithmetic & Assignment Operators : 

This operators are simple operators that we use for calculation purpose. Here some operators that we use for calculation is ' + ' ( Addition ), ' - ' ( Subtraction ), ' * ' ( Multiplication ), ' / ' ( Division ) and this is assignment operator ' = ' ( Assignment ) etc.

for ex.  x = a + b here , if a = 10 and b = 5 then the value of x will be x = 15. Now,  suppose x = 20 and equation is x = x + 5 What will be the value of x ?  How this will work ? It's very simple any operation like this we first calculate a right side of ' = ' ( assignment operator ) and stores that result in variable of left side. Simply x is 20 then x + 5 = 20 + 5 = 25 now this result 25 we will store in variable of left side i.e. x now the value of x will be 25. 

To assign value to a variable we use assignment operator that is ' = '. for ex. number_first = 90 , number_second = 10 etc. Here also we have shortcut that is assigning value to multiple variables in a single line. for ex. var1 , var2, var3 = 50 , 70 , 90 this will assign value 50 to variable var1 , value 70 to variable var2 and value 90 to variable var3. respectively .

If we are doing such operations here we can use shortcut of  this equation i.e. x += 5. Now How this will work ? This is also very simple first take both side of operator and put first operator in between them for ex. x = 50 and equation is x += 20 here first we are taking both sides i.e. x and 20 ( x  20 ) now put the first operator in between them i.e. ' + ' ( x + 20 ) and calculate the result and stores in variable of left side that is x  and result is x + 20 i.e. 70 and this will be store in x. So now value of x will be 70. 

Now you have learned about how shortcut works. Here are some shortcuts for ex. ' += ', ' -= ', ' *= ', ' /= ' .




2} Relational Operators : 

This type of operator is used to comparison purpose . Here are some of them. ' == ' ( equal ), ' != ' ( not equal ), ' > ' ( greater than ), ' < ' ( less than ), ' >= ' ( greater than / equal to ), ' <= ' ( less than / equal to) .

For ex. a = 10 , b = 20 then 

a == b is false because a is not equal to b . a != b is true because a is not equal to b. Same for ' < ' , ' > ' operator also. If a < b then true and a > b then false. 

Here ' >= ' and ' <= ' returns true if one of the condition is true , for ex. a = 1 , b = 2 , c = 1 . So if a >= b this will return false because a is not greater than b Or a is not equal to b. But  a >= c this will return true. Even a is not greater than c but here a is equal to c that's why this will return true. Same rules for ' <= ' also. 




3} Logical Operator : 

Here we have mainly three operators. ' and ' , ' or ' , and ' not '. This type of operators we use when we want to check multiple conditions at same time.

Operator ' and ' will return true if  all conditions are true, else this will return false. For ex. a  = 10 , b = 10 , c = 20 and condition is a == b and ( this ' and ' here is a logical operator ) a == c here you can see first condition is true i.e. a == b. But the second condition i.e. a == c is false . hence this will return false. This will return true only if there all conditions are true. If you want result as true your all conditions should be true, otherwise your result will be false. 

Operator ' or ' will return true if any one condition is true from all conditions , else this will return false. For ex. a  = 10 , b = 10 , c = 20 and condition is a == b or ( this ' or ' here is a logical operator ) a == c here you can see first condition is true i.e. a == b. And second condition is false i.e. a == c . Because first condition is true so this will return true. This will return false only if all conditions are false. If you want result as true at least your any one condition should be true, otherwise your result will be false.

Operator ' not ' use to compliment the result. It means if your condition is true this will return false and your condition is false this will return true. for ex.  a  = 10 , b = 20 and condition is not( a == b ) ( this ' not ' here is a logical operator ) Here you can see condition a == b is false because a is not equal to b . But we are using ' not ' logical operator here this will return true. This operator will return false if given condition is true and will return true if given condition is false. If you want result as true your condition should be false, otherwise your result will be false.



 


What is bitwise operators ? and How it works ? that we will see in next post. 


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post