# Python operators

In this tutorial. we will see about Python operators.

Table of Contents

Python operators are nothing but special symbols which performs arithimatic or logical or assignment operations,

Let’s go through all Python operators one by one.

## Arithmetic operators

```
x = 15
y = 10
# + operator is used to add two operands
#Output: x + y = 25
print('x + y =',x+y)
# - operator is used to substract two operands
# Output: x - y = 5
print('x - y =',x-y)
# * operator is used to multiply two operands
print('x * y =',x*y)
# / operator is used to divide two operands
# Output: x / y = 1.
print('x / y =',x/y)
# // operator is used to floor divide two operands
# Output: x // y = 1
print('x // y =',x//y)
# ** operator is used to find power of two operands
# Output: x ** y = 576650390625
print('x ** y =',x**y)
```

Output of above program:

x – y = 5

x * y = 150

x / y = 1.5

x // y = 1

x ** y = 576650390625

## Comparison operators in Python

Comparison operators is used to compare two values. It either returns True or False.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
x = 15 y = 20 # If left operand is greater than right # Output: x > y is False print('x > y is',x>y) # If left operand is less than right # Output: x < y is True print('x < y is',x<y) # If left operand is equal to the right # Output: x == y is False print('x == y is',x==y) # If left operand is not equal to the right # Output: x != y is True print('x != y is',x!=y) # If left operand is greater than equal to the right # Output: x >= y is False print('x >= y is',x>=y) # If left operand is less than equal to the right # Output: x <= y is True print('x <= y is',x<=y) |

Output of above program:

x < y is True

x == y is False

x != y is True

x >= y is False

x <= y is True

## Logical operators in Python

Logical operators are and, or and not.

```
x=True
y=False
z= x and y
print("X and Y: ",z)
z= x or y
print("X or Y: ",z)
z= not x
print("X not Y: ",z)
```

Output of above program:

X or Y: True

X not Y: False

## Bitwise operators

Bitwise operators works on bits of operands.It works on bitwise presentation of bits.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
x = 2 y = 4 # Bitwise and print('x & y is',x & y) # Bitwise or print('x | y is',x | y) #Bitwise print('~x is',~x) # Bitwise XOR print('x ^ y is',x^y) # Bitwise right shit print('x >> y is',x>>y) # Bitwise left shift print('x << y is',x<<y) |

x | y is 6

~x is -3

x ^ y is 6

x >> y is 0

x << y is 32

## Assignment operators

Assignment operators are used to assign values to variables.

For example You can assign add and assign the value to same variable using x=x+5.

Operator | Example | Assignment |
---|---|---|

= | 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 |

^= | x ^= 5 | x = x ^ 5 |

>>= | x >>= 5 | x = x >> 5 |

<<= | x <<= 5 | x = x << 5 |

## Python specific operators

There are some python specific operators which use only in python.

### Identity opetators

is and is not both are identity operators and are used to check if both operators point to same memory location.

```
s1='code2master'
s2='code2master'
print('If s1 is s2:',s1 is s2)
x1=2
x2=2
print('If x1 is not x2:',x1 is not x2)
list1=[9,8,7]
list2=[9,8,7]
print('If list1 is list2:',list1 is list2)
```

Output:

If x1 is not x2: False

If list1 is list2: False

### Memebership opetators

in and not in operators are used to check if a value is present in the sequence or not. It is often is used to check value in list, dict or tuple.

```
list1=[9,8,7]
print('If 9 is present in list1: ',9 in list1)
str1='code2master'
str2='code'
print('If code is present in code2master: ',str2 in str1)
str1='code2master'
str2='java'
print('If java is not present in code2master: ',str2 not in str1)
```

Output:

If code is present in code2master: True

If java is not present in code2master: True

That’s all about Python operators.