Operators provide a vital role in programming, and in combination with values and other identifiers form expressions and statements, which is also an essential building block for Python programming.
Operators and Operands
Python operators are symbols that are used to perform mathematical or logical manipulations. Operands are the values or variables with which the operator is applied to, and values of operands can manipulate by using the operators.
Let us take a Scenario:
6 + 2=8, where there are two operands and a plus (+) operator, and the result turns 8.
Here a single operator is used to manipulate the values. The +, -, *, / and ** does addition, subtraction, multiplication, division & exponentiation respectively.
Types of Python Operators
Python programming language is rich with built-in operators.
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Identity Operators
- Bitwise Operators
- Membership Operators
|+||Addition||Adds the values on either side of the operator and calculate a result.|
|-||Subtraction||Subtracts values of right side operand from left side operand.|
|*||Multiplication||Multiplies the values on both sides of the operator.|
|/||Division||Divides left side operand with right side operand.|
|%||Modulus||It returns the remainder by dividing the left side operand with right side operand|
|**||Exponent||Calculates the exponential power|
|//||Floor Division||Here the result is the quotient in which the digits after decimal points are not taken into account.|
|=||Equal||Assigns the values of the right side operand to the left side operand.|
|+=||Add AND||Adds right-side operand value to the left side operand value and assigns the results to the left operand.|
|-=||Subtract AND||Subtracts right-side operand value to the left side operand value and assigns the results to the left operand.|
|*=||Multiply AND||Similarly does their respective operations and assigns the operator value to the left operand.|
|//=||Floor Division AND|
Comparison (Relational) Operators
|==||Double Equal||If the two value of its operands are equal, then the condition becomes true, otherwise false|
|!= or <>||Not Equal To||If two operand's values are not equal, then the condition becomes true. Both the operators define the same meaning and function|
|>||Greater Than||If the value of the left-hand operand is greater than the value of right-hand operand, the condition becomes true.|
|<||Less Than||If the value of the left-hand operand is less than the value of the right operand, then the condition becomes true.|
|<=||Less Than Equal To||If the value of the left-hand operand is less than or equal to the value of right-hand operand, the condition becomes true.|
|>=||Greater Than Equal To||If the value of the left-hand operand is greater than or equal to the value of right-hand operand, the condition becomes true.|
|or||Logical OR||If any of the two operands are non-zero, then the condition is true.|
|and||Logical AND||If both the operands are true, then the condition is true.|
|not||Logical NOT||It is used to reverse the logical state of its operand.|
For comparing memory locations of two objects, identity operators are used.
There are two types of identity operators. These are:
|is||is||The result becomes true if values on either side of the operator point to the same object and False otherwise.|
|is not||is not||The result becomes False if the variables on either side of the operator point to the same object|
These operators are used to manipulate with bits, & performs bit-by-bit operations.
There are six types of bitwise operators supported by Python. These are:
|&||Binary AND||This operator copies the bit to the result if it exists in both operands.|
||||Binary OR||This operator copies the bit if it exists in either of the operands.|
|^||Binary XOR||This operator copies the bit if it is set in one operand but not both.|
|~||Binary 1s Complement||This is a unary operator and has the ability of 'flipping' bits.|
|<<||Binary Left Shift||The left operands value is moved left by the number of bits specified by the right operand using this operator.|
|>>||Binary Right Shift||The left operands value is moved right by the number of bits specified by the right operand using this operator.|
|in||in||The result of this operation becomes True if it finds a value in a specified sequence & False otherwise.|
|not in||not in||result of this operation becomes True if it doesn't find a value in a specified sequence and False otherwise.|