Python Operators and Expression
Python Operators and Expression
Expression are made of Operator and Operands. In Python most of the code you will write as expression.
Before reading about Operators in Python, read about pythonvariabletypes.
Python Operators
Operators are the symbol which tells the Python interpreter to do some mathematical and logical operation.
Types of Operator in Python
Arithmetic operators 
Relational operators 
Logical (Boolean) operators 
Assignment operators 
Bitwise operators 
Special operators 
Arithmetic Operators
Suppose value of a = 20 and b = 10.
Operator  Description  Example 
+  For Addition  a + b = 30 
–  For Subtraction  a – b = 10 
*  For Multiplication  a * b = 200 
/  For Division  a / b = 2 
%  Returns reminder after division  a % b = 0 
**  Performs exponential (power) calculation on operators  a**b =20 to the power 10 
//  Floor Division – The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):  9//2 = 4 and 9.0//2.0 = 4.0, 11//3 = 4, 11.0//3 = 4.0 
Relational Operator
Suppose value of a and b are 20 and 10.
Operator  Description  Example 
==  Becomes true when value of a and b is equal.  (a == b) is not true. 
!=  True when a and b are not equal.  (a!=b) is true. 
<>  Same as != operator.  (a <> b) is true. This is similar to != operator. 
>  True if left operand is greater then right operand.  (a > b) is true. 
<  True if value of right operand is greater than left.  (a < b) is not true. 
>=  true if value of left operand is greater then or equal than left operand.  ( a>=b ) is true. 
<=  true if value of right operand is greater then left one.  (a <= b) is true. 
Assignment Operator
Operator  Description  Example 
=  Assigns values from right operands to left operand  c = a + b assigns value of a + b into c 
+=  Adds right operand to the left operand and assign the result to left operand.  c += a —> c = c + a 
=  Subtracts right operand from the left operand and assign the result to left operand.  c = a —> c = c a 
*=  Multiplies right operand with the left operand and assign the result to left operand.  c *= a —> c = c * a 
/=  Divides left operand with the right operand and assign the result to left operand.  c /= a —> c = c / a

%=  Takes modulus using two operands and assign the result to left operand  c %= a —> c = c % a 
**=  Performs exponential (power) calculation on operators and assign value to the left operand  c **= a —> c = c ** a 
//=  performs floor division on operators and assign value to the left operand  c //= a —> c = c // a 
Bitwise Operator
Operator  Description 
& (Binary AND)  Performs the Bitwise AND operation. 
 (Binary OR)  Perform the Bitwise OR operation. 
^ (Binary XOR)  Performs Bitwise XOR operation 
~ (Binary Ones Complement)  Used for Flipping of Bits. 
<< (Binary Left Shift)  Left operands value is moved left by the number of bits specified by the right operand. 
>> ( Binary Right Shift)  Left operands value is moved right by the number of bits specified by the right operand. 
Special Operators
Python has two special operators Identity Operators and Membership Operators.
Identity Operators
Identity operators are used to check if two different variables or values refers to same object or not, means if two values are equal does not mean that it refers to the same object. So in that case Identity variable will compare the location of the two.
Operator  Meaning  Example 
is  True if the operands are identical (refer to the same object)  ais b, here is results in 1 if id(a) equals id(b). 
is not  True if the operands are not identical (do not refer to the same object)  a is not b, here is not results in 1 if id(a) is not equal to id(b). 
Membership Operators
Membership operators are used to check for membership in a sequence(String, list, tuple etc), means whether a particular value exists in a sequence or not.
Operator  Description  Example 

in  True if a variable exist in the specified sequence and false otherwise.  x in y, true if x is a member of sequence y. 
not in  True if a variable exist in the specified sequence and false otherwise.  x not in y, true if x is not a member of sequence y. 
Operator Precedence in Python
Operator  Description  
()  Parenthesis (grouping)  
f(args)  Function call  
X[index:index]  Slicing  
X[index]  Subscription  
x.attribute  Attribute reference  
**  Exponentiation (raise to the power)  
~ + –  Complement, unary plus and minus (method names for the last two are +@ and @)  
* / % //  Multiply, divide, modulo and floor division  
+ –  Addition and subtraction  
>> <<  Right and left bitwise shift  
&  Bitwise ‘AND’  
^   Bitwise exclusive `OR’ and regular `OR’  
<= < > >=  Comparison operators  
<> == !=  Equality operators  
= %= /= //= = += *= **=  Assignment operators  
is is not  Identity operators  
in not in  Membership operators  
not or and  Logical operators 
Thanks for Reading…….
Nice tutorial…