Explore
ProGrad Programs
About Us

Edit
Reply




Edit

Python Operators: Types of Operators in Python with Examples | FACE Prep

Published on 09 Mar 2020

Similar to other programming languages, Python also has some special characters called operators. Operators in Python are useful to perform different operations on variables/values. These variables/values which are passed as an input to an operator are known as operands. Further, these operands & operators form an expression.

Consider a basic arithmetic expression, a + b.Here,a and b are operands and the symbol + is an operator.


operators in python


Types of Operators in Python


Operators in Python are classified into the following 7 types:


  1. Arithmetic Operators
  2. Bitwise Operators
  3. Assignment Operators
  4. Relational or Comparison Operators
  5. Logical Operators
  6. Identity Operators
  7. Membership Operators


a) Arithmetic Operators in Python


Using arithmetic operators, we can perform mathematical operations like addition, subtraction, multiplication, division, etc. In Python, we also have additional arithmetic operators such as exponent and floor division.


Click here to practice example Python programs using arithmetic operators.


OperatorNameDescription
+AdditionThe operands on either side of this operator are added
-SubtractionThe operand on the right side of this operator is subtracted from the one on the left side
*MultiplicationThe operands on either side of this operator are multiplied
/DivisionThe operand on the left side of this operator is divided by the one on the right side. As a result, it returns the quotient in the form of a floating-point value
%ModuloThe operand on the left side of this operator is divided by the one on the right side. As a result, it returns the remainder value
**ExponentThis operator raises the left side operand to the power of the right side operand
//Floor divisionThe operand on the left side of this operator is divided by the one on the right side. As a result, it returns the quotient in the form of an integer value


Know more about Arithmetic operators


b) Bitwise Operators in Python


Python bitwise operators are used to perform bitwise calculations on integers. The given integers are converted into binary format and then operations are performed bit by bit. These are also called as Binary Operators.


OperatorName
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Bitwise left shift
>>Bitwise right shift


Go ahead and try to understand the example Python programs using Bitwise operators.


Know more about Bitwise operators


c) Logical Operators in Python


There are three logical operators in Python - and, or and not. They are used to combine two or more conditional statements.


OperatorDescription
andThe output is true when both the expressions are true
orThe output is true if either one of the expression is true
notReverses the output


Know more about Logical operators


d) Assignment Operators in Python


Using assignment operators, we can assign values to an operand.


Operator NameDescription
=AssignmentThis operator assigns its right-side value to its left-side operand
+=Addition assignmentThis operator adds left and the right side operands and assigns the result to the left side operand
-=Subtraction assignmentThis operator subtracts the right operand from the left operand and assigns the result to the left side operand
*=Multiplication assignmentThis operator multiplies the right-side operand with the left-side operand and assigns the result to the left side operand
/=Division assignmentThis operator divides the left side operand by the right-side operand and assigns the quotient to the left side operand
%=Modulus assignmentThis operator divides the left side operand by the right side operand and assigns the remainder to the left side operand
**=Exponentiation assignmentThis operator raises the left side operand to the power of the right-side operand and assigns the result value to the left side operand
//=Floor division assignmentThis operator divides the left operand by the right operand and assigns the quotient value (in the form of an integer value) to the left operand
&=Bitwise AND assignmentThis operator performs a bitwise AND operation on both the left and the right side operands and assigns the result to the left side operand
|=Bitwise OR assignmentThis operator performs a bitwise OR operation on both the left and the right side operands and assigns the result to the left side operand
^=Bitwise XOR assignmentThis operator performs bitwise XOR operation on both the left and the right side operands and assigns the result to the left side operand
>>=Bitwise right shift assignmentThis operator right shifts the given value by the specified position and assigns the result to the left side operand
<<=Bitwise left shift assignmentThis operator left shifts the given value by the specified position and assigns the result to the left side operand


>>Know more about Assignment operators in Python


e) Relational or Comparison Operators in Python


Using these operators, we can compare two operands and determine their relationship.


OperatorNameDescription
==Equal toChecks whether two operands are equal
!=Not equal toChecks whether two operands are not equal
>Greater thanChecks whether the left side operand is greater than the right side operand
<Less thanChecks whether the left side operand is less than the right side operand
>=Greater than or equal toChecks whether the left side operand is either greater or equal to the right side operand
<=Lesser than or equal toChecks whether the left side operand is either less than or equal to the right side operand


Know more about Relational Operators


f) Identity Operators in Python


Identity operators is and is not are used to check whether the memory locations of two variables/objects are the same or not. Also, we can use these operators to find if a variable/object belongs to a particular type or not.


OperatorDescription
isReturns True if the operands on either side of the operator point to the same object and false otherwise.
is notReturns False if the operands on either side of the operator point to the same object and true otherwise.


g) Membership Operators in Python


Membership operators in and not in are used to find whether a value is present in a particular Python object or not.


OperatorDescription
inReturns True if it finds a variable in the specified sequence and false otherwise.
not in Returns False if it does not find a variable in the specified sequence and false otherwise.

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'python'
Articles