2. OPERATORS IN PYTHON
10/11/2021 OPEARATORS IN PYTHON 2
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operator
Assignment Operator
Special Operator
3. ARITHMETIC
OPERATORS IN
PYTHON
Arithmetic
operators are used
to perform
mathematical
operations like
addition,
subtraction,
multiplication, etc.
10/11/2021 OPEARATORS IN PYTHON 3
Operator Description Syntax
+ Add two operands or unary plus x+y+2
- Subtract right operand from the
left or unary minus
x-y-2
* Multiply two operands x*y
** Exponent - left operand raised to
the power of right
x**y
/ Divide left operand by the right
one (always results into float)
x/y
// Floor division - division that
results into whole number
adjusted to the left in the number
line
x//y
% Modulus - remainder of the
division of left operand by the
right
x%y
5. COMPARISION
OPERATORS IN
PYTHON
Comparison
operators are used
to compare values.
It returns either
True or False
according to the
condition.
10/11/2021 OPEARATORS IN PYTHON 5
Operator Description Syntax
> Greater than - True if left operand
is greater than the right
x>y
< Less than - True if left operand is
less than the right
y<x
== Equal to - True if both operands
are equal
x==y
!= Not equal to - True if operands
are not equal
x!=y
>= Greater than or equal to - True if
left operand is greater than or
equal to the right
x>=y
<= Less than or equal to - True if left
operand is less than or equal to
the right
x<=y
7. BITWISE
OPERATORS IN
PYTHON
Bitwise operators
act on operands as
if they were strings
of binary digits.
They operate bit by
bit, hence the.
10/11/2021 OPEARATORS IN PYTHON 7
Operator Description Syntax
& Bitwise and x & y
| Bitwise or x|y
~ Bitwise not ~x
^ Bitwise XOR x ^ y
>> Bitwise right
shift
x>>y
<< Bitwise left shift x<<y
8. Truth Table
AND
OR
Ex-OR
A B A &B
0 0 0
0 1 0
1 0 0
1 1 1
A B A |B
0 0 0
0 1 1
1 0 1
1 1 1
A B A |B
0 0 0
0 1 1
1 0 1
1 1 0
11. Not
operator
Bitwise (Not) one's compliment operator will
invert the binary bits.
If a bit is 1, it will change it to 0.
If the bit is 0, it will change it to 1.
The ones' complement binary numeral system
is characterized by the bit complement of any
integer value being the arithmetic negative of
the value.
13. LOGICAL
OPERATORS IN
PYTHON
Logical operators
are and,or & not
operators. It returns
either True or False
according to the
operator.
Practical approach :
Comparision
operators
execution with
output
10/11/2021 OPEARATORS IN PYTHON 13
Operator Description Syntax
and True if both the operands are true x and y
or True if either of the operands is
true
x or y
not True if operand is false
(complements the operand)
x not y
14. ASSIGNMENT
OPERATORS IN
PYTHON
Assignment operators
are used to assign
values to variables.
There are various
compound operators
in Python like a+=5
that adds to the
variable and later
assigns the same. It is
equivalent to a=a+5
10/11/2021 OPEARATORS IN PYTHON 14
Operator Example Equivalent to
= x=7 x=7
+= x+=7 x=x+7
-= x-=7 x=x-7
*= x*=7 x=x*7
/= x/=7 x=x/7
//= x//=7 x=x//7
%= x%=7 x=x%7
**= x**=7 x=x**7
&= x&=7 x=x&7
|= x|=7 x=x|7
^= x^=7 x=x^7
>>= x>>=7 x=x>>7
<<= x<<=7 x<<7
15. Special Operators
Identity operator
is and is not are the identity
operators in Python. They are used
to check if two values (or
variables) are located on the same
part of the memory.
Membership
Operator
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
(string,list,tuple,set and
dictionary)
16. Difference between is and == operator
The is keyword is used to test if
two variables refer to the same
object.
The test returns True if the two
objects are the same object.
The test returns False if they
are not the same object, even if
the two objects are 100%
equal.
Use the == operator to test if
two variables are equal.