# 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:
1. Unary : A unary operator is an operator, which operates on one operand.
2. Binary : A binary operator is an operator, which operates on two operands.

TYPES OF OPERATOR :-
1. Arithmetic Operators.

2. Assignment Operators.

3. Relational Operators.

4. Logical Operators.

5. Bitwise Operators.

6. Membership Operators.

7. Identity Operators.

1 . Arithmetic Operator :

Arithmetic operator in Python behaves as one would expect
OperatorDescription
- 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-7
principle + (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;
OperatorDescription
= Assignment
-= Subtract and Assign
*= Multiply and assign
/= Divide and Assign
%= Modulus and assign
//= Floor division and assign
**= Exponent and assign
```# 1. Add Assignment +=
>>> a = 20;
>>> a += 5 # similar to  a = a + 5
>>> print(a)
[OUTPUT : 25]
-----------------------------
# 2. Multiply Assignment *=
>>> a = 20;
# multiply 5 to a
>>> a *= 5 # similar to a = 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:
1. 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:
Exp1Exp2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
2. 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:
Exp1Exp2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
3. 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:
ExprResult
TrueFalse
FalseTrue

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 ]

DESCRIPTION
a = 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 = 24

6 >> 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.

OperatorDescription
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.

OperatorDescription
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.

### Projects For Students

Students can mail requirement at info@prowessapps.in

#### CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824