- In python operators are generally used to perform operations on variables and values.
- In specific programming language operators are the pillars of a program on which the logic is built.
- Python operator consists of different types, they are
- Arithmetic operators
- Comparison operators
- Assignment operators
- Logical operators
- Bitwise operators
- Membership operators
- Identity operators
Arithmetic operators
- Arithmetic operators are used to perform arithmetic operations between two operands and it includes + (addition), – (subtraction), * (multiplication), / (divide), % (reminder), // (floor division) and exponent (**) operators.
- + (addition) operator is used to add two operands.
- – (Subtraction) operator is used to subtract the second operand from the first operand then if the first operand is less than the second operand, the value results negative.
- / (divide) returns the quotient after dividing the first operand by the second operand.
- * (multiplication) is used to multiply one operand with the other.
- % (reminder) returns the reminder after dividing the first operand by the second operand.
- **(Exponent) is an exponent operator represented as it calculates the first operand power to the second operand.
- // (Floor division) operator gives the floor value of the quotient produced by dividing the two operands.
Comparison operators
- It is used to comparing the value of the two operands and returns Boolean true or false accordingly.
- (= =) If the value of two operands is equal, then the condition becomes true.
- (! =) if the value of two operands is not equal, then the condition becomes true.
- (< =) If the first operand is less than or equal to the second operand, then the condition becomes true.
- (> =) if the first operand is greater than or equal to the second operand, then the condition becomes true.
- (>) If the first operand is greater than the second operand, then the condition becomes true.
- (<) If the first operand is less than the second operand, then the condition becomes true.
Assignment operators
- It is used to assign the value of the right expression to the left operand.
- (=) It assigns the value of the right expression to the left operand.
- (+=) It increases the value of the left operand by the value of the right operand and assigns the modified value back to left operand.
- (-=) It increases the value of the left operand by the value of the right operand and assigns the modified value back to left operand.
- (*=) It multiplies the value of the left operand by the value of the right operand and assigns the modified value back to then the left operand.
- (%=) It divides the value of the left operand by the value of the right operand and assigns the reminder back to the left operand.
- (**=) a**=b will be equal to a=a**b.
- (//=) A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.
Logical operators
- In expression evaluation logical operators are used primarily to make a decision.
- “and” operator is used when both the expression is true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true.
- “or” operator is used when one of the expressions is true, then the condition will be true. In this a and b are the two expressions, a → true, b → false => a or b → true.
- “not” operator is used when an expression a is true, then not (a) will be false and vice versa.
Membership operators
- It is used to check the membership of value inside a python data structure and if the value is present in the data structure, then the resulting value is true otherwise it returns false.
- “in” operator is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary).
- “Not in” operator is evaluated to be true if the first operand is not found in the second operand (list, tuple, or dictionary).
Identity operators
- It is used to decide whether an element certain class or type.
- “is” operator is evaluated to be true if the reference present at both sides point to the same object.
- “Is not” is evaluated to be true if the reference present at both sides do not point to the same object.
Bitwise operators
- These operators perform bit by bit operation on the values of the two operands.
- “& (binary and)” is used when both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied.
- “| (binary or)” operator is used where the resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1.
- “^ (binary xor)” operator is used where the resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0.
- “~(negation)” operator calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa.
- “<< (left shift)” operator is used where the left operand value is moved left by the number of bits present in the right operand.
- “>>(right shift)” operator is used where the left operand value is moved right by the number of bits present in the right operand.