Chapter 3
Chapter 3
Operato
Description Example
r
== Checks if the value of two operands are equal or not, if (a == b) is not true.
yes then condition becomes true.
!= Checks if the value of two operands are equal or not, if (a != b) is true.
values are not equal then condition becomes true.
<> Checks if the value of two operands are equal or not, if (a <> b) is true. This is
values are not equal then condition becomes true. similar to != operator.
> Checks if the value of left operand is greater than the (a > b) is not true.
value of right operand, if yes then condition becomes
true.
< Checks if the value of left operand is less than the (a < b) is true.
value of right operand, if yes then condition becomes
true.
>= Checks if the value of left operand is greater than or (a >= b) is not true.
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or equal (a <= b) is true.
to the value of right operand, if yes then condition
becomes true.
Operat
Description Example
or
& Binary AND Operator copies a bit to the (a & b) will give 12
result if it exists in both operands. which is 0000 1100
| Binary OR Operator copies a bit if it exists (a | b) will give 61
in either operand. which is 0011 1101
^ Binary XOR Operator copies the bit if it is (a ^ b) will give 49
set in one operand but not both. which is 0011 0001
~ Binary Ones Complement Operator is unary (~a ) will give -60
and has the effect of 'flipping' bits. which is 1100 0011
<< Binary Left Shift Operator. The left a << 2 will give 240
operands value is moved left by the which is 1111 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left a >> 2 will give 15
operands value is moved right by the which is 0000 1111
number of bits specified by the right
operand.
Operat
Description Example
or
and Called Logical AND operator. If both the (a and b) is true.
operands are true then then condition
becomes true.
or Called Logical OR Operator. If any of the two (a or b) is true.
operands are non zero then then condition
becomes true.
not Called Logical NOT Operator. Use to not(a and b) is false.
reverses the logical state of its operand. If a
condition is true then Logical NOT operator
will make false.
Operator Description
** Exponentiation (raise to the power)
~+- Ccomplement, unary plus and minus (method names for
the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += Assignment operators
*= **=
is is not Identity operators
in not in Membership operators
not or and Logical operators
9/8/2023 2:24 PM Dr. Dipankar Dutta, UIT, BU 1.11
Operator Overloading in Python
Operator Overloading means giving extended meaning beyond
their predefined operational meaning. For example operator + is
used to add two integers as well as join two strings and merge two
lists. It is achievable because „+‟ operator is overloaded by int class
and str class. You might have noticed that the same built-in
operator or function shows different behavior for objects of different
classes, this is called Operator Overloading.
class A:
def __init__(self, a):
self.a = a
print(ob1 + ob2)
print(ob3 + ob4)
9/8/2023 2:24 PM Dr. Dipankar Dutta, UIT, BU 1.17
Operator Overloading in Python
Output:
3
UITFor
class complex:
def __init__(self, a, b):
self.a = a
self.b = b
Ob1 = complex(1, 2)
Ob2 = complex(2, 3)
Ob3 = Ob1 + Ob2
print(Ob3)
9/8/2023 2:24 PM Dr. Dipankar Dutta, UIT, BU 1.19
Operator Overloading in Python
Output:
(3, 5)
class A:
def __init__(self, a):
self.a = a
def __gt__(self, other):
if(self.a>other.a):
return True
else:
return False
ob1 = A(2)
ob2 = A(3)
if(ob1>ob2):
print("ob1 is greater than ob2")
else:
print("ob2 is greater than ob1")
9/8/2023 2:24 PM Dr. Dipankar Dutta, UIT, BU 1.21
Operator Overloading in Python
Output:
ob2 is greater than ob1
ob3 = A(4)
ob4 = A(4)
print(ob1 == ob2)
– __neg__(self)
+ __pos__(self)
~ __invert__(self)
Output:
False
True
True
def Print(self):
print("Emp class called")
class Person(object):
# Constructor
def __init__(self, name):
self.name = name
Output:
UIT1 False
UIT2 True
# parent class
class Person(object):
def display(self):
print(self.name)
print(self.idnumber)
class B(A):
def __init__(self, roll):
self.roll = roll
object = B(23)
print(object.name)
9/8/2023 2:24 PM Dr. Dipankar Dutta, UIT, BU 1.53
Inheritance
Output :
Traceback (most recent call last):
File "/home/de4570cca20263ac2c4149f435dba22c.py", line 12, in
print (object.name)
AttributeError: 'B' object has no attribute 'name'
Hybrid Inheritance
class Base1(object):
def __init__(self):
self.str1 = “UIT1"
print("Base1")
class Base2(object):
def __init__(self):
self.str2 = “UIT2"
print("Base2")
def printStrs(self):
print(self.str1, self.str2)
ob = Derived()
ob.printStrs()
# Constructor
def __init__(self, name, age):
Base.__init__(self, name)
self.age = age
# To get name
def getAge(self):
return self.age
# Constructor
def __init__(self, name, age, address):
Child.__init__(self, name, age)
self.address = address
# To get address
def getAddress(self):
return self.address
# Driver code
g = GrandChild("Geek1", 23, "Noida")
print(g.getName(), g.getAge(), g.getAddress())
object1 = D()
Questions?