While working with Python or any other programming language always check the version in which you will be coding, as may be some behavior got changed.
Like in Python, there is change in the behavior of Division.
Prior to Python3, 5/2 used to give 2 & 5/2.0 or 5.0/2 or 5.0/2.0 will only give 2.5
But from Python3, 5/2 will give 2.5
So prior to Python3, you must give at least one operand as floating point to get the result as floating point, else Python truncates the result.
Like in Python, there is change in the behavior of Division.
Prior to Python3, 5/2 used to give 2 & 5/2.0 or 5.0/2 or 5.0/2.0 will only give 2.5
But from Python3, 5/2 will give 2.5
So prior to Python3, you must give at least one operand as floating point to get the result as floating point, else Python truncates the result.
===============================================================================================
Check below code to get the idea about Subscript, Superscript, Operation implementations using dunder methods, use of __call__(), use of one line if statement.....
class Polynomial:
def __init__(self, *coeffs):
self.coeffs = coeffs
# For subscript & superscript notations look for unicode list
def __repr__(self):
# return 'Polynomial (*{!r})'.format(self.coeffs) # Python 3+, only a & r work here
result = ''
result += f'{self.coeffs[0]}X\u00b2' if self.coeffs[0] != 0 else ""
val = not str(self.coeffs[1]).startswith('-') if self.coeffs[1] != 0 else ""
result += ('+'*val+f'{self.coeffs[1]}X')
val = not str(self.coeffs[1]).startswith('-') if self.coeffs[1] != 0 else ""
result += ('+'*val+f'{self.coeffs[2]}X')
if result == '':
result = '0'
return result
def __add__(self, other):
return Polynomial(*(x+y for x, y in zip(self.coeffs, other.coeffs)))
def __sub__(self, other):
return Polynomial(*(x-y for x, y in zip(self.coeffs, other.coeffs)))
def __mul__(self, other):
return 'Not Supported'
def __call__(self, *args):
if isinstance(args[0], Polynomial):
return 'Not Supported this way'
else:
return 'No such Operation is supported'
# X²+X+X
p1 = Polynomial(5, 3, 2)
p2 = Polynomial(2, 4, 3)
print(p1+p2)
print(p1-p2)
print(p1*p2)
print(p1(p2))
print(p1('7X²+7X+5X'))
print("You might recall a\u00b2 + b\u00b2 = c\u00b2 from your Math lessons, or you like Chemistry for H\u2082O.")
Check below code to get the idea about Subscript, Superscript, Operation implementations using dunder methods, use of __call__(), use of one line if statement.....
class Polynomial:
def __init__(self, *coeffs):
self.coeffs = coeffs
# For subscript & superscript notations look for unicode list
def __repr__(self):
# return 'Polynomial (*{!r})'.format(self.coeffs) # Python 3+, only a & r work here
result = ''
result += f'{self.coeffs[0]}X\u00b2' if self.coeffs[0] != 0 else ""
val = not str(self.coeffs[1]).startswith('-') if self.coeffs[1] != 0 else ""
result += ('+'*val+f'{self.coeffs[1]}X')
val = not str(self.coeffs[1]).startswith('-') if self.coeffs[1] != 0 else ""
result += ('+'*val+f'{self.coeffs[2]}X')
if result == '':
result = '0'
return result
def __add__(self, other):
return Polynomial(*(x+y for x, y in zip(self.coeffs, other.coeffs)))
def __sub__(self, other):
return Polynomial(*(x-y for x, y in zip(self.coeffs, other.coeffs)))
def __mul__(self, other):
return 'Not Supported'
def __call__(self, *args):
if isinstance(args[0], Polynomial):
return 'Not Supported this way'
else:
return 'No such Operation is supported'
# X²+X+X
p1 = Polynomial(5, 3, 2)
p2 = Polynomial(2, 4, 3)
print(p1+p2)
print(p1-p2)
print(p1*p2)
print(p1(p2))
print(p1('7X²+7X+5X'))
print("You might recall a\u00b2 + b\u00b2 = c\u00b2 from your Math lessons, or you like Chemistry for H\u2082O.")