PYTHON PRACTICAL FILE
SUBMITTED TO: POOJA MAM
NAME: TARUN SAHU
CLASS: 10-B
ROLL NO.: 32
SUBJECT: ARTIFICIAL INTELLIGENCE (AI)
Program 1: Add Elements of Two Lists
Aim: To write a Python program that adds the corresponding elements of two lists.
Activity: Addition of elements of two lists using a loop.
Concept: Lists in Python can store multiple values and we can access elements using their
index. We use a loop to iterate over both lists and add corresponding elements.
Code:
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)
Output:
Sum of corresponding elements: [11, 22, 33, 44, 55]
Program 2: Calculate Mean, Median and Mode using NumPy
Aim: To calculate the mean, median and mode of a dataset using NumPy and SciPy.
Activity: Statistical calculations using NumPy and SciPy.
Concept: Mean: The average value.
Median: The middle value when data is sorted.
Mode: The most frequently occurring value.
Code:
import numpy as np
from scipy import stats
data = [10, 20, 20, 30, 40, 50, 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[0])
Output:
Mean: 36.67
Median: 40.0
Mode: [50]
Program 3: Display a Line Chart
Aim: To plot a line chart using Matplotlib in Python.
Activity: Creating a line chart using a set of given points.
Concept: A line chart represents data points connected by a continuous line. We use the
plot() function from Matplotlib to draw the graph.
Code:
import matplotlib.pyplot as plt
x = [2, 3, 4, 5, 6, 7, 8, 9]
y = [5, 6, 7, 8, 9, 9.5, 10, 10]
plt.plot(x, y, marker='o', linestyle='-', color='b')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Line Chart")
plt.show()
Output:
A line chart is displayed.
Program 4: Display a Scatter Chart
Aim: To create a scatter plot using Matplotlib.
Activity: Plotting a scatter chart using given points.
Concept: A scatter plot is used to show the relationship between two variables. The
scatter() function is used for this purpose.
Code:
import matplotlib.pyplot as plt
points = [(2,5), (9,10), (8,3), (5,7), (6,18)]
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()
Output:
A scatter chart is displayed.
Program 5: Read a CSV File and Display 10 Rows
Aim: To read a CSV file and display the first 10 rows using Pandas.
Activity: Reading a CSV file and displaying its content.
Concept: CSV files store tabular data. Pandas read_csv() is used to read the file and head(10)
displays the first 10 rows.
Code:
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head(10))
Output:
First 10 rows of CSV file are displayed.
Program 6: Read a CSV File and Display Its Information
Aim: To read a CSV file and display its structure and details using Pandas.
Activity: Reading a CSV file and displaying its information.
Concept: Pandas’ info() function provides details about column names, data types and
missing values.
Code:
import pandas as pd
df = pd.read_csv("data.csv")
print(df.info())
Output:
Information about dataset (columns, datatypes, null values).
Program 7: Read an Image and Display It
Aim: To read and display an image using OpenCV and Matplotlib.
Activity: Loading an image and displaying it.
Concept: OpenCV (cv2.imread) is used to read images while Matplotlib (plt.imshow)
displays them.
Code:
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.show()
Output:
The image is displayed.
Program 8: Read an Image and Identify Its Shape
Aim: To read an image and determine its height, width and number of channels.
Activity: Reading an image and extracting its dimensions.
Concept: An image consists of pixels arranged in rows and columns. The shape attribute in
OpenCV provides this information.
Code:
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)
Output:
Image Height: 720
Image Width: 1280
Number of Channels: 3