Python – Operators

Operators in Python are used to perform operations on values and variables in general. Standard symbols for logical and mathematical operations are represented by these symbols. We’ll look at numerous types of Python operators in this tutorial.

Types of Operator

Python language supports the following types of operators.

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Let us have a look on all operators one by one.

Arithmetic Operators

To conduct arithmetic operations between two operands, arithmetic operators are employed. There are operators for + (addition), – (subtraction), (multiplication), /(divide), percent (reminder), /(floor division), and exponent (*).

Consider the following table for a detailed explanation of arithmetic operators.

OperatorDescription
+ (Addition)It is used to add two operands. to add two operands. For example, if a = 20, b = 10 => a+b = 30
– (Subtraction)Subtract right operand from the left or unary minus. For example, if a = 20, b = 10 => a – b = 10
/ (divide)divides the first operand by the second. For example, if a = 20, b = 10 => a/b = 2.0
* (Multiplication)It is used to multiply one operand with the other.  For example, if a = 20, b = 10 => a * b = 200
% (reminder)It returns the remainder when the first operand is divided by the second. For example, if a = 20, b = 10 => a%b = 0
** (Exponent)It is an exponent operator represented as it calculates the first operand power to the second operand.
// (Floor division)It gives the floor value of the quotient produced by dividing the two operands.

Comparison operators

Comparison operators are used to compare values. It returns either True or False according to the condition.

OperatorMeaningExample
>Greater than – True if left operand is greater than the rightx > y
<Less than – True if left operand is less than the rightx < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the right

Assignment operators

To assign values to variables in Python, assignment operators are used.

a = 10  is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.

There are various compound operators in Python like a +=10 that adds to the variable and later assigns the same. It is equivalent to a = a + 10 .

OperatorExampleEquivalent to
=x = 10x = 10
+=x += 10x = x + 10
-=x -= 10x = x – 10
*=x *= 10x = x * 10
/=x /= 10x = x / 10
%=x %= 10x = x % 10
//=x //= 10x = x // 10
**=x **= 10x = x ** 10
&=x &= 10x = x & 10
|=x |= 10x = x | 10
^=x ^= 0x = x ^ 10
>>=x >>= 10x = x >> 10
<<=x <<= 10x = x << 10

Bitwise Operators

Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

Logical Operators

Logical operators perform Logical ANDLogical OR, and Logical NOT operations. It is used to combine conditional statements.

OperatorDescriptionSyntax
andLogical AND: True if both the operands are truex and y
orLogical OR: True if either of the operands is true x or y
notLogical NOT: True if the operand is false not x

Membership operators

In Python, the membership operators are in and not in. They check whether a value or variable exists in a sequence (string, list, tuple, set, and dictionary).

In a dictionary we can only test for presence of key, not the value.

OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Identity Operators

The identity operators are used to decide whether an element certain class or type.

OperatorDescription
isIt is evaluated to be true if the reference present at both sides point to the same object.
is notIt is evaluated to be true if the reference present at both sides do not point to the same object.

Leave a Comment

Your email address will not be published.