# Python Operators

Expression :

- An expression is anything which evaluates to something.
- Expressions are combinations of operators and operands.

Operator :

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.

**Operator's Categories:**

- Unary : A unary operator is an operator, which operates on one operand.
- Binary : A binary operator is an operator, which operates on two operands.

**TYPES OF OPERATOR :-**

Arithmetic Operators.

Assignment Operators.

Relational Operators.

Logical Operators.

Bitwise Operators.

Membership Operators.

Identity Operators.

1 . Arithmetic Operator :

Arithmetic operator in Python behaves as one would expectOperator | Description |
---|---|

+ | To perform addition |

- | To perform subtraction |

* | To perform multiplication |

/ | To perform division |

% | To return remainder after division(Modulus) |

// | Floor division(gives integer value after division) |

** | To calculate power |

Example :

>>> 5 + 10 15 >>> 10 - 5 5 >>> 2 * 5 10 >>> 13/5 2.6 >>> 13//5 2 >>> 14 % 4 2 >>> 2 ** 3 8

Compound expression:

3*x+5-7principle + (principle + interest)

2 . Assignment Operator

- The assignment operator has the lowest precedence of all operator.
- It is always evaluated last.
- Assignment can be used to assign the value of an expression to a variable.
- In assignment, the previous value of the variable is overwritten by the value of the expression.
**variable = expression****Example:**

x = x+10

isVisible = true;

timeInSecond = distance/speedOfLight;

Operator | Description |
---|---|

= | Assignment |

+= | Add and assign |

-= | Subtract and Assign |

*= | Multiply and assign |

/= | Divide and Assign |

%= | Modulus and assign |

//= | Floor division and assign |

**= | Exponent and assign |

# 1. Add Assignment +=>>> a = 20; # add 5 to a >>> a += 5 #similar toa = a + 5 >>> print(a) [OUTPUT : 25] -----------------------------# 2. Multiply Assignment *=>>> a = 20; # multiply 5 to a >>> a *= 5 #similar toa = a * 5 >>>print(a) [OUTPUT : 100] -----------------------------Similarly, we have-=, /=, %=, <<=, >>=, &=, ^=, |=

3 . Relational Operator

Relational Operators are used to compare two values. Relational expression always evaluates result as boolean either**True**or

**False**.

# 1. Greater Than: >>>> a, b = 5, 10 >>> ( a > b ) [Result: False] -----------------# 2. Less Than: <>>> a, b = 5, 10 >>> ( a < b ) [Result: True] -----------------# 3. Equal To: ==>>> a, b = 5, 5 >>> ( a == b ) [Result: True] ------------------# 4. Not Equal To: !=>>> a, b = 5, 2 >>> ( a != b ) [Result: True] --------------------# 5.Greater Than Equal: >=>>> a, b = 10, 5; >>> ( a >= b ) [Result: True] -----------------# 6.Less Than Equal: <=>>> a, b = 10, 5; >>> ( a <= b ) [Result: False]

4 . Logical Operator

**Logical Expression**

An expression whose value is a boolean type ( True or False ).

Logical operators are used when we want to compare more than one relation at a time.

**Logical operators:**

**Logical AND ( and ):**

**exp1 and exp2**

*Logical AND*.**True**when both expressions are True.

Logical AND**False**when one of the expression is False

**Truth Table:**Exp1 Exp2 Result True True True True False False False True False False False False **Logical OR ( or ):**

**exp1 or exp2**

*Logical OR*.**True**when one of the expression is True.

Logical OR**False**when when both expressions are False

**Truth Table:**Exp1 Exp2 Result True True True True False True False True True False False False **Logical NOT ( not )**

**not expr**

It flips the result.

*Logical Not is*.**True**when the expression is False.

Logical NOT is**False**when the expression is True**Truth Table:**Expr Result True False False True

Example:

# 1. Logical AND ( and )>>> x = 6 >>> b = (x > 5) and (x < 10) >>> print( b ) [OUTPUT: True] >>> x = 6 >>> b = (x > 5) and (x != 6) >>> print( b ) [OUTPUT: False] -------------------------# 2. Logical OR ( or )>>> x = 6 >>> b = (x > 5) or (x != 6) >>> print( b ) [OUTPUT: True] -------------------------# 3. Logical NOT ( not )>>> x = 6 >>> b = !(x > 5) >>> print( b ) [OUTPUT: False] -------------------------

5 . Bitwise Operator :

Bitwise operators performs operations on bits.AND ( & ), OR ( | ), XOR ( ^ ), NOT ( ~ ).

**Truth Table:**

----------------------------------A B A&B A|B A^B ~A---------------------------------- 0 0 0 0 0 1 ---------------------------------- 0 1 0 1 1 1 ---------------------------------- 1 0 0 1 1 0 ---------------------------------- 1 1 1 1 0 0 ----------------------------------

**Example :**

>>> a = 6 >>> b = 3 >>> r1 = a & b >>> r2 = a | b >>> r3 = a ^ b >>> r4 = ~a >>> print(r1,r2,r3,r4) [OUTPUT:2, 7, 5, -7 ]DESCRIPTIONa = 0000 0110 b = 0000 0011 --------------------- a&b = 0000 0010 = 2 --------------------- a|b = 0000 0111 = 7 --------------------- a^b = 0000 0101 = 5 --------------------- ~a = 1111 1001 = -7 --------------------- In ~a the first bit(Most significant bit ) is 1 that represents number is negative, so for negative values system evaluates 2s compliment of the bit and print magnitude with minus (-) symbol. So, ~6 = 1111 1001 1s 0f ~6 = 0000 0110 +1 --------------------- 2s of ~6 = 0000 0111 = 7 ---------------------- so, result is -7.

Shift Operator (<<, >>) :

Shift operator are used to shift the bits of a number towards left or right.**1.**Left Shift Operator :

**<<**

**2.**Right Shift Operator :

**>>**

#### Syntax :

**n ShiftOperator t**

Here :

**n**: Perform shift on n's bits.

**t**: number of times.

Example:

#1. Left Shift ( << )>>> x = 6 << 2 >>> print(x) [OUTPUT :24] -----------------------------------# 2. Right Shift ( >> )>>> x = 6 >> 2 >>> print(x) [OUTPUT :1] -----------------------------------DESCRIPTION :6 << 2 means :We need to perform left shift on bits of 6, 2 times 6 = 0000 0110 first time = 0000 1100 second time = 0001 1000 = 246 >> 2 means :We need to perform right shift on bits of 6, 2 times 6 = 0000 0110 first time = 0000 0011 second time = 0001 0001 = 1

6. Membership Operators (in, not in) :

Membership operators are used to test if a sequence is presented in an object:

`in`

and `not in`

are the membership operators in Python.
They are used to test whether a value or variable is found in a sequence (for e.g.: string, list, tuple, set and dictionary).

In dictionary we can only test for presence of key, not the value.

Operator | Description |
---|---|

in | Returns 'True' if value/variable is found in the sequence |

not in | Returns 'True' if value/variable is not found in the sequence |

>>> a=10 >>> b=20 >>> list=[10,20,30,40,50] >>> a in list True OUTPUT >>> b not in list False OUTPUT

7. Identity Operators (is, is not) :

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

`is`

and `is not`

are the identity operators in Python.

They are used to check, two values (or variables) are located on the same memory or not.

Two variables that are equal does not imply that they are identical.

Operator | Description |
---|---|

is | Returns 'True' if both variables are the same object |

is not | Returns 'True' if both variables are not the same object |

>>> a = 10 >>> b = 10 >>> a is b True >>> a is not b False >>>

Next chapter is id() and type() functions in python

## Video Lecture

### Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like

C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at **info@prowessapps.in**

### Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at **info@prowessapps.in**