# Operators

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

## Arithmetic Operators <a href="#arithmetic-operators" id="arithmetic-operators"></a>

1. Addition Operator ( + )
2. Subtraction Operator ( - )
3. Multiplication Operator ( \* )
4. Division Operator ( / )
5. Modulas Operator ( % )
6. Floor Division ( // )
7. Exponent Operator ( \*\* )

Example 1:

```python
#Addition Operator 
x = 10
y = 3
z = x + y
print("Addition of {} and {} is {}.".format(x,y,z))
#Value of          x       y     z
x = 'hello '
y = 'world'
print(x+y)
l1 = [ 1,2,3]
l2 = [ 4,5,6]
print(l2 + l1 )
t1 = ('one','two','three')
t2 = ('four','five','six')
print(t1+t2)
''' Dictionary can not be processed by Airthmatic Operators
d = { 1:'one',2:'two'}
d1 = { 3:'three',4:'four'}
print(d+d1)
'''
""" Sets can not be processed by Airthmatic Operators
s1 = { 1,2,3 }
s2 = { 4,5,6 }
print(s1+s2)
"""
```

Addition of 10 and 3 is 13.

hello world

\[4, 5, 6, 1, 2, 3]

('one', 'two', 'three', 'four', 'five', 'six')

Example 2:

```python
#Subtraction Operator
x = 10
y = 50
print(x-y)
#s = 'hello'
#l = 'world' #strings cannot be subtracted
#print(s-l)
l1 = [ 1,2,3] #list does not support subtraction operation
l2 = [ 4,5,6]
print(l1-l2)
```

-40

TypeError Traceback (most recent call last)

&#x20;in () 8 l1 = \[ 1,2,3] #list does not support subtraction operation 9 l2 = \[ 4,5,6] ---> 10 print(l1-l2)

TypeError: unsupported operand type(s) for -: 'list' and 'list'

Example 3:

```python
x = 53
y = 20 
print(x*y)
s = 'Hello_World '
k = ' Hi '
#print(s*k)
print(5*s)  #print s 5 times
print(k*3) #print k 3 times
l1 =[1,2,3]
print(l1*3) #returns list 3 times
l2 = [ 4,5,6]
#print(l1*l2) # does not support multiplication
```

1060

Hello\_World Hello\_World Hello\_World Hello\_World Hello\_World Hi Hi Hi

\[1, 2, 3, 1, 2, 3, 1, 2, 3]

TypeError Traceback (most recent call last)

&#x20;in () 11 print(l1*3) #returns list 3 times 12 l2 = \[ 4,5,6] ---> 13 print(l1*l2) # does not support multiplication

TypeError: can't multiply sequence by non-int of type 'list'

Example 4:

```python
#Division Operator
x = 53
y = 12
print(x/y) #Absolute Division
print(x%y) #Remainder
print(x//y) #Quotient
```

4.416666666666667

5

4

Example 5:

```python
#Exponent
x = 5
y = 3
print(x**y)
```

125

## Comparison Operators <a href="#comparison-operators" id="comparison-operators"></a>

1. Less Than ( < )
2. Less Than Equals To ( <= )
3. Greater Than ( > )
4. Greater Than Equals To ( >= )
5. Equals To Equals To ( == )
6. Not Equals To ( != )

Example 6:

```python
#returns true if statement is True else returns False
print("6 < 8 ", 6 < 8)
print("8 < 6 ", 8 < 6)
print("6 <= 6 ", 6 <= 6)
print("6 <= 3 ", 6 <= 3)
print("5 > 6 ", 5 > 6)
print("6 > 5 ", 6 > 5 )
print("6 >= 6 ", 6 >= 6)
print("6 >= 5 ", 6 >= 5)
print("6 == 6 ", 6 == 6)
print("6 == 7 ",6 == 7)
print("6 != 6 ", 6 != 6)
print("6 != 7 ", 6 != 7)
```

6 < 8 True

8 < 6 False

6 <= 6 True

6 <= 3 False

5 > 6 False

6 > 5 True

6 >= 6 True

6 >= 5 True

6 == 6 True

6 == 7 False

6 != 6 False

6 != 7 True

## Logical Operators <a href="#logical-operators" id="logical-operators"></a>

1. and
2. or
3. not

Negative/Positive integers are tru&#x65;**,** 0 is false

![](https://2666241650-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSc84l98ozg0PCtzUMg%2F-MRi2g_rxq_BaTauwnde%2F-MRi35dkwUHVIesQl3ta%2FAnd-Or.PNG?alt=media\&token=4c32c4b8-5204-4cfa-9bf5-a93b6b684b75)

**And**

If x is false, return x&#x20;

else return y

Eg:

```python
print( 5>7 and 6-5*3+6)
print( 5<7 and 6-5*3+6)
print( 5<7 and 6-4*3+6)
print( 6-4*3+6 and 5<7)
print( 6-4*3+6 and 5>7)
```

False

-3

0

0

0

**Or**

If x is false, return y

else x

Example 7:

```python
print( 6-4*3+6 or 6 < 5)
print( 6-4*3+6 or 6 > 5)
print( 6 > 5 or 6-4*3+6)
print( 6 < 5 or 6-4*3+6)
print( 6 < 5 or 16-4*3+6)

print( not True )
```

False

True

True

0

10

False

Example 8:

```python
t = True
f = False 
print("X\tY\tand\tor")
print("{}\t{}\t{}\t{}".format(t, t, t and t, t or t))
print("{}\t{}\t{}\t{}".format(t, f, t and f, t or f))
print("{}\t{}\t{}\t{}".format(f, t, f and t, f or t))
print("{}\t{}\t{}\t{}".format(f, f, f and f, f or f))
```

X Y and or

True True True True

True False False True

False True False True

False False False False

Example 9:

```python
print("X\tnot")
print("{}\t{}".format(True,not True))
print("{}\t{}".format(False,not False))
```

X not

True False

False True

## Assignment Operators <a href="#assignment-operators" id="assignment-operators"></a>

Example 10:

```python
x = 'something'
print(x)
```

something

Example 11:

```python
k = ( (6*5/2-5) and (6<4 or 3 > 6) ) or (10/2*3-1)
print(k)
```

14.0

Example 12:

```python
x = 6+7
print(x)
x,y = 8,7
l = ['hi','hello','how are you']
a,b,c = l
print(x,y,a,b,c)
```

13

8 7 hi hello how are you

Example 13:

```python
x = 5
y = 6
x,y=y,x
print(x,y)
a = 4
b = 6
c  = 10
a,b,c=a+b,a-b,c*b
print(a,b,c)
```

6 5

10 -2 60

## Membership Operator <a href="#membership-operator" id="membership-operator"></a>

Example 14:

```python
# in , not in 
s2 = "Dog is an animal."
s1 = "Dog"
x = s1 in s2
if x :     
 print("Pattern Found in step 1")   #return this if x=True
else : print("Patten Not Found")
p = s1 not in s2

print(x)
print(p)
```

Pattern Found in step 1

True

False

Example 15:

```python
l1 = [ 1,2,6,5,[3,4]]
l2 = [ 3,4]
if l2 in l1 :
    print("l2 found in l1")
else :
    print("No Pattern Found")
```

l2 found in l1

Example 16:

```python
l = [ 1,2,6,324,23,4234,54,2342,3432]
if int(input("Enter a number : ")) in l :   #takes input and convert it into int print("Number found in list")
    print("Number matched")
else :
    print("No such number in our list")
```

Enter a number : 324

Number matched

## Identity Operator <a href="#identity-operator" id="identity-operator"></a>

Example 17:

```python
#is or is not
x = 5
y = 5
print(x is y)
print( x is not y )
p = 3
q = 4
if p is q :
    print("Both are equal")
else :
    print("Both are different")
```

\
True

False

Both are different
