PYTHON
OPERATORS
Outline
Operators
Need of operators
Types of Python Operators
Python Programming
Operators…
Python Programming
python operator
A special symbol that denotes
an operation
Operands……..
• Operands are the values or objects on which operations are
performed
>>> a + b
>>> x * y
>>> a + 10
>>> 100 + 200
Python Programming
Operators vs Operands……..
Operands
a + b
Operator
Python Programming
Operators
• Depending on the number of operands involved in the
operation, operators are categorized.
Operators
Unary Binary Ternary
Python Programming
Python Operators…
Python Programming
Logical
Membership Arithmetic
Operators
Bitwise Relational
Assignment
Identity
Python Programming
Arithmetic Operators…
Python Programming
Operators: Arithmetic
Operation Operator Usage Result
Addition +
Subtraction -
Multiplication *
Division /
Integer division //
Remainder %
Exponent **
Python Programming
Operators: Arithmetic
Let a = 20 and b= 3
Operation Operator Usage Result
Addition + a+b 23
Subtraction - a–b 17
Multiplication * a*b 60
Division / a/b 6.667
Integer division //
Remainder %
Exponent **
Python Programming
Operators: Arithmetic
Let a = 20 and b= 3
Operation Operator Usage Result
Addition + a+b 23
Subtraction - a–b 17
Multiplication * a*b 60
Division / a/b 6.667
Integer division // a // b 6
Remainder %
Exponent **
Python Programming
Operators: Arithmetic
Let a = 20 and b= 3
Operation Operator Usage Result
Addition + a+b 23
Subtraction - a–b 17
Multiplication * a*b 60
Division / a/b 6.667
Integer division // a // b 6
Remainder % a%b 2
Exponent **
Python Programming
Operators: Arithmetic
Let a = 20 and b= 3
Operation Operator Usage Result
Addition + a+b 23
Subtraction - a–b 17
Multiplication * a*b 60
Division / a/b 6.667
Integer division // a // b 6
Remainder % a%b 2
Exponent ** a ** b 8000
The type of value returned is dependent on the operator and
operands
Python Programming
Relational Operators…
Python Programming
Operators: Relational
• Used to check conditions (compare expressions)
• General form:
<expr1> <operator> <expr2>
Examples:
>>> 100 > 20
>>> ( 100 + 20 ) < ( 200 * 2 )
>>> a != b
• Comparisons yield Boolean values: True or False.
Python Programming
Operators: Relational
Let a = 1 and b =2
Comparison Operator Usage Result
Equal to == a == b False
Not equal to != a != b True
Greater than > a>b False
Greater than or equal to >= a >= b False
Less than < a<b True
Less than or equal to <= a <= b True
Python Programming
Logical Operators…
Python Programming
Operators: Logical
• Combines two or more conditions
• General form:
<Cond1> <operator> <Cond2>
• Returns Boolean value: True or False
Python Programming
Operators: Logical
Let a =10, b=20, c= 30
Description Operator Usage Result
Returns True if both conditions and (a>b) and (a>c) False
are true
Returns True if one of the or (a>b) or (c>a) True
conditions is true
Flips the result, returns False if not not (b>a) False
the expression is true
Python Programming
Operators: Chaining Relational
• Relational operators can be chained arbitrarily.
• General form:
<exp1> <op1> <exp2> <op2><exp3><op3>< exp4>…
• Example:
a>b>c
• equivalent to (a>b) and (b>c)
• Each expression is evaluated only once
• a and c are never compared
Python Programming
Bitwise Operators…
Python Programming
Operators: Bit-wise Operators
• Operate on bits
• Treats each operand as a string of binary digits.
• Constant 2 if used as operand is treated as binary “10” and operand 5 is
treated as binary “101”
Python Programming
Operators: Bitwise
Operation Operator Usage Result
NOT ~
AND &
OR |
XOR ^
Left-Shift <<
Right-Shift >>
Python Programming
Operators: Bitwise NOT
Let a = 10
Binary:
a = 0000 1010
Operation Operator Usage Operation
NOT ~ ~a
= ~10 a: 0000 1010
---------------
= -11 ~a:1111 0101
~a:1111 01 The negative sign indicates that it's represented as a two's
complement binary number
Python Programming
Operators: Bitwise AND
Let a = 10 and b = 5
Binary:
a = 0000 1010
b = 0000 0101
Operation Operator Usage Operation
AND & a&b a: 0000 1010
= 10 & B b: 0000 0101
---------------------
=0 a &b: 0000 0000
Python Programming
Operators: Bitwise OR
Let a = 10 and b = 5
Binary:
a = 0000 1010
b = 0000 0101
Operation Operator Usage Operation
OR | a|b
a: 0000 1010
= 10 | B b: 0000 0101
---------------------
= 15 a &b: 0000 1111
Python Programming
Operators: Bitwise XOR
Let a = 10 and b = 5
Binary:
a = 0000 1010
b = 0000 0101
Operation Operator Usage Operation
XOR ^ a^b
a: 0000 1010
= 10 ^ 5 b: 0000 0101
---------------------
= 15 a^b: 0000 1111
Python Programming
Operators: Bit-wise shift
• Left-shift (<<)
• Right-shift (>>)
• Shifts the number by n bits
• General form:
• number >> n
• number << n
• Shift operators preserves sign-bit
Python Programming
Operators: Bitwise left-
shift
Let a = 10
Binary:
a = 0000 1010
Operation Operator Usage Operation
Left-shift << a << 2 a: 0000 1010
= 10 << -----------------------
2 a << 2: 0010 1000
= 40
In case of shift operators sign bit is
preserved
Python Programming
Operators: Bitwise left-
shift
Let a = 10
Binary:
a = 0000 1010
Operation Operator Usage Operation
Right- >> a >> 2 a: 0000 1010
shift =10>>2 -----------------------
=2 a >> 2: 0000 0010
Python Programming
Operators: Bit-wise shift
• number >> n : number / 2n
• number << n : number * 2n
Example:
20 >> 2 = 5
This is equivalent to 20 / 22
20 << 2 = 20
This is equivalent to 20 * 22
Python Programming
Assignment Operators…
Python Programming
Operators: Assignment
• Used to assign values to variables
• “ = “ is the python assignment operator
• General form:
<variable> = <value>
<variable> = <variable>
<variable> = <expression>
• Examples:
• a = 100
•c = a
• b=a+1
Python Programming
Operators: Multi-variable Assignment
Multi-variable assignment with same value
<variable 1> = <variable 2>=<variable n> = <value>
• Example:
• a = b = c = 100
Python Programming
Operators: Multi-variable Assignment
Multi-variable Assignment with different values
General form1:
<var 1>, <var 2>…<var n> = <val 1>,<val 2>..<valn>
Example:
a , b , c = 100, 200, 300
General form2:
<var1> = <val1>; <var2> = <val2>; ..<var n>=,..<valn>
Example:
a = 10; b = 20; c = 30
Python Programming
Operators: Short-hand Assignment
It is possible to mix few of the python
operators with assignment
Example:
a=a+b Results in
a += b Same value
Python Programming
Operators: Short-hand
Assignment
Operator Usage Equivalent Operation
+=
-=
*=
/=
%=
//=
**=
&=
|=
^=
>>=
<<=
Python Programming
Operators: Short-hand
Assignment
Operator Usage Equivalent Operation
+= A += B A=A+B
-= A -= B A=A–B
*= A *= B A=A*B
/= A /= B A=A/B
%= A %= B A=A%B
//= A //= B A = A // B
**= A **= B A = A ** B
&= A &= B A=A&B
|= A |= B A=A|B
^= A ^= B A=A^B
>>= A >>= 10 A = A >> B
<<= A <<= 10 A = A << B
Python Programming
Identity Operators…
Python Programming
Operators: Identity
Compares the identity of two objects
>>>is
>>>not is
General form:
<object1> operator <object2>
Returns Boolean values: True or False
Every object created in python is given a unique integer id
Method id() returns this unique id
Python Programming
Operators: Identity
Operator Usage Description
is a is b Returns True; if a and b have same id
Returns False; Otherwise
not is a not is b Returns False; if a and b have same id
Returns True; Otherwise
Python Programming
Operators: Identity
Example:
>>> a = 300 a 300
>>> b = a b
>>> a is b # returns True
Python Programming
Operators: Identity
python interpreter creates a cache of integer objects in the
range [-5, 256] at the beginning
>>> a =100
>>> b= 100
>>> a is b # returns True
Hence, a and b are the names given to the same object.
Python Programming
Comparison: is and ==
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> result1 = x is y # Checks if 'x' and 'y' refer to the same
object (False)
>>> result2 = x == y # Checks if 'x' and 'y' have the same
value (True) print(result1) print(result2)
Python Programming
Membership Operators…
Python Programming
Membership Operator
Operators: Membership
Checks the membership of an element in a sequence
>>> in
>>> not in
Returns Boolean values: True or False
Python Programming
Operators: Membership
Operator Usage Description
in <val> in <seq> Returns True; if a is part of the
Sequence and False Otherwise
not in <val> not in <seq> Returns False; if a is part of the
Sequence and True Otherwise
Python Programming
Operators: Membership
• Membership operators with Lists
>>> a =10
>>> L = [1, 3, 5, 7, 10, 12]
Example:
>>> a in L # True
>>> a not in L # False
Python Programming
Operators: Membership
• Membership operators with
Tuples
>>> a =10
>>> T = (1, 3, 5, 7, 10, 12)
Example:
>>> a in T # True
>>> a not in T # False
Python Programming
Operators: Membership
• Membership operators with Dictionary
>>> a =10
>>> d = {1:10, 3:30, 5:50, 7:70}
Example:
>>> a in d # False; checks for the presence of a as key
>>> 1 in d # True
Python Programming
Operators: Membership
• Membership operators with Sets
>>> a =10
>>> d = {1, 3, 5, 7, 10 }
Example:
>>> a in d # True
>>> 1 in d # True
Python Programming