5.operators Python
5.operators 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
Python Programming
Python Operators…
Python Programming
Logical
Membership Arithmetic
Operators
Bitwise Relational
Assignment
Identity
Python Programming
Arithmetic Operators…
Python Programming
Operators: Arithmetic
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
Python Programming
Operators: Relational
• Used to check conditions (compare expressions)
• General form:
<expr1> <operator> <expr2>
Examples:
>>> 100 > 20
>>> ( 100 + 20 ) < ( 200 * 2 )
>>> a != b
Python Programming
Logical Operators…
Python Programming
Operators: Logical
• General form:
<Cond1> <operator> <Cond2>
Python Programming
Operators: Logical
Let a =10, b=20, c= 30
Python Programming
Operators: Chaining Relational
• 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
Python Programming
Operators: Bitwise
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
Python Programming
Operators: Bitwise AND
Let a = 10 and b = 5
Binary:
a = 0000 1010
b = 0000 0101
Operation Operator Usage Operation
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 (>>)
• General form:
• number >> n
• number << n
Python Programming
Operators: Bitwise left-
shift
Let a = 10
Binary:
a = 0000 1010
Python Programming
Operators: Bit-wise shift
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
• 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
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>
Python Programming
Operators: Identity
Operator Usage Description
Python Programming
Operators: Identity
Example:
>>> a = 300 a 300
>>> b = a b
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
Python Programming
Comparison: is and ==
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
Python Programming
Membership Operators…
Python Programming
Membership Operator
Operators: Membership
Checks the membership of an element in a sequence
>>> in
>>> not in
Python Programming
Operators: Membership
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