 # What is Operator in Python | Types of Operator

0
(0)

## Introduction

Operators are the symbols carryout the arithmetic or logical computations on the operands.

Types of operators:

There are 7 types of operators. They are

• Arithmetic operators
• Comparison operators
• Logical operators
• Bitwise operators
• Assignment operators
• Identity operators
• Membership operators

## Arithmetic operators

Example
 ```1 2 3 4 5 6 7 8``` ```x=16 y=6 print("x+y=",x+y) print("x-y=",x-y) print("x*y=",x*y) print("x/y=",x/y) print("x//y=",x//y) print("x%y=",x%y)```

Output:

```x+y= 22
x-y= 10
x*y= 96
x/y= 2.6666666666666665
x//y= 2
x%y= 4
```

## Comparison operators

Comparison operators compare given two operands or values and return True or False.

Example
 ```1 2 3 4 5 6 7 8``` ```x=10 y=15 print("x>y is:",x>y) print("x=y is:",x>=y) print("x<=y is:",x<=y) print("x==y is:",x==y) print("x!=y is:",x!=y) ```
Output:
```x>y is: False
x<y is: True
x>=y is: False
x<=y is: True
x==y is: False
x!=y is: True
```

## Logical operators

Logical operators are used to club two conditions and to evaluate them. The conditions can be negated using logical not operator.

Example
 ```1 2 3``` ```print((5>2)and (6>3)) print((2<5)or (6>3)) print(not(5==5)) ```

Output:

```True
True
False```

## Bitwise operators

Bitwise operators act on binary equivalent of the operands as if they were strings of binary digits. They operate bit by bit.

For example 2 in binary is 0010, 7 in binary is 0111.

In the below table let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Example

 ```1 2 3 4 5 6 7 8 9 10``` ```x=10 y=4 print("x:",x) print("y:",y) print("x&y:",x&y) print("x|y:",x|y) print("~x:",~x) print("x^y:",x^y) print("x>>2:",x>>2) print("x<<2:",x<<2) ```

Output:
```x: 10
y: 4
x&y: 0
x|y: 14
~x: -11
x^y: 14
x>>2: 2
x<<2: 40```

## Assignment operators

Assignment operators are used to assign a value to a variable. Simple assignment operator(=)assigns the value to its right to variable to its left.

Example
 ```1 2``` ```x=10 print("x:",x) ```

Output:

`x:10`

Compound assignment operators

## Identity operators

Identity operators “is” and “is not” are used to check if the operands or objects to the left and right of these operators are referring to a value stored in the same memory location and returns True or False.

Example
 ```1 2 3 4 5 6 7 8 9``` ```n1=10 n2=10 print(n1 is n2) s1="Good morning" s2="Good morning" print(s1 is not s2) l1=[1,2,3] #refer list copy behaviour l2=[1,2,3] print(l1 is l2) ```

Output:

```True
False
False```

## Membership operators

Membership operators “in” and “not in” are used to check if the given value is present in the sequence of values and returns True or False.

In dictionary, membership operators will return True if the key is present. Presence of value in a Dictionary cannot be checked using membership operators.

Example

 ```1 2 3 4``` ```a=[1,2,3,4] print(5 in a) square={1:1,2:4,3:9,4:16} print(4 in a) ```

Output:
```False
True```

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...