Python Operators

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.

Python supports the following types of operators:
  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Identity Operators
  • Bitwise Operators
  • Membership Operators

Arithmetic Operators

Python Arithmetic Operators
SymbolOperator NameDescription
+AdditionAdds the values on either side of the operator and calculate a result.
-SubtractionSubtracts values of right side operand from left side operand.
*MultiplicationMultiplies the values on both sides of the operator.
/DivisionDivides left side operand with right side operand.
%ModulusIt returns the remainder by dividing the left side operand with right side operand
**ExponentCalculates the exponential power
//Floor DivisionHere the result is the quotient in which the digits after decimal points are not taken into account.

Assignment Operators

Python Assignment Operators
SymbolOperator NameDescription
=EqualAssigns the values of right side operand to left side operand.
+=Add ANDAdds right side operand value to the left side operand value and assigns the results to the left operand.
-=Subtract ANDSubtracts right side operand value to the left side operand value and assigns the results to the left operand.
*=Multiply ANDSimilarly does their respective operations and assigns the operator value to the left operand.
/=Division AND
%=Modulus AND
**=Exponent AND
//=Floor Division AND

Comparison (Relational) Operators

Python Comparison Operators
SymbolOperator NameDescription
==Double EqualIf the two value of its operands are equal, then the condition becomes true, otherwise false
!= or <>Not Equal ToIf two operands values are not equal, then condition becomes true. Both the operators define the same meaning and function
>Greater ThanIf the value of the left-hand operand is greater than the value of right-hand operand, the condition becomes true.
<Less ThanIf the value of the left-hand operand is less than the value of right operand, then condition becomes true.
<=Less Than Equal ToIf 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 ToIf the value of the left-hand operand is greater than or equal to the value of right-hand operand, the condition becomes true.

Logical Operators

Python Logical Operators
SymbolOperator NameDescription
orLogical ORIf any of the two operands are non-zero, then the condition is true.
andLogical ANDIf both the operands are true, then the condition is true.
notLogical NOTIt is used to reverse the logical state of its operand.

Identity Operators

For comparing memory locations of two objects, identity operators are used.

There are two types of identity operators. These are:

Python Identity Operators
SymbolOperator NameDescription
isisThe result becomes true if values on either side of the operator point to the same object and False otherwise.
is notis notThe result becomes False if the variables on either side of the operator point to the same object

Bitwise Operators

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:

Python Bitwise Operators
SymbolOperator NameDescription
&Binary ANDThis operator copies the bit to the result if it exists in both operands.
|Binary ORThis operator copies the bit if it exists in either of the operands.
^Binary XORThis operator copies the bit if it is set in one operand but not both.
~Binary 1s ComplementThis is a unary operator and has the ability of 'flipping' bits.
<<Binary Left ShiftThe left operands value is moved left by the number of bits specified by the right operand using this operator.
>>Binary Right ShiftThe left operands value is moved right by the number of bits specified by the right operand using this operator.

Membership Operators

Python Membership Operators
SymbolOperator NameDescription
ininThe result of this operation becomes True if it finds a value in a specified sequence & False otherwise.
not innot inresult of this operation becomes True if it doesn't find a value in a specified sequence & False otherwise.

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.