AI Practical
AI Practical
1.AND gate
2.OR gate
1.Learn the building blocks of Logic Programming in Python.
1.AND Gate
# Python3 program to illustrate
# working of AND gate
def AND (a, b):
if a == 1 and b == 1:
return True
else:
return False
# Driver code
if __name__=='__main__':
print(AND(1, 1))
print("+---------------+----------------+")
print(" | AND Truth Table | Result |")
print(" A = False, B = False | A AND B =",AND(False,False)," | ")
print(" A = False, B = True | A AND B =",AND(False,True)," | ")
print(" A = True, B = False | A AND B =",AND(True,False)," | ")
print(" A = True, B = True | A AND B =",AND(True,True)," | ")
2.OR gate
# Python3 program to illustrate
# working of OR gate
# Driver code
if __name__=='__main__':
print(OR(0, 0))
print("+---------------+----------------+")
print(" | OR Truth Table | Result |")
print(" A = False, B = False | A OR B =",OR(False,False)," | ")
print(" A = False, B = True | A OR B =",OR(False,True)," | ")
print(" A = True, B = False | A OR B =",OR(True,False)," | ")
print(" A = True, B = True | A OR B =",OR(True,True)," | ")
OUTPUT:-
3.NOT gate
3.NOT gate
# Python3 program to illustrate
# working of Not gate
def NOT(a):
return not a
# Driver code
if __name__=='__main__':
print(NOT(0))
print("+---------------+----------------+")
print(" | NOT Truth Table | Result |")
print(" A = False | A NOT =",NOT(False)," | ")
print(" A = True, | A NOT =",NOT(True)," | ")
OUTPUT:-
# Iterate from 2 to n // 2
for i in range(2, (num//2)+1):
OUTPUT:-
3.Use logic programming in Python parse a family tree and infer the
relationships between the family members
# Family tree represented as a dictionary
family_tree = {
def find_siblings(name):
siblings = []
if name in data["children"]:
return siblings
def find_parents(name):
parents = []
if name in data["children"]:
parents.append(parent)
return parents
parents = find_parents(name)
grandparents = []
grandparents.extend(find_parents(parent))
return grandparents
def find_cousins(name):
parents = find_parents(name)
cousins = []
siblings_of_parent = find_siblings(parent)
cousins.extend(family_tree[sibling]["children"])
OUTPUT:-