Fai & Ml Lab Experiments - Manual
Fai & Ml Lab Experiments - Manual
a=int(input("Enter a year"))
if(a%4 == 0 and a%1000 !=0 or a%400 == a):
print("It is a leap year")
else:
print("It is not a leap year")
● a.create a list
● b.insert an element at specific position
● c remove an element
● d.Append an element to the end of the list
● e.Get the length of the list
● f.Pop an element from the end of the list
a = ["apple","orange","grapes"]
a.insert(o,"banana")
print(a)
a.remove("grapes")
print(a)
a.append("jackfruit")
print(a)
l=len(a)
print("The length of the list is",l)
a.pop(2)
print(a)
7.Develope a python program to perform the following operations(dictionary manipulation)
a={"name":"Sagar","roll no":'33'}
print(a)
print(a.values())
print (a.get("name"))
a["roll no"] = 22
print(a)
l=len(a)
print(l)
def add(a,b):
print("the result of Addition is",a+b)
def sub(a,b):
print("the result of Subtraction is",a-b)
def multi(a,b):
print("the result of Mutiplication is",a*b)
def div(a,b):
print("the result of Division is",a/b)
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
print("Addition\nSubtraction\nMultiplication\nDivision")
c=int(input("Enter your choice: "))
if(c==1):
add(a,b)
elif(c==2):
sub(a,b)
elif(c==3):
multi(a,b)
elif(c==4):
div(a.b)
else:
print("Invalid choice")
9. Develope a Python program using the predefined module to display calendar of current month
import calendar
a=int(input("Enter the year: "))
b=int(input("Enter the month: "))
print(calendar.month(a, b))
10. Develope a Python program using the user-defined module to perform area of circle
import AA
r= float(input ('Enter the radius: '))
A = AA.PI*r*r
print("area is: ",A)
AA.py
PI = 3.14159
import re
pattern = r"Apple"
text = "Apple is a fruit"
X = re.findall(pattern, text)
Y = re.search(pattern, text)
Z = re.split(r"\s", text)
A = re.sub(r"Apple", "Orange", text)
print(X)
print(Y)
print(Z)
print(A)
12. Develope a Python program using NumPy to create and manipulate ( addition, subtraction,
multiplication, division)arrays.
import numpy as np
A1 = np.array([[1,0],[3,5]])
A2 = np.array([[2,5],[6,8]])
S1=np.sum(A1)
print("Sum of the first array elements: ",S1)
S2 = np.sum(A2)
print("Sum of the second array elements: ",S2)
SUM=A1+A2
print("Addition of two array: ")
print(SUM)
SUB=A1-A2
print("Subtraction of two array: ")
print(SUB)
DIV = A1/A2
print("division of two array: ")
print(DIV)
MULTI =A1*A2
print("multiplication of two array: ")
print(MULTI)
13. Develope a Python program using NumPy to perform data processing tasks given below
● a binarization
● b.mean removal
● c. scaling
● d.normalization
import numpy as np
data = np.array([[1.5,2.7,3.2],[0.8,1.0,2.5],[-2.0,0.5,4.0]])
print("The assigned array is: ",data)
threshold = 1.0
binarized_data = np.where(data>threshold,1,0)
print("\n The binarized Data are: ")
print(binarized_data)
14. Develope a Python program using Scikit-learn to implement classifiers given below
● a . decision tree classifier
● b.random forest classifier
● c.svm
● d naive bayes
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
b = RandomForestClassifier()
b.fit(X_train, Y_train)
print("Using Random Forest Classifier, predict result is:", b.predict(X_test))
c = SVC()
c.fit(X_train, Y_train)
print("Using SVC, predict result is:", c.predict(X_test))
d = GaussianNB()
d.fit(X_train, Y_train)
print("Using Naive Bayes, predict result is:", d.predict(X_test))
15. Develope a Python program to implement the Last Coin Standing game.
16. Develope a Python program to implement the Tic Tac Toe game.
def display():
for r in b:
print(" | ".join(r))
def checkwinner():
# Check rows and columns
for i in range(3):
if b[i][0] == b[i][1] == b[i][2] != ' ':
return b[i][0]
if b[0][i] == b[1][i] == b[2][i] != ' ':
return b[0][i]
# Check diagonals
if b[0][0] == b[1][1] == b[2][2] != ' ':
return b[0][0]
if b[0][2] == b[1][1] == b[2][0] != ' ':
return b[0][2]
return None
def botplay():
for i in range(3):
for j in range(3):
if b[i][j] == ' ':
b[i][j] = 'O'
return
def play():
global b
b = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X' # Human player
while True:
display()
if current_player == 'X':
move = int(input("Enter your move (1-9): ")) - 1
row, col = divmod(move, 3)
if b[row][col] == ' ':
b[row][col] = 'X'
current_player = 'O'
else:
print("Invalid move, try again.")
continue
else:
botplay()
current_player = 'X'
winner = checkwinner()
if winner is not None:
display()
print(" wins!",winner)
break
play()