AI FILE
AI FILE
ARTIFICIAL INTELLIGENCE
&
EXPERT SYSTEM
PCC-AI-301
DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING
grade = 'A'
grade = 'B'
grade = 'C'
grade = 'D'
grade = 'E'
else:
grade = 'Fail'
OUTPUT:
Enter the marks: 96
if a==0 or a==1:
elif a>1:
for i in range(2,a):
if a%i==0:
break
else:
print("Number is prime")
break
OUTPUT:
Enter number: 6
Enter number: 3
Number is prime
PROGRAM -3
AIM : - WAP in Python to print multiplication table of a given number.
x=int(input("enter a number:"))
for i in range(1,11):
OUTPUT:
enter a number:56
56 * 1 = 56
56 * 2 = 112
56 * 3 = 168
56 * 4 = 224
56 * 5 = 280
56 * 6 = 336
56 * 7 = 392
56 * 8 = 448
56 * 9 = 504
56 * 10 = 560
PROGRAM -4
AIM : - WAP in Python to find the factorial of a given number.
factorial = 1
if num < 0:
else:
factorial *= i
OUTPUT:
Enter a number: 5
temp = a
a=b
b = temp
OUTPUT:
Enter the first number: 4
After swapping: a = 6, b = 4
a, b = b, a
OUTPUT:
Enter the first number: 56
Aim - Demonstrate the different ways of creating the list objects with suitable
2) append()
3) insert()
4) extend()
5) remove()
6) pop()
7) reverse()
8) sort()
9) sort(reverse=True)
10) looping
11) min()
12) max()
13) sums()
14) clear()
15) del()
Code-
1. Len()
a=[12,3,28,93]
len(a)
4
2. Append()
b=[1,2,3,4]
b.append(5)
print(b)
[1, 2, 3, 4, 5]
3. Insert()
print(b)
[1, 2, 3, 4, 5]
b.insert(1,3)
print(b)
[1, 3, 2, 3, 4, 5]
4. Extend()
a=[1,2,3,4]
b=[5,6,7,8]
a.extend(b)
print(a)
[1, 2, 3, 4, 5, 6, 7, 8]
5. Remove()
print(b)
[1, 3, 2, 3, 4, 5]
b.remove(3)
print(b)
[1, 2, 3, 4, 5]
6. Pop()
a=[1,2,3,4]
a.pop(3)
print(a)
[1, 2, 3]
7. Reverse()
print(a)
[1, 2, 3]
a.reverse()
print(a)
[3, 2, 1]
8. Sort()
print(a)
a.sort()
print(a)
[1, 2, 3, 3, 3, 4, 5, 12, 93]
9. Sort(reverse=true)
a=[1,5,3,6]
a.sort(reverse=true)
print(a)
a=[6,5,3,1]
10. Looping()
a=[1,2,3]
for b in a:
print(b)
11. Min()
a=[1,5,9,3]
min(a)
12. Max()
a=[12,87,29]
max(a)
87
13. Sums()
a=[19,3,4]
sum(a)
26
14. Clear()
a=[1,2,3,4]
a.clear()
print(a)
[]
15. Del()
a=[1,2,3,4]
del(a[2])
print(a)
[1, 2, 4]
PROGRAM-7
Aim - Demonstrate the different ways of creating the sets object with the
suitable
2) update()
3) copy()
4) pop()
5) remove()
6) discard()
7) clear()
8) union()
9) intersection()
10) difference ()
Code-
1) Add
set1={10,20,30,40}
print(set1)
set1.add(7)
print(set1)
set1={1,2,3}
set2={4,5}
print(set1.update(set2))
None
3) pop()
set1={10,20,30,40}
set1.pop()
40
print(set1)
4) remove()
set1={7,10,20,30}
print(set1)
set1.remove(30)
print(set1)
5) discard()
set1={7,10}
set1.discard(7)
print(set1)
{10}
6) clear()
Set2={5,6,7,8}
set2.clear()
print(set2)
set()
7) union()
set1 ={5,6,7,8}
set2 ={5,7,3,9}
set1.union(set2)
{3, 5, 6, 7, 8, 9}
print(set1)
{8, 5, 6, 7}
8) intersection()
set1 ={1,2,3}
set2 ={4,5}
print(set1.intersection(set2))
{4, 5}
print(set1&amp;set2)
{4, 5}
9) difference()
print(set1.difference(set2))
{1, 2, 3}
print(set1-set2)
{1, 2, 3}
Program -08
PROGRAM-09
i. Len()
ii. Count()
iii. Index()
iv. Sorted
v. Min
vi. Max
vii. Cmp
viii. reverse
(a)
CODE-
· t1={1,2,3,'abc',4}
print (t1)
OUTPUT-
{1, 2, 3, 4, 'abc'}
CODE-
· t3=(t1,t2)
print (t3)
OUTPUT-
· t4=t1+t2
print(t4)
OUTPUT-
(1, 2, 3, 'pari', 5, 6, 1, 2, 0, 1, 2, 0, 0, 5)
(b)
i. len()
CODE-
t2=(1,2,0,1,2,0,0,5)
print(len(t2))
OUTPUT-
ii. count()
CODE-
t2=(1,2,0,1,2,0,0,5)
OUTPUT-
iii. index()
CODE-
print(t1[1:])
OUTPUT-
(2, 3, 'pari', 5, 6)
CODE-
print(t2[1:5])
OUTPUT-
(2, 0, 1, 2)
CODE-
print(t2[:4])
OUTPUT-
(1, 2, 0, 1)
CODE-
print(t2[::2])
OUTPUT-
(1, 0, 2, 0)
iv. sorted
CODE-
x = sorted(a)
print(x)
OUTPUT-
(“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”)
v. min
CODE-
t2=(1,2,0,1,2,0,0,5)
print(min(t2))
OUTPUT-
vi. max
CODE-
t2=(1,2,0,1,2,0,0,5)
print(max(t2))
OUTPUT-
vii. Cmp
CODE-
OUTPUT-
viii. reverse
CODE-
t=(1,2,3)
rev=t[::-1]
print(rev)
OUTPUT-
(3,2,1)
PROGRAM-10
CODE:
return x + y
return x - y
return x * y
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
try:
except ValueError:
continue
if choice == '1':
if next_calculation == "no":
break
else:
print("Invalid Input")
OUTPUT:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 1
CODE:
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
OUTPUT:
Fibonacci sequence:
3
PROGRAM-12
CODE:
def mymul(x):
return 5*x
print(mymul(5))
print(mymul (6))
25
30
def fact(n):
if(n==0):
return 1
else:
return n*fact(n-1
fact(4)
24
def tri_reccursion(k):
if(k>0):
result = k + tri_reccursion(k-1)
print(result)
else:
result=0
return result
tri_reccursion(6)
OUTPUT:
10
15
21
PROGRAM-13
AIM- WAP in python to create a dictionary with first character as key and
word starting with that character as a value
CODE:
OUTPUT:
H : ['Hey']
J : ['Jane,']
h : ['how']
a : ['are']
y : ['you']
PROGRAM-14
AIM- WAP in python to create a list of tuples with the first element as the
number and second element as the square of that number.
CODE:
OUTPUT:
The list is
[(23, 529), (42, 1764), (67, 4489), (89, 7921), (11, 121), (32, 1024)]
PROGRAM-15
AIM- WAP in python to implement the breath first search traversal
Code:
graph = {'5':['2','7'],'2':['3','4'],'7':['8'],'3':[],'4':['8'],'8':[]}
visited = []
queue = []
def bfs(visited,graph,node):
visited.append(node)
queue.append(node)
while queue:
m=queue.pop(0)
print(m,end='')
visited.append(nbr)
queue.append(nbr)
bfs(visited,graph,'5')
output:
527348
PROGRAM-16
AIM- WAP in python to implement the depth first search traversal
Code:
graph = {'5':['2','7'],'2':['3','4'],'7':['8'],'3':[],'4':['8'],'8':[]}
visited = []
def dfs(visited,graph,node):
if nbr not in visited:
print(node)
visited.add (node)
for nbr in graph(node):
dfs(visited,graph,nbr)
output:
Following is the Depth-First Search
5
3
2
4
8
7