-
-
Save IndhumathyChelliah/3ec659580448e29c4cc7b2d0d3095c07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student: | |
def __init__(self,name,rollno,age): | |
self.name=name | |
self.rollno=rollno | |
self.age=age | |
def __str__(self): | |
return "{}-{}-{}".format(self.name,self.rollno,self.age) | |
def __add__(self,other): | |
return (self.age+other.age) | |
def __sub__(self,other): | |
return (self.age-other.age) | |
def __mul__(self,other): | |
return (self.age*other.age) | |
def __truediv__(self,other): | |
return (self.age/other.age) | |
def __floordiv__(self,other): | |
return (self.age//other.age) | |
def __mod__(self,other): | |
return (self.age%other.age) | |
def __pow__(self,other): | |
return (self.age**other.age) | |
def __lshift__(self,other): | |
return (self.age<<other.age) | |
def __rshift__(self,other): | |
return (self.age>>other.age) | |
def __and__(self,other): | |
return (self.age&other.age) | |
def __xor__(self,other): | |
return (self.age^other.age) | |
def __or__(self,other): | |
return (self.age|other.age) | |
s1=Student("karthi",12,7) | |
s2=Student("Sarvesh",15,3) | |
#when trying to add two class objects,magic method __add__() is invoked. | |
#It will age attribute of two Student objects according to the behavior mentioned in the method __add__() | |
print (s1+s2)#Output: 10 | |
#when trying to subtract two class objects,magic method __sub__() is invoked | |
print (s1-s2)#Output:4 | |
#when trying to multiply two class objects,magic method __mul__() is invoked | |
print (s1*s2)#Output:21 | |
#when trying to divide two class objects,magic method __truediv__() is invoked | |
print (s1/s2)#Ouput:2.3333333333333335 | |
#when trying to floor division operation on two class objects,magic method __floordiv__() is invoked | |
print (s1//s2)#Output:2 | |
#when trying to perform modulus operation on two class objects,magic method __mod__() is invoked | |
print (s1%s2)#Output:1 | |
#when trying to do power of operation on two class objects,magic method __pow__() is invoked | |
print (s1**s2)#Output:343 | |
#when trying to do leftshift operation on two class objects,magic method __lshift__() is invoked | |
print (s1<<s2)#Outptu:56 | |
#when trying to do rightshift operation on two class objects,magic method __rshift__() is invoked | |
print (s1>>s2)#Output:0 | |
#when trying to do & operation on two class objects,magic method __and__() is invoked | |
print (s1&s2)#Output:3 | |
#when trying to do ^ operation on two class objects,magic method __xor__() is invoked | |
print (s1^s2)#Output:4 | |
#when trying to do | operation on two class objects,magic method __or__() is invoked | |
print (s1|s2)#Output:7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment