[go: up one dir, main page]

0% found this document useful (0 votes)
13 views9 pages

PRACTICAL PROGRAMS

The document contains a series of practical programming exercises in Python focused on artificial intelligence. Each exercise includes a problem statement, a sample program, and the expected output. Topics covered include string manipulation, mathematical calculations, data visualization using libraries like NumPy and Matplotlib, and image processing with OpenCV.

Uploaded by

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

PRACTICAL PROGRAMS

The document contains a series of practical programming exercises in Python focused on artificial intelligence. Each exercise includes a problem statement, a sample program, and the expected output. Topics covered include string manipulation, mathematical calculations, data visualization using libraries like NumPy and Matplotlib, and image processing with OpenCV.

Uploaded by

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

PRACTICAL RECORD PROGRAMS

ARTIFICIAL INTELLIGENCE

Python Practical Questions


1. Write a program to input a string and display the count of vowels and consonants in the
string.
Ans. Program:
str=input ("Please enter a string as you wish: ")
vowels=0
consonants=0
for i in str:
if (i == 'a' or i == 'e' or i == 'i'or i == 'o' or i == 'u' or i == 'A' or
i == 'E' or i == 'I' or i == '0' or i == 'U' ):
vowels=vowels+1
else:
consonants-consonants+1
print ("The number of vowels: ", vowels)
print ("\nThe number of consonants: ", consonants)

Output:
Please enter a string as you wish: Orange Education
The number of vowels: 8
The number of consonants: 8

2. Write a program to input a string and display the string in the reverse order.
Ans. Program:
def reverse_string (str):
str1 = ""
for i in str:
str1 = i + str1
return str1
str = "Artificial Intelligence"
print ("The original string is: ",str)
print ("The reverse string is", reverse_string (str))

Output:
The original string is: Artificial Intelligence
The reverse string is ecnegilletnllaicifitrA

3. Write a Python code to take the input of a number n and then find and display its factorial
(n!). For example, 5!= 5x4x3x2x1 ie, 120.
Ans. Program:
n = int(input ("Enter a number: "))
factorial = 1
if n < 0:
print ("Factorial does not exist for negative numbers")
elif n == 0:
print ("The factorial of 0 is 1")
else:
for i in range (1, n + 1):
factorial = factorial*i
print ("The factorial of ",n," is", factorial)

Output:
Enter a number: 7
The factorial of 7 is 5040

4. Write a Python code to input the lengths of the three sides of a triangle and display whether a
triangle can be formed with the inputs or not. If a triangle can be formed then display whether
the triangle will be scalene, isosceles or equilateral triangle.
Ans. Program:
print ("Input the sides of the triangle: ")
A = int (input ("A: "))
B = int (input ("B: "))
C = int (input ("C: "))
if A== B == C:
print ("Equilateral triangle")
elif A==B or B==C or A==C:
print ("isosceles triangle")
else:
print ("Scalene triangle")

Output:
Input the sides of the triangle:
A: 10
B: 10
C: 10
Equilateral triangle

5. Write a program to input two numbers and display the LCM of the two numbers.
Ans. Program:
def calculate_lcm (x, y):
if x > y:
greater = x
else:
greater = y
while (True):
if ((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = int (input ("Enter first number: "))
num2 = int (input ("Enter second number: "))
print ("The L.C.M. of", num1, "and", num2, "is", calculate_lcm (num1, num2))

Output:
Enter first number: 5
Enter second number: 6
The L.C.M. of 5 and 6 is 30

6. Write a program to input a number and display whether it is a palindrome or not.


Ans. Program:
num-int (input ("Enter a number:"))
temp=num
rev=0
while (num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if (temp==rev):
print ("The number is a palindrome!")
else:
print ("The number is not a palindrome!")

Output:
Enter a number:1002001
The number is a palindrome!

7.Write a program to input a number and display whether it is a prime number or not.
Ans. Program:
num = int(input ("Enter a number: "))
if num > 1:
for i in range (2, int (num/2)+1):
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:
Enter a number: 12
12 is not a prime number

8.Write a program to input two numbers and swap both the numbers without using the third
number. Ans. Program:
x = int (input ("Enter the value of x: "))
y = int (input ("Enter the value of y: "))
print ("Numbers before swapping: %d %d\n" % (x, y))

x=x+y
y = x- y
x = x- y
print ("Numbers after swapping: %d %d\n" % (x, y))

Output:
Enter the value of x: 22
Enter the value of y: 14
Numbers before swapping: 22 14
Numbers after swapping: 14 22

9. Using Numpy Package:

. Create a 4X2 array with random integer.


. Create 3X3 array with all zeros.
Ans. Program:
import numpy as np
r = np.random.rand (4,2)
print (r)
import numpy as np
a = np.zeros((3,3))
print (a)

Output:
[[0.01770743 0.50604842)
[0.40198197 0.974359 ]
[0.35439403 0.6841792 1
[0.99028845 0.57243665]]
[[0.0.0.]
[0.0.0.]
[0.0.0.]]

10. Using Matplotlib and the given data, plot a bar chart:
No of people voted = [23,45,31,40,35]
Area covered = [‘a1’,’a2’,’a3’,’a4’,’a5’]
Ans. Program:
import matplotlib.pyplot as plt
No_of_people_voted = [23,45,31,40,35]
plt.bar (['al', 'a2', 'a3', 'a4', 'a5'], No_of_people_voted)
plt.title('Bar Chart')
plt.xlabel('Area covered')
plt.ylabel('No of people voted')
plt.show()

Output:
11. Using Matplotlib and the given data, plot a line chart:
x_axis [3,4,6,2,8]
y_axis = [9,10,8,7,6]
Ans. Program:
import matplotlib.pyplot as plt.
import numpy as np
x = np.array([3,4,6,2,8])
y= np.array([9,10,8,7,6])
plt.plot(x, y)
plt.show()

Output:

12. Using Matplotlib and the given data, Plot a pie chart:
Data= [45,23,41,78,65]
Ans. Program:
from matplotlib import pyplot as plt
import numpy as np
fig=plt.figure()
ax = fig.add_axes ([0,0,1,1])
ax.axis ('equal')
Data= [45, 23, 41, 78, 65]
ax.pie (Data, autopct='1.2f%%')
plt.show()

Output:

13. Using Matplotlib and the given data, plot a scatter chart:
Height =(4.5,5.2, 4.1, 3.5,5]
Weight =[34,41,45,56,39]
Ans. Program:
import matplotlib.pyplot as plt
Weight=[34,41,45,56,39]
Height= [4.5, 5.2, 4.1, 3.5,5]
plt.scatter (Height, Weight, c='blue')
plt.title('Scatter plot')
plt.xlabel('Height')
plt.ylabel('Weight")
plt.show()

Output:

14. Using NumPy package: Create an array of 5 marks and display the average of all marks.
Ans. Program:
import numpy as np
import statistics

a =np.array([95, 90, 49, 71,80])


m = statistics.mean (a)
print ("The average is: ", m)

Output:
The average is: 77

15. Create a list of distance travelled by a car in a week and calculate using statistics module:
Mean Median Mode

Ans. Program:
import statistics
d = [95,90, 49, 71, 90, 100,55]
m1 = statistics.mean (d)
m2=statistics.median (d)
m3 = statistics.mode (d)
print ("The mean is: ", m1)
print ("The median is: ", m2)
print ("The mode is: ", m3)

Output:
The mean is: 78.57142857142857
The median is: 90
The mode is: 90

16. Create a list of number of voting done in 6 areas and calculate using statistics module:
● Standard deviation

● Variance
Ans. Program:
import statistics
Voting=[59,52,49,71, 44, 63]
m1 = statistics.stdev (Voting)
print ("The standard deviation is: ", round (m1,2))

Output:
The standard deviation is: 9.91

17. Using the library, import cv2, matplotlib


Upload an image of your favourite city in the world on the screen and give a proper title to it.
Ans. Program:
import cv2
from matplotlib import pyplot as plt
import numpy as np
img=cv2.imread(‘D:\London.jpg’)
plt.imshow(img)
plt.title(‘My favourite City’)
plt.axis (‘off’)
plt.show()

Output:
My Favourite City

18. Using the library: import cv2, matplotlib


Upload an image of your favourite food and display the picture in RGB color mode.
Ans. Program:
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('D:\Pasta.jpg')
plt.imshow(cv2.cvtColor (img, cv2.COLOR_BGR2RGB))
plt.title('My Favourite Food')
plt.axis ('off')
plt.show()

Output:
My Favourite Food

19. Using the library: import cv2, matplotlib


Upload an image of your favourite pet and display the picture in grayscale mode.
Ans. Program:
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('D:\Dog.jpg', 0)
plt.imshow(img, cmap = 'gray')
plt.title('My Pet')
plt.axis ('off')
plt.show()

Output:
My Pet

20. Using the library: import cv2, matplotlib


Upload your own picture in your favorite dress and put a black box on the face and the hands.
Ans. Program:
import cv2
from matplotlib import pyplot as plt

import numpy as np
img cv2.imread('D:\Picture of myself.jpg')
print (img.shape)
img [500:1600, 2200:3500]=[0,0,0]
img[2800:3700, 2300:3300]=[0,0,0]
plt.imshow (cv2.cvtColor (img, cv2.COLOR_BGR2RGB))
plt.title('Shaded Face')
plt.axis ('off')
plt.show()

Output:
(3755, 5632, 3)
Shaded Face

21. Using the library: import cv2, matplotlib


Upload an image of a banana tree and change the color of the tree in the image and save it in
your drive.
Ans. Program:
import cv2
Banana Tree
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('D:\Banana Tree.jpg', 0)
plt.imshow(img, cmap = 'gray')
plt.title('Banana Tree')
plt.axis('off')
plt.show()
cv2.imwrite ('E: \Banana tree after color change.jpg',img)

Output:
True

You might also like