PRACTICAL PROGRAMS
PRACTICAL PROGRAMS
ARTIFICIAL INTELLIGENCE
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
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
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
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
Output:
My Favourite City
Output:
My Favourite Food
Output:
My Pet
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
Output:
True