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 python-variable-types.
Operators are the symbol which tells the Python interpreter to do some mathematical and logical operation.
Types of Operator in Python
|Logical (Boolean) operators|
Suppose value of a = 20 and b = 10.
|+||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|
Suppose value of a and b are 20 and 10.
|==||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.|
|=||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|
|& (Binary AND)||Performs the Bit-wise AND operation.|
|| (Binary OR)||Perform the Bit-wise OR operation.|
|^ (Binary XOR)||Performs Bit-wise 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.|
Python has two special operators Identity Operators and Membership 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.
|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 are used to check for membership in a sequence(String, list, tuple etc), means whether a particular value exists in a sequence or not.
|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
|**||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 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…….