0% found this document useful (0 votes)
34 views11 pages

AI Practical

The document provides Python code examples for basic logic gates (AND, OR, NOT) and their truth tables. It also includes a program to check for prime numbers and a family tree representation with functions to infer relationships such as siblings, parents, grandparents, and cousins. Each section illustrates the use of logic programming in Python.

Uploaded by

balilovejot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views11 pages

AI Practical

The document provides Python code examples for basic logic gates (AND, OR, NOT) and their truth tables. It also includes a program to check for prime numbers and a family tree representation with functions to infer relationships such as siblings, parents, grandparents, and cousins. Each section illustrates the use of logic programming in Python.

Uploaded by

balilovejot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

OUTPUT:-

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

def OR(a, b):


if a == 1 or b ==1:
return True
else:
return False

# 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:-

2. Use logic programming in Python to check for prime numbers.


num = 13
# Negative numbers, 0 and 1 are not primes
if num > 1:

# Iterate from 2 to n // 2
for i in range(2, (num//2)+1):

# If num is divisible by any number between


# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

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 = {

"John": {"children": ["Alice", "Bob"]},

"Alice": {"children": ["Charlie"]},

"Bob": {"children": ["David"]},

"Charlie": {"children": ["Eve"]},

"David": {"children": ["Frank"]}

# Function to find siblings

def find_siblings(name):

siblings = []

for parent, data in family_tree.items():

if name in data["children"]:

siblings = [child for child in data["children"] if child != name]

return siblings

# Function to find parents

def find_parents(name):

parents = []

for parent, data in family_tree.items():

if name in data["children"]:

parents.append(parent)

return parents

# Function to find grandparents


def find_grandparents(name):

parents = find_parents(name)

grandparents = []

for parent in parents:

grandparents.extend(find_parents(parent))

return grandparents

# Function to find cousins

def find_cousins(name):

parents = find_parents(name)

cousins = []

for parent in parents:

siblings_of_parent = find_siblings(parent)

for sibling in siblings_of_parent:

cousins.extend(family_tree[sibling]["children"])

return [cousin for cousin in cousins if cousin != name]

# Testing the functions

print("Siblings of Alice:", find_siblings("Alice"))

print("Parents of Charlie:", find_parents("Charlie"))

print("Grandparents of Eve:", find_grandparents("Eve"))

print("Cousins of David:", find_cousins("David"))

OUTPUT:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy