Class 10 Python Practicals
Program 1: Add Elements of Two Lists
Aim: To write a Python program that adds the corresponding elements of two lists.
Program:
list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]
sum_list = []
for i in range(len(list1)):
sum_list.append(list1[i] + list2[i])
print("Sum of corresponding elements:", sum_list)
Sample Output:
Sum of corresponding elements: [11, 22, 33, 44, 55]
Result: The program successfully adds corresponding elements of two lists.
Program 2: Calculate Mean, Median and Mode using NumPy
Aim: To calculate the mean, median and mode of a dataset using NumPy and SciPy.
Program:
import numpy as np
from scipy import stats
data = [10, 20, 20, 30, 40, 50, 50, 60]
mean_value = np.mean(data)
median_value = np.median(data)
mode_value = stats.mode(data)
print("Mean:", mean_value)
print("Median:", median_value)
print("Mode:", mode_value.mode[0])
Sample Output:
Mean: 35.0
Median: 35.0
Mode: 20
Result: The program successfully calculates mean, median and mode of the dataset.
Program 3: Display a Line Chart
Aim: To plot a line chart from (2,5) to (9,10).
Program:
import matplotlib.pyplot as plt
x = [2, 3, 5, 7, 9]
y = [5, 7, 6, 8, 10]
plt.plot(x, y, marker='o', linestyle='-', color='b')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Line Chart")
plt.show()
Sample Output:
A line chart is displayed with given points.
Result: The program successfully displays a line chart using matplotlib.
Program 4: Display a Scatter Chart
Aim: To create a scatter plot using Matplotlib.
Program:
import matplotlib.pyplot as plt
points = [(2,5), (9,10), (8,3), (5,7), (6,10)]
x, y = zip(*points)
plt.scatter(x, y, color='r', marker='o')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Scatter Chart")
plt.show()
Sample Output:
A scatter plot is displayed with the given points.
Result: The program successfully displays a scatter chart using matplotlib.
Program 5: Read a CSV File and Display 10 Rows
Aim: To read a CSV file and display the first 10 rows using Pandas.
Program:
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head(10))
Sample Output:
Displays the first 10 rows of the CSV file.
Result: The program successfully reads a CSV file and displays 10 rows.
Program 6: Read a CSV File and Display Its Information
Aim: To read a CSV file and display its structure and details using Pandas.
Program:
import pandas as pd
df = pd.read_csv("data.csv")
print(df.info())
Sample Output:
Displays column names, non-null counts, and data types.
Result: The program successfully displays the information of the CSV file.
Program 7: Read an Image and Display It
Aim: To read and display an image using OpenCV and Matplotlib.
Program:
import cv2
import matplotlib.pyplot as plt
image = cv2.imread("image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.axis("off")
plt.title("Image Display")
plt.show()
Sample Output:
Displays the image in a window.
Result: The program successfully reads and displays an image.
Program 8: Read an Image and Identify Its Shape
Aim: To read an image and determine its height, width, and number of channels.
Program:
import cv2
image = cv2.imread("image.jpg")
(height, width, channels) = image.shape
print("Image Height:", height)
print("Image Width:", width)
print("Number of Channels:", channels)
Sample Output:
Image Height: 720
Image Width: 1280
Number of Channels: 3
Result: The program successfully identifies the shape of the image.