You are on page 1of 6

KENDRIYA VIDYALAYA IISC

Session: 2023-24 Class: 10


Sub: Artificial Intelligence (AI) Code: 417 (Practical)
List of Python Programs to complete in Practical Record Book:
Note: Write the Question, Code and an output.

1. Write a program (WAP) in python to take two numbers as input and print their sum, difference, product
and division as output.

Code:
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))
print("Sum is:", x+y)
print("Difference is:", x-y)
print("Product is:", x*y)
print("Division is:", x/y)

Output:
Enter first number:12
Enter second number:8
Sum is: 20
Difference is: 4
Product is: 96
Division is: 1.5

2. WAP to take two numbers and print the smaller of the two numbers.
Code:
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))
if x<y:
print(x, "is smaller.")
else:
print(y, "is smaller.")

Output:
Enter first number:9
Enter second number:56
9 is smaller.
3. WAP to take three numbers and print the largest of the numbers.
Code:
x=int(input("Enter first number:"))
y=int(input("Enter second number:"))
z=int(input("Enter third number:"))
if x>y>z:
print(x, "is largest.")
elif y>x>z:
print(y, "is largest.")
else:
print(z, "is largest.")
Output:
Enter first number:5
Enter second number:9
Enter third number:2
9 is largest.

4. WAP to take a number and print whether it is even or odd.


Code:
x=int(input("Enter the number:"))
if x%2==0:
print(x, "is Even.")
else:
print(x, "is Odd.")
Output:
Enter the number:37
37 is Odd.

5. WAP that takes the megapixel information of a camera from the user and prints the storage size, in MB,
of a colour picture captured from that camera.
Code:
x=int(input("Enter Mega-pixels of the camera:"))
y=x*(10**6)
z=3*y/(1024*1024)
print("It captures a colour image of size:", round(z,0), "MB")
Output:
Enter Mega-pixels of the camera:50
It captures a colour image of size: 143.0 MB

6. WAP that takes the megapixel information of a camera from the user and prints the storage size, in GB,
of a Grayscale video of length 10 seconds at 25 frames per second, recorded from that camera.
Code:
x=int(input("Enter Mega-pixels of the camera:"))
y=x*(10**6)
z=(25*10*y)/(1024*1024*1024)
print("It records a 10-seconds Grayscale video of size:", round(z,0), "GB")
Output:
Enter Mega-pixels of the camera:20
It records a 10-seconds Grayscale video of size: 5.0 GB

7. WAP that takes the stage number from the user and prints the AI Project Cycle stage name associated
with the stage number. Like, for 3, it should print Data Exploration.
Code:
x=int(input("Enter the Stage no."))
stages=['Problem Scoping', 'Data Acquisition', 'Data Exploration', 'Modeling', 'Evaluation']
print("Stage ", x, "is:", stages[x-1])
Output:
Enter the Stage no.4
Stage 4 is: Modeling
8. WAP to take a string (sentence) and count the number of vowels and consonants in it.
Code:
x=input("Enter any sentence:")
cv=cc=0
for i in x:
if i.isalpha():
if i in 'aeiouAEIOU':
cv=cv+1
else:
cc=cc+1
print(cv, "vowels and ", cc, "consonants.")

Output:

Enter any sentence: India is my country 2 good.

8 vowels and 12 consonants.

9. WAP to take a string and count all words that start with the letter ‘a’.
Code:
x=input("Enter any sentence:")
ca=0
words=x.split()
for i in words:
if i[0] in 'aA':
ca=ca+1
print(ca, "words start with letter a."))
Output:
Enter any sentence: An apple a day keeps us awake.
4 words start with letter a.
10. WAP to take a string and print the stems of all words after removing five common suffixes and prefixes.
Code:
x=input("Enter any sentence:")
prefixes=['un', 'de', 'ex', 'mis', 'pre']
suffixes=['ful', 'es', 'ly', 'ing', 'ed']
words=x.lower().split()
for i in words:
if i[0:2] in prefixes:
print(i, 'becomes:', i[2:])
elif i[0:3] in prefixes:
print(i, 'becomes:', i[3:])
elif i[-2:] in suffixes:
print(i, 'becomes:', i[:-2])
elif i[-3:] in suffixes:
print(i, 'becomes:', i[:-3])
else:
print(i, 'is unchanged.')
Output:
Enter any sentence:Keerti does not seem unhappy and is caring and beautiful
Keerti is unchanged.
does becomes: do
not is unchanged.
seem is unchanged.
unhappy becomes: happy
and is unchanged.
is is unchanged.
caring becomes: car
and is unchanged.
beautiful becomes: beauti
11. WAP to take a string and print the frequencies of all words occurring in the sentence.
Code:
x=input("Enter any sentence:")
freq={}
words=x.lower().split()
for i in words:
if i not in freq:
freq[i]=1
else:
freq[i]=freq[i]+1
print(freq)
Output:
Enter any sentence:Jeevan is a good boy A Boy is naughty Lalit is naughty
{'jeevan': 1, 'is': 3, 'a': 2, 'good': 1, 'boy': 2, 'naughty': 2, 'lalit': 1}

12. WAP to take a string and identify five most commonly occurring stop words from it.

Code:
x=input("Enter any sentence:")
stopwords=['a', 'an', 'the', 'to', 'is']
words=x.lower().split()
for i in words:
if i in stopwords:
print("Found a stopword:",i)
Output:
Enter any sentence:Jeevan is a good boy A Boy is naughty Lalit is naughty
Found a stopword: is
Found a stopword: a
Found a stopword: a
Found a stopword: is
Found a stopword: is

13. WAP to take a string and print all words in common case (lower-case), exactly once only.

Code:
x=input("Enter any sentence:")
words=x.lower().split()
c=0
for i in words:
if i not in words[:c]:
print(i)
c=c+1
Output:
Enter any sentence: Veda and Keerthana are good friends And ARE Good students
veda
and
keerthana
are
good
friends
students

14. WAP that takes your name and gender as input and prints a rule based output like: ‘Hello, Master name’
or ‘Hello, Miss name’ depending on your gender.
Code:
x=input("What is your name?")
y=input('Please tell me your gender:')
if y.lower() in 'maleboy':
print("Hello, Master ", x.upper())
else:
print("Hello, Miss ", x.upper())
Output:
What is your name?mahima
Please tell me your gender:girl
Hello, Miss MAHIMA

15. WAP to take 4 numbers as inputs for TP, TN, FP and FN values of a confusion matrix for a model. Then
print the accuracy and F1 score of that model.
Code:
tp=int(input("Enter True Positive:"))
tn=int(input("Enter True Negative:"))
fp=int(input("Enter False Positive:"))
fn=int(input("Enter False Negative:"))
acc=((tp+tn)/(tp+tn+fp+fn))*100
prec=tp/(tp+fp)
recal=tp/(tp+fn)
f1score=((2*prec*recal)/(prec+recal))*100
print("The Accuracy is:", int(acc), "%")
print("The F1 Score is:", int(f1score), "%")
Output:
Enter True Positive:5
Enter True Negative:45
Enter False Positive:0
Enter False Negative:50
The Accuracy is: 50 %
The F1 Score is: 16 %

You might also like