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

Fai & Ml Lab Experiments - Manual

Uploaded by

xcr6p6ysq7
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

Fai & Ml Lab Experiments - Manual

Uploaded by

xcr6p6ysq7
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/ 9

1. Develope a Python program to check if a number is positive or negative.

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


if (a>0):
print("The entered number is positive")
else:
print("the entered number is negative")

2. Develope a Python program to check if a year is a leap year.

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

3. Develope a Python program to classify a triangle as equilateral, isosceles, or scalene.

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


b=int(input("Enter the second side: "))
c=int(input("Enter the third side: "))
if (a==b==c):
print("It is a equilateral triangle: ")
elif (a==b or b==c or a==c):
print("It is a isoceales triangle")
else:
print("It is a scalence triangle")

4. Develope a python program find the maximum of three numbers

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


b=int(input("Enter the second number: "))
c=int(input("Enter the third number: "))
d=max(a,b,c)
print(d)

5.Develope a python program to perform the following operations(string manipulation)


● a.Count the number and characters in the string
● b reverse a string
● c.count the occurrence of specific characters in a string
● d.check If a string is palindrome or not
● e.capitalize the first letter of each word in a string
a=input("Enter the string: ")
print( len(a))
rev = a[::-1]
print(rev)
occ = a. count("c")
print(occ)
r = a[::-1]
if (a == r):
print("The string is palindrome")
else:
print("The string is not palindrome")
print(a . title())

6.Develope a python program and perform following task(list manipulation)

● 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.print the dictionary items


● b.access items in the dictionary
● c. Access the item use get() method
● d.Change values in dictionary
● e.Calculate the length of the dictionary

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)

8. Develope a Python program to perform addition, subtraction, multiplication, and division


using functions.

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

11.Develope a python program using regular expression


a. find all occurrence of a pattern in a string
b.serach for pattern in a string
c.split a string using a pattern
d.replace occurrence of a pattern in a string

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)

mean = np.mean(data, axis=0)


print(mean)
data_meanremoved = data-mean
print("\n Mean removaled Data: ")
print(data_meanremoved)

min_value = np.min(data, axis=0)


print("\nThe min value is : ")
print(min_value)
max_value = np.max(data, axis=0)
print("\n The max value is : ")
print(max_value)
scaled_data = (data - min_value) / (max_value - min_value)
print("Min-max scaled data: " )
print(scaled_data)

standard_deviation = np.std(data, axis=0)


normalized_data = (data - mean ) / standard_deviation
print("\nThe normalized data: " )
print(normalized_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

# Sample data with consistent labels


X = np.array([[25,15],[30,18],[35,19],[40,20]])
Y = np.array(['dog', 'dog', 'cat', 'cat']) # Consistent lowercase labels
# Split data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.4, random_state=40)

# Initialize and train classifiers


a = DecisionTreeClassifier()
a.fit(X_train, Y_train)
print("Using Decision Tree Classifier, predict result is:", a.predict(X_test))

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

You might also like