AI File (5627)
AI File (5627)
AND MANAGEMENT
Practical File of
Artificial Intelligence
print("Hello, World!")
Output:
Hello, World!
2. Write a program to add two numbers with user input.
# Python program to add two numbers
num1 = 15
num2 = 12
# printing values
print("Sum of", num1, "and", num2 , "is", sum)
Output:
Sum of 15 and 12 is 27
3. Write a program to swap two variables.
x = 10
y = 50
print("Value of x:", x)
print("Value of y:", y)
Output:
Value of x: 50
Value of y: 10
4. Write a program to check if a number is positive,
negative or 0.
def check(n):
# Driver Code
check(5)
check(0)
check(-5)
Output:
Positive
Equal to zero
Negative
5. Write a program to check if a number is even or odd.
x = 24
if x % 24 == 0:
print(x,"Is Even Number")
else:
print(x, "Is Odd Number")
y = 19
if y % 19 == 0:
print(y,"Is Even Number")
else:
print(y, "Is Odd Number")
Output:
24 Is Even Number
19 Is Even Number
6. Write a program to check leap year.
def is_leap_year_modulo(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
# Example
year_to_check = 2024
result_modulo = is_leap_year_modulo(year_to_check)
print(f"{year_to_check} is a leap year: {result_modulo}")
Output:
num = 11
# If given number is greater than 1
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:
11 is a prime number
8. Write a program to find factorial of number using
recursion.
return 1
else:
# Driver Code
num = 5;
print("number : ",num)
print("Factorial : ",factorial(num))
Output:
number : 5
Factorial : 120
9. Write a program to create Pyramid Patterns.
Output:
*
***
*****
*******
*********
10.Write a program to implement Inheritance.
class Animal:
# attribute and method of the parent class
name = ""
def eat(self):
print("I can eat")
Output:
I can eat
My name is Dollar
11.Write a program to implement python List operation.
Output:
class Person:
def init (self, name, age):
self.name = name
self.age = age
p1 = Person("Saloni", 20)
print(p1.name)
print(p1.age
Output:
Saloni
20
13.Write a program in python to implement polymorphism.
class Car:
def init (self, brand, model):
self.brand = brand
self.model = model
def move(self):
print(" C R E T A ")
class Boat:
def init (self, brand, model):
self.brand = brand
self.model = model
def move(self):
print(" H Y U N D A I ")
class Plane:
def init (self, brand, model):
self.brand = brand
self.model = model
def move(self):
print(2022)
car1 = Car("Ford", "Mustang") #Create a Car class
boat1 = Boat("Ibiza", "Touring 20") #Create a Boat class
plane1 = Plane("Boeing", "747") #Create a Plane class
Output:
C R E T A
H Y U N D A I
2022
14.Write a program to implement breadth first search using
python.
# Constructor
def __init__(self):
while queue:
# Driver code
if __name__ == '__main__':
Output:
Following is Breadth First Traversal (starting from vertex 2)
2 0 3 1
15.Write a program to implement depth first search using
python.
# Constructor
def __init__(self):
# Driver's code
if __name__ == "__main__":
g = Graph()
g.addEdge(4, 5)
g.addEdge(4, 6)
g.addEdge(5, 6)
g.addEdge(6, 4)
g.addEdge(6, 7)
g.addEdge(7, 7)
# Function call
g.DFS(6)
Output:
Following is Depth First Traversal (starting from vertex 6)
6 4 5 7
16.Write a program to implement water jug problem using
python.
m = {}
isSolvable = False
path = []
q = deque()
q.append((0, 0))
if (u[0] == target):
if (u[1] != 0):
# Fill final state
path.append([u[0], 0])
else:
if (u[0] != 0):
# Empty Jug2
q.append([a, 0])
# Empty Jug1
q.append([0, b])
# Driver code
if __name__ == '__main__':
Output:
( 0 , 0 )
( 0 , 3 )
( 4 , 0 )
( 4 , 3 )
( 3 , 0 )
( 1 , 3 )
( 3 , 3 )
( 4 , 2 )
( 0 , 2 )
17.Write a program to implement travelling salesman problem
in python.
# update minimum
min_path = min(min_path, current_pathweight)
return min_path
# Driver Code
if __name__ == "__main__":
Output:
80
18.Write a program to implement Tower of Hanoi using python.
# Driver code
n = 4
TowerOfHanoi(n,'A','B','C')
# A, C, B are the name of rods
Output:
# Python program to print the path from root node to destination node
for N*N-1 puzzle algorithm using Branch and Bound
#The solution assumes that instance of puzzle is solvable
# Importing the heap functions from python library for Priority Queue
from heapq import heappush, heappop
# Node structure
class node:
count = 0
for i in range(n):
for j in range(n):
if ((mat[i][j]) and
(mat[i][j] != final[i][j])):
count += 1
return count
for i in range(n):
for j in range(n):
print("%d " % (mat[i][j]), end = " ")
print()
if root == None:
return
printPath(root.parent)
printMatrix(root.mat)
print()
# Finds a live node with least cost, add its children to list of
live nodes and finally deletes it from the list.
while not pq.empty():
# Find a live node with least estimated cost and delete it
from the list of live nodes
minimum = pq.pop()
if isSafe(new_tile_pos[0], new_tile_pos[1]):
# Driver Code
# Initial configuration
# Value 0 is used for empty space
initial = [ [ 1, 2, 3 ],
[ 5, 6, 0 ],
[ 7, 8, 4 ] ]
Output:
1 2 3
5 0 6
7 8 4
1 2 3
5 8 6
7 0 4
1 2 3
5 8 6
0 7 4
20.Write a python program to implement MATPLOT Library.
Output:
21.Write a python program to implement MATPLOT LABEL.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.show()
Output:
22.Write a python program to implement MATPLOT BAR GRAPH.
plt.bar(x,y)
plt.show()
Output: