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

AI FILE

This document is a lab file for a course on Artificial Intelligence and Expert Systems, containing various Python programming exercises. It includes programs for calculating grades, checking prime numbers, printing multiplication tables, finding factorials, swapping numbers, and demonstrating list, set, and tuple operations. Additionally, it features a simple calculator and examples of recursion, dictionary creation, and tuple manipulation.

Uploaded by

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

AI FILE

This document is a lab file for a course on Artificial Intelligence and Expert Systems, containing various Python programming exercises. It includes programs for calculating grades, checking prime numbers, printing multiplication tables, finding factorials, swapping numbers, and demonstrating list, set, and tuple operations. Additionally, it features a simple calculator and examples of recursion, dictionary creation, and tuple manipulation.

Uploaded by

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

LAB FILE

ARTIFICIAL INTELLIGENCE
&
EXPERT SYSTEM
PCC-AI-301

DEPARTMENT OF COMPUTER
SCIENCE & ENGINEERING

SUBMITTED BY: SUBMITTED TO:


Sachin Ms. Jyoti Kashyap
23011018056
INDEX
S NO. PROGRAM NAMES DATE SIGN
PROGRAM -1
AIM : -WAP in Python to calculate grade.

91-100: A , 81-90: B, 71-80: C, 61-70: D, 45-60: E, 45>: Fail

marks = int(input("Enter the marks: "))

if marks >= 91 and marks <= 100:

grade = 'A'

elif marks >= 81 and marks <= 90:

grade = 'B'

elif marks >= 71 and marks <= 80:

grade = 'C'

elif marks >= 61 and marks <= 70:

grade = 'D'

elif marks >= 45 and marks <= 60:

grade = 'E'

else:

grade = 'Fail'

print("Your grade is:",grade)

OUTPUT:
Enter the marks: 96

Your grade is: A


PROGRAM -2
AIM : - WAP in Python to check whether given number is Prime or not.

a=int(input("Enter number :"))

if a==0 or a==1:

print("Number is not Prime")

elif a>1:

for i in range(2,a):

if a%i==0:

print("Number is not prime")

break

else:

print("Number is prime")

break

OUTPUT:
Enter number: 6

Number is not prime

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

print(x,"*" ,i, "=",x*i)

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.

num = int(input("Enter a number: "))

factorial = 1

if num < 0:

print("Factorial does not exist for negative numbers")

else:

for i in range(1, num + 1):

factorial *= i

print("The factorial of {num} is:",factorial)

OUTPUT:
Enter a number: 5

The factorial of 5 is: 120


PROGRAM -5
AIM : - WAP in Python to swap the two numbers

a)with using 3rd variable


a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

temp = a

a=b

b = temp

print(f"After swapping: a = {a}, b = {b}")

OUTPUT:
Enter the first number: 4

Enter the second number: 6

After swapping: a = 6, b = 4

b)without using 3rd variable.


a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

a, b = b, a

print(f"After swapping: a = {a}, b = {b}")

OUTPUT:
Enter the first number: 56

Enter the second number: 89

After swapping: a = 89, b = 56


Program-6

Aim - Demonstrate the different ways of creating the list objects with suitable

example programs. Demonstrate the following functions which operates on list


in

python with the suitable example -


1) len()

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)

[5, 4, 3, 2, 3, 1, 93, 3, 12]

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

example programs. Demonstrate the following functions and methods which

operates on sets in python with suitable examples-


1) add()

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)

{10, 20, 30}

set1.add(7)

print(set1)

{7, 10, 20, 30}


2) Update()

set1={1,2,3}

set2={4,5}

print(set1.update(set2))

None

3) pop()

set1={10,20,30,40}

set1.pop()

40

print(set1)

{10, 20, 30}

4) remove()

set1={7,10,20,30}

print(set1)

{7, 10, 20, 30}

set1.remove(30)

print(set1)

{7, 10, 20}

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;amp;set2)

{4, 5}

9) difference()

print(set1.difference(set2))

{1, 2, 3}

print(set1-set2)

{1, 2, 3}
Program -08
PROGRAM-09

AIM- WAP in python to :-

(a)Demonstrate the different ways of creating ​


“TUPLE”objects with suitable example programs

(b) Demonstrate the following functions/methods which operate on TUPLE


in python with suitable example:-

i. Len()

ii. Count()

iii. Index()

iv. Sorted

v. Min

vi. Max

vii. Cmp

viii. reverse

v Tuples are used to store multiple items in a single variable. Tuple is


one of 4 built-in data types in Python used to store collections of
data, the other 3 are List, Set, and Dictionary, all with different
qualities and usage. A tuple is a collection which is ordered and
unchangeable.

(a)
CODE-

· t1={1,2,3,'abc',4}

​print (t1)

OUTPUT-

​ {1, 2, 3, 4, 'abc'}
CODE-

· t3=(t1,t2)

print (t3)

OUTPUT-

((1, 2, 3, 'pari', 5, 6), (1, 2, 0, 1, 2, 0, 0, 5))


CODE-

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

print(t2 count (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-

a = ("b", "g", "a", "d", "f", "c", "h", "e")

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

AIM- WAP in python to make a simple calculator

CODE:

def add(x, y):

​ return x + y

def subtract(x, y):

​ return x - y

def multiply(x, y):

​ return x * y

def divide(x, y):

​ return x / y

print("Select operation.")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")
while True:

​ choice = input("Enter choice(1/2/3/4): ")

​ if choice in ('1', '2', '3', '4'):

​ try:

​num1 = float(input("Enter first number: "))

​num2 = float(input("Enter second number: "))

​ except ValueError:

​print("Invalid input. Please enter a number.")

​continue

​ if choice == '1':

​print(num1, "+", num2, "=", add(num1, num2))

​ elif choice == '2':

​print(num1, "-", num2, "=", subtract(num1, num2))

​ elif choice == '3':

​print(num1, "*", num2, "=", multiply(num1, num2))


​ elif choice == '4':

​print(num1, "/", num2, "=", divide(num1, num2))

​next_calculation = input("Let's do next calculation? (yes/no): ")

​ 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

Enter first number: 5

Enter second number: 1

5.0 + 1.0 = 6.0


PROGRAM-11

AIM- WAP in python to display one Fibonacci series using recursion.

CODE:

nterms = int(input("How many terms? "))

n1, n2 = 0, 1

count = 0

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

​ print(n1)

​ nth = n1 + n2

​ n1 = n2

​ n2 = nth

​ count += 1

OUTPUT:

How many terms? 5

Fibonacci sequence:

3
PROGRAM-12

AIM- WAP in python to find factorial of number using recursion

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

print("Reccursion Example result:")

tri_reccursion(6)

OUTPUT:

Reccursion Example result:

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:

my_string=input("Enter the string :")


split_string = my_string.split()
my_dict={}
for elem in split_string:
if(elem[0] not in my_dict.keys()):
my_dict[elem[0]]=[]
my_dict[elem[0]].append(elem)
else:
if(elem not in my_dict[elem[0]]):
my_dict[elem[0]].append(elem)
print("The dictionary created is")
for k,v in my_dict.items():
print(k,":",v)

OUTPUT:

Enter the string :Hey Jane, how are you

The dictionary created is

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:

my_list = [23, 42, 67, 89, 11, 32]


print(“The list is “)
print(my_list)
print(“The resultant tuple is :”)
my_result = [(val, pow(val, 2)) for val in my_list]
print(my_result)

OUTPUT:

The list is

[23, 42, 67, 89, 11, 32]

The resultant tuple 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='')

for nbr in graph[m]:

if nbr not in visited:

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​

You might also like