Aks Python 08-12
Aks Python 08-12
class Book:
def __init__(self, title, author, copies):
self.title = title
self.author = author
self.copies = copies
def getCopies(self):
return self.copies
In [3]:
class LogicalValue:
def __init__(self, value):
self.value = value
def __invert__(self):
return LogicalValue(not self.value)
def __str__(self):
return str(self.value)
trueValue = LogicalValue(True)
falseValue = LogicalValue(False)
In [4]:
class Matrix:
def __init__(self, elements):
self.elements = elements
self.rows = len(elements)
self.cols = len(elements[0])
result_elements = [
[self.elements[i][j] + other.elements[i][j] for j in range(self.cols)]
for i in range(self.rows)
]
return Matrix(result_elements)
def display(self):
for i in range (0, 3):
for j in range (0, 3):
print(self.elements[i][j], end=' ')
print()
result_elements = [
[self.elements[i][j] - other.elements[i][j] for j in range(self.cols)]
for i in range(self.rows)
]
return Matrix(result_elements)
print("Matrix 1:")
matrix1.display()
print("\nMatrix 2:")
matrix2.display()
print("\nMatrix Addition:")
result_addition = matrix1 + matrix2
result_addition.display()
print("\nMatrix Subtraction:")
result_subtraction = matrix1 - matrix2
result_subtraction.display()
Matrix 1:
5 6 3
7 9 2
8 6 1
Matrix 2:
0 8 6
1 7 1
9 7 5
Matrix Addition:
5 14 9
8 16 3
17 13 6
Matrix Subtraction:
5 -2 -3
6 2 1
-1 -1 -4
In [ ]: