# 7 Python Operators with examples

In C, C++ languages like python also have 7 types of operators

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

1. Arithmetic Operators :

Arithmetic Operators are like addition, subtraction, division, multiplication, Modular, and Floor Division.

Example:

` ` ``` a= 5 b= 4 print('a+b =', a+b) print('a-b =', a-b) print('a*b =', a*b) print('a/b =', a/b) print('a%b =', a%b) print('a//b =', a//b) ``` ` ` ` `

Output:

```a+b = 9
a-b = 1
a*b = 20
a/b = 1.25
a%b = 1
a//b = 1```

2. Comparison Operations:

Comparison Operators compare both sides of the values. It will produce true and false results. The operators ==,!=, >, <, <>, <=,>=.

Example:

```a= 5
b= 4
print('a==b =', a==b)
print('a!=b =', a!=b)
print('a>b =', a>b)
print('a<b =', a<b)
print('a>=b =', a>=b)
print('a<=b =', a<=b)```

Output:

```a==b = False
a!=b = True
a>b = True
a<b = False
a>=b = True
a<=b = False```

3. Assignment Operators:

These operators are used to assign values to variables. You can do arithmetic operations while assigning variables also.

Example:

` ` ``` a= 5 b= 4 c=0 print('C value', c) c+=a print('C value', c) c*=a print('C value', c) c-=a print('C value', c) c/=a print('C value', c) c%=a print('C value', c) c//=a print('C value', c) c**=a print('C value', c) ``` ` ` ` `

Output:

```C value 0
C value 5
C value 25
C value 20
C value 4.0
C value 4.0
C value 0.0
C value 0.0```

Python Logical Operators:

In python we have 3 logical operators are there.  And, Or, Not –  We can use these operators with multiple statement comparisons

And  –  If both the statements true it returns, true value

Or  – if any one of the statement false it returns true value

Not – It is used to reverse the result with true to false or false to true

Example:

` ` ``` a= 5 b= 4 c=7 print('a>b and c>b =', a>b and c>b) print('a>b or c>b =', a>b or c>b) print('not(a>b or c>b) =', not(a>b and c>b)) ``` ` ` ` `

Output:

```a>b and c>b = True

a>b or c>b = True

not(a>b or c>b) = False```

### Python Identity Operators:

We have 2 identity operators in python. We can use them to compare variable values. If the value of variable is equal it returns true value, otherwise it returns false value.

Is  –  Returns true value if the both variables have same object

Is not – Returns true value if the both variables have not same object

Example 1:

` ` ``` a= 5 b= 4 print('a is b', a is b) print('a is not b =', a is not b) ``` ` ` ` `

Output:

```a is b False

a is not b = True```

Example 2:

` ` ``` a= 5 b= 5 print('a is b', a is b) print('a is not b =', a is not b) ``` ` ` ` `

Output:

```a is b True

a is not b = False```

### Python Membership operators:

Python membership operators are used to test the variables are present in the sequences of tuples, lists, arrays and strings.  We have 2 membership operators.

In –  Returns true value, if the object present in sequence

Not in – Returns true value, if the object not present in sequence

Example 1:

` ` ``` a= 3 b= [2,3,5,8,9] print('a in b', a in b) print('a not in b =', a not in b) ``` ` ` ` `

Output:

```a in b True

a not in b = False```

Example 2:

` ` ``` a= 4 b= [2,3,5,8,9] print('a in b', a in b) print('a not in b =', a not in b) ``` ` ` ` `

Output:

```a in b False

a not in b = True```

### Python Bitwise Operators:

In python we have 6 bitwise operators – &, |, ^, ~, >>, <<.  It performed operation bit by bit between variables.

Example:

```a= 5
b= 5
print('a & b =', a & b)
print('a | b =', a | b)
print('a ^ b =', a ^ b)
print('a ~ b =',  a ~ b)
print('a<<b =',  a<<b)
print('a>>b =',  a>>b)```

Output:

```a & b = 5

a | b = 5

a ^ b = 0

a ~ b = -6

a<<b = 160

a>>b = 0``` 