Class X Practical Program
Class X Practical Program
Leading open data science platform Free IDE that is included Create and share documents
with Anaconda with live code,visualizations,
text, ...powered by Python
INDEX
S. Name of the Program Page Sign
No. No.
1 Write a python program to check whether the given number is prime or not?
DATA SCIENCE
5 Write a program to import data from a CSV file into a pandas program.
11 Write a program to read and display image using the opencv cv2 library.
12 Write a program to read and display original image using the opencv cv2 library.
13 Write a program to read and display gray color image using the opencv cv2
library.
Vaishali Mahajan 1
Program 1: Write a python program to check whether the given number is
prime or not. Source Code:
# Python program to check if the input number is prime or not
#num = 407
# take input from the user
num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
rem=1
for i in range(2,num):
rem=num%i
if rem == 0:
break
if rem == 0:
print(num,"is not a prime number")
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Output:
Enter a number: 6
6 is not a prime number
Result: Thus, the program to to check whether the given number is prime or not has been
created and executed successfully.
Output:
Result: Thus, the program to implement simple Chatbot? has been created and
executed successfully.
Program 3: Write a python program to generate Calendar for the given month and
year?
Source Code:
import calendar
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
print(calendar.month(yy, mm))
Output:
Enter year: 2010
Enter month: 5
Result:
Program 4: Write a python program to implement a Simple Calculator program?
Source Code:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
Vaishali Mahajan 3
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
Output:
Data Science
Program 5: Write a program to import data from a CSV file into a pandas program.
Id, name, salary, start_date, dept
1 Rick 623.3 2012-01-01 IT
2 Dan 515.2 2013-09-23 Operations
3 Tusar 611 2014-11-15 IT
4 Ryan 729 2014-05-11 HR
5 Gary 843.25 2015-03-27 Finance
6 Rasmi 578 2013-05-21 IT
7 Pranab 632.8 2013-07-30 Operations
● Reading a CSV File
The read_csv function of the pandas library is used to read the content of a CSV file into the python
environment as a pandas DataFrame. The function can read the files from the OS by using the proper
path to the file.
Source Code:
import pandas as pd
df = pd.read_csv('path\filename.csv')
print (df)
Output
id name salary start_date dept
0 1 Rick 623.30 2012-01-01 IT
1 2 Dan 515.20 2013-09-23 Operations
2 3 Tusar 611.00 2014-11-15 IT
3 4 Ryan 729.00 2014-05-11 HR
4 5 Gary 843.25 2015-03-27 Finance
5 6 Rasmi 578.00 2013-05-21 IT
6 7 Pranab 632.80 2013-07-30 Operations
Vaishali Mahajan 4
Program 6: Write a program to draw line plots using matplotlib library.
import matplotlib.pyplot as plt
import numpy as np
x=np.array([2,3,5,6])
y=np.array([3,5,2,7])
plt.plot(x,y, marker='o', color='green')
plt.show()
Bar Graph
Histogram
Vaishali Mahajan 5
Program 8: Write a program to draw histogram charts
using matplotlib library
Syntax:
import numpy as np
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.show(x)
Program 10:
With Pyplot, you can use the scatter() function to draw a scatter plot.
The scatter() function plots one dot for each observation.
It needs two arrays of the same length, one for the values
of the x-axis, and one for values on the y-axis:
Syntax:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
Computer Vision (OpenCV)
Note: Before going to execute computer vision examples we need to execute opencv library command. i.e.
‘pip install opencv-python’. Afterwards in the new cell you can write the program to execute.
Program 11: Write a program to read and display images using the opencv cv2 library.
Source Code:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('E:\SVIS_Logo.jpg') #Load the image file into memory
plt.imshow(img)
plt.title('School')
plt.axis('off')
plt.show( )
Note: BGR stands for Blue (255, 0, 0), Green (0, 255, 0), Red (0, 0, 255). OpenCV uses BGR color as a
default color space to display images, when we open an image in openCV using cv2.
imread( ) it display the image in BGR format. And it provides color-changing methods using cv2.
Vaishali Mahajan 6
Output:
Program 12: Write a program to read and display original images using opencv cv2 library.
Source Code:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('E:\SVIS_Logo.jpg') #Load the image file into memory
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('School')
plt.axis('off')
plt.show( )
Program 13: Write a program to read and display gray color images using the opencv cv2 library.
Source Code:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('E:\SVIS_Logo.jpg',0)
# the number zero opens the image as a grayscale image
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
#cmap specifies color mapping, gray in this case.
plt.title('School')
plt.axis('off')
plt.show( )
Output:Gray in color
Vaishali Mahajan 7
Program 14: Write a program to crop the image using opencv cv2 library.
Source Code:
import cv2
import matplotlib.pyplot as plt
roi = img[0:100,80:180] #img[range of y, range of x]
plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))
plt.title('School')
plt.axis('off')
plt.show( )
Output: Crop the image
Program 15: Write a program to resize the image using opencv cv2 library.
Source Code:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('E:\SVIS_Logo.jpg')
resized = cv2.resize(img, (400, 250))
plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
plt.title('School')
plt.axis('on')
print(resized.shape)
plt.show( )
Output: To resize the image
*************************************************************************************
Vaishali Mahajan 8