Operators in python

Operators are used to performing operations on variables and values. In this article, you will learn operators in python in detail with examples.
Operators in python are divided into the following types:

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

Arithmetic operators:

Arithmetic operators are used for mathematical computations. Arithmetic operators are used with numeric values like integers or float.

OperatorDescriptionSyntax
+Addition Operator: Adds two valuesa * b
Subtraction Operator: Subtracts two Valuesa – b
*Multiplication Operator: Multiplies two Valuesa * b
/Division Operator: Divides two Valuesa / b
//Floor Division Operator: Returns result of the division in integera // b
%Modulus Operator: Returns the remainder after divisiona % b
**Exponent Operator: Performs exponentiation operationa ** b

Logical operators:

OperatorDescriptionSyntax
andReturns True if both conditions are truea < b and b < c
orReturns True if one of the condition is truea< b or b < c
notReverse the result i.e returns true if the result is truenot (a < b)

Comparison operator:

OperatorNameSyntax
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b
==Equala == b

Assignment operators:

Assignment operators are used to assign values to the variables.

OperatorExampleEquivalent to
+a = 2a = 2
+=a += 2a = a + 2
-=a -= 2a = a – 2
*=a *= 2a = a * 2
/=a /= 2a = a / 2
%=a %= 2a = a % 2
//=a //= 2a = a // 2
**=a **= 2a = a ** 2
&=a &= 2a = a & 2
|=a |= 2a = a | 2
^=a ^= 2a = a ^ 2
<<=a <<= 2a = a << 2
>>=a >>= 2a = a >> 2

Membership Operators:

Membership operators in python are used to check if particular item or substring is presented in string, list, tuple, etc.

OperatorDescriptionSyntax
inin operator returns True if particular item or substring is present in the string, list, tuple, etc. But it returns False if that item is not present.a in b
not innot in operator returns True if a particular item is not present in the string, list, tuple, etc. But it returns False if that item is present.a not in b

Identity Operators:

Identity operators in python checks if two objects or variables are same or not and if their memory location is also same or not.

OperatorDescriptionSyntax
isis operator returns True if two objects or variables are the same and their memory location is also the same.a is b
is notis not operator returns True if two objects or variables are not the same and their memory location is also not the same.a is not b

Bitwise operators:

Bitwise operators treats operands as binary digits and perform operation bit by bit.

OperatorDescriptionSyntax
&Bitwise AND operatora & b
|Bitwise OR operatora | b
^Bitwise XOR operatora ^ b
~Bitwise NOT operator~a
<<Bitwise left shift operatora<<
>>Bitwise right shift operatora>>

Summary:

Operators are used to performing operations on variables and values. There are different types of operators in python like arithmetic, logical, comparison, assignment, membership, identity, and bitwise.

See Also

Variables in python

Loops in python

Leave a Reply

Your email address will not be published. Required fields are marked *