Operators in Python

By
Operators In Python Blog

Operators in Python is very use full to perform any operation and logic.
Operators are special symbols that carry out arithmetic or logical or any other computation.
Python can construct which symbol can we used to manipulate the value of the operands.

In python basically six type of operators available, that listed are as under

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Special Operators

1) Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

2) Relational Operators

These are also known as Comparison operators.
Relational operators are used to compare or relate the values.
It either return True or False according to the condition.

3) Logical Operators

The logical operators for And, Or and Not are used to combine simple relational statements into more complex expressions.

4) Bitwise Operators

Bitwise operators act on the operands as if they were strings of binary digits.
It operates bit by bit, hence the name bitwise operator.
The bitwise operator works at bit and performs bit by bit operations in Python.

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

5) Assignment Operators

Assignment operators are used in python to assign values to variables.
Here,
a = 5 is a simple assignment operator that assigns the value on the right, to the variable a on the left.

Operator Example Equivalent to
= x = 5 x = 5
-= x -= 5 x = x – 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x = x & 5
|= x |= 5 x = x | 5
^= x ^= 5 x = x ^ 5

6) Special Operators

Python language provides some special types of operators,

  • Identity Operators
  • Membership Operators
  • Identity Operators

  • “is” and “is not” are the identity operators in python.
    This is used to check if two variables are located on the same part of the memory.
    Two variables that are equal, do not imply that they are identical.

    Operator Meaning Example
    is If True, refer to the same object. a is b
    isnot If True, do not refer to the same object. a is not b x

    Make one file called as special_operators.py

    #Identity Operators
    
    a1 = 5
    b1 = 5
    a2 = 'Hello'
    b2 = 'Hello'
    a3 = [1,2,3]
    b3 = [1,2,3]
    print(a1 is not b1)
    print(a2 is b2)
    print(a3 is b3)
    
  • Membership Operators

  • “in” and “not in” are the membership operators in python.
    This is used to test whether a variable is found in a sequence or not.
    Note : In a dictionary, a user can only test for the key, not the value.

    Operator Meaning Example
    in True if value is found in the sequence 5 in x
    notin True if value is not found in the sequence 5 not in x

create python file name as special_operators.py

#Membership Operators

x = "Hello world"
y = {1:'a',2:'b'}
print('H' in x)
print('hello' not in x)
print('Hello' not in x)
print(1 in y)
print('a' in y)

Latest Blog