3. Operators are functions that perform some
logical calculations.
Operators manipulate the value of the
operands.
Python supports several kinds of operators.
6. Arithmetic operators are used with numeric
values to perform common mathematical
operations.
Arithmetic operators are used for Addition,
subtraction, multiplication and division and so
on.
Let us discuss them with an example.
7. Operator Description Example
Addition(+) Add values on either side of the operator A+B =9
Subtraction(-) Subtracts A from B A-B =3
Multiplication(*) Multiplies A with B A*B = 18
Division(/) Divides the denominator with numerator A/B =2
Modulus(%)
Performs the division operator and returns the
remainder
A%B=0
Exponent(**) Performs exponential calculation on operators
A**B= 6 power 3
=216
9. Let us assume a = 3 , b =5
Operator Description Example
Equal to (==) This operation returns true if two values are equal A==B returns False
Not equal to
(!=)
If the two operands are not equal this returns true A!=B returns true
Greater than
(>)
If the value of the left operand is greater than right this returns
true
A>B returns false
Less than (<)
This operator true if the left operand is less than the right
operand
A< B returns true
Greater than
or equal to
(>=)
This operator returns true if the left operand is greater than or
equal to the right operand
A>=B returns false
Less than or
equal to (<=)
This operator returns true if the left operand is less than or equal
to the right operand
A<=B returns True
10. The Identity operators are used to check if both
the operands reference the same object
memory.
Operator Description Example
is
Returns true if both variables are the
same object
x is y
is not
Returns true if both variables are not
the same object
x is not y
The is not operator is opposite of is operator.
11. Let us take some examples
A = 10
B = 10
A is B #will return True because both are referencing the same
memory address.
A is not B #will return False because both are referencing the same
memory address.
12. Logical operators are used for the logic
condition ( true (or) false).
We have three types of logical operators.
13. Let a = 1, b = 2
Operator Example Meaning Result
And a and b If a is False returns
a
Otherwise returns b
2
Or A or b If a is false returns
b
Otherwise returns a
1
Not Not a If a is False returns
True
Otherwise returns
False
False
14. Python Bitwise operators are used to perform
bit operations.
All the decimal values will be converted into
binary values (sequence of bits i.e 0100, 1100,
1000, 1001 etc). Next, Python bitwise operators
will work on these bits such as shifting them
left to right or converting bit value from 0 to 1
etc.
15. Let us discuss with an example A =30, B= 23
These two numbers written in binary format as
A = 30 = 0001 1110, B = 23 = 0001 0111.
Operator Description Example
And (&)
If both the inputs are True the result is True else the
result is False
A&B =22 (0001 0110)
Or(|)
If both the inputs are False the result is zero else the
result is True
A|B= 31(0001 1111)
Xor(^)
If both the bits are same the result is False else the
result is True
A^B =9(0000 1001)
Compliment(~) If the input is True the output is False and vice-versa A~B =-31(1000 1111)
17. The membership operators are classified into two types.
A={1 ,2,3,4,5}
Operator Description Example
Is
It returns true if the
value is found in the
sequence
5 in A returns True
10 in A returns False
Not in
True if the variable is
not found in the
sequence
5 not in A
returns False
10 not in
A returns True
18. When there is several operations to be performed, operator precedence
determines which operator would be given importance over others.
**
~+/~-
*/%//
+-
>><<
&
^|
<=< >>=
<>==!=
= -= +=//=%=/=*=*==
Is, is not
In in not
Not or and