0% found this document useful (0 votes)
10 views

Aks Python 08-12

The document defines a Matrix class to represent and perform arithmetic operations on matrices. It initializes two 3x3 matrices, matrix1 and matrix2, and uses the Matrix class methods to add and subtract the matrices by overriding the __add__ and __sub__ methods. The addition and subtraction results are then displayed.

Uploaded by

akshaysasi12.knr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Aks Python 08-12

The document defines a Matrix class to represent and perform arithmetic operations on matrices. It initializes two 3x3 matrices, matrix1 and matrix2, and uses the Matrix class methods to add and subtract the matrices by overriding the __add__ and __sub__ methods. The addition and subtraction results are then displayed.

Uploaded by

akshaysasi12.knr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [2]:

class Book:
def __init__(self, title, author, copies):
self.title = title
self.author = author
self.copies = copies

def __gt__(self, other):


return len(self.title) > len(other.title)

def getCopies(self):
return self.copies

book1_title = input('Enter name of book 1: ')


book1_author = input('Enter name of author: ')
book1_copies = int(input('Enter number of copies: '))
book1 = Book(book1_title, book1_author, book1_copies)

book2_title = input('Enter name of book 2: ')


book2_author = input('Enter name of author: ')
book2_copies = int(input('Enter number of copies: '))
book2 = Book(book2_title, book2_author, book2_copies)
print(book1 > book2)

bookName = input("Enter book Name:- ")


if (bookName == book1.title):
print(book1.getCopies())
elif (bookName == book2.title):
print(book2.getCopies())
else:
print("Wrong book name!")

Enter name of book 1: Harry Potter


Enter name of author: J K Rowling
Enter number of copies: 30000
Enter name of book 2: Percy Jackson
Enter name of author: Rick
Enter number of copies: 20000
False
Enter book Name:- Harry Potter
30000

In [3]:
class LogicalValue:
def __init__(self, value):
self.value = value

def __and__(self, other):


return LogicalValue(self.value and other.value)

def __or__(self, other):


return LogicalValue(self.value or other.value)

def __xor__(self, other):


return LogicalValue(self.value != other.value)

def __invert__(self):
return LogicalValue(not self.value)

def __str__(self):
return str(self.value)

trueValue = LogicalValue(True)
falseValue = LogicalValue(False)

print("Truth Table for AND gate:")


print(f"True AND True = {trueValue & trueValue}")
print(f"True AND False = {trueValue & falseValue}")
print(f"False AND True = {falseValue & trueValue}")
print(f"False AND False = {falseValue & falseValue}")

print("\nTruth Table for OR gate:")


print(f"True OR True = {trueValue | trueValue}")
print(f"True OR False = {trueValue | falseValue}")
print(f"False OR True = {falseValue | trueValue}")
print(f"False OR False = {falseValue | falseValue}")

print("\nTruth Table for XOR gate:")


print(f"True XOR True = {trueValue ^ trueValue}")
print(f"True XOR False = {trueValue ^ falseValue}")
print(f"False XOR True = {falseValue ^ trueValue}")
print(f"False XOR False = {falseValue ^ falseValue}")

print("\nTruth Table for NOT gate:")


print(f"NOT True = {~trueValue}")
print(f"NOT False = {~falseValue}")

Truth Table for AND gate:


True AND True = True
True AND False = False
False AND True = False
False AND False = False

Truth Table for OR gate:


True OR True = True
True OR False = True
False OR True = True
False OR False = False

Truth Table for XOR gate:


True XOR True = False
True XOR False = True
False XOR True = True
False XOR False = False

Truth Table for NOT gate:


NOT True = False
NOT False = True

In [4]:
class Matrix:
def __init__(self, elements):
self.elements = elements
self.rows = len(elements)
self.cols = len(elements[0])

def __add__(self, other):


if self.rows != other.rows or self.cols != other.cols:
raise ValueError("Matrix dimensions do not match for addition.")

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()

def __sub__(self, other):


if self.rows != other.rows or self.cols != other.cols:
raise ValueError("Matrix dimensions do not match for subtraction.")

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)

matrix1 = Matrix([[5, 6, 3], [7, 9, 2], [8, 6, 1]])


matrix2 = Matrix([[0, 8, 6], [1, 7, 1], [9, 7, 5]])

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 [ ]:

You might also like