0% found this document useful (0 votes)
11 views

24-25 Gr-10-Ai Practical File Python Term-1 and Term-2!24!25 2

This document is a practical file for Grade 10 Artificial Intelligence, covering various programming topics including lists, tuples, dictionaries, and data visualization techniques using Python. It includes sample programs for creating and manipulating lists, drawing charts using Matplotlib, and working with CSV files. Additionally, it features exercises related to computer vision and natural language processing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

24-25 Gr-10-Ai Practical File Python Term-1 and Term-2!24!25 2

This document is a practical file for Grade 10 Artificial Intelligence, covering various programming topics including lists, tuples, dictionaries, and data visualization techniques using Python. It includes sample programs for creating and manipulating lists, drawing charts using Matplotlib, and working with CSV files. Additionally, it features exercises related to computer vision and natural language processing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

ARTIFICIAL INTELLIGENCE

PRACTICAL FILE
GRADE-10
Academic Year-2024-25

PREPARED BY
STUDENT NAME :
GRADE: SECTION:
INDEX

Sr.No TOPIC DATE REMARK SIGN


1. List MARCH

2. Tuple MARCH
3. Dictionary APRIL
4. Program on Graphs –Bar APRIL
Graphs
5. Pie chart(2) JUNE
6. Line chart(2) JUNE
7. CV- Image editing- 2 July
8. CV –Image –editing -2
9. CV –Image -editing
10. CV Image
11. CSV
12. CSV

List programs:
Q1. Create a empty list, Integer list and nested list
#empty list
empty_list = []
#list of integers
age = [15,12,18]
#list with mixed data types
student_height_weight = ["Ansh", 5.7, 60]
Note: A list can also have another list as an item. This is called nested lists.
# nested list student marks = ["Aditya", "10-A", [ "english",75]]
----------------------------------------------------------------------------------------------------
---
Q2. Write a program to explain how to access elements in list and nested
list
List Index
A list index is the position at which any element is present in the list. Index in the
list starts from 0, so if a list has 5 elements the index will start from 0 and go on
till 4. In order to access an element in a list we need to use index operator [].
Negative Indexing
Python allows negative indexing for its sequences. The index of -1 refers to the
last item, -2 to the second last item and so on.
In order to access elements using negative indexing, we can use the negative
index as mentioned in the above figure.

Q3.Write a program to add elements to elements to the list


Adding Element to a List
We can add an element to any list using two methods :
1) Using append() method
2) Using insert() method
3) Using extend() method
Using append() method
Q4.Write python program to remove or pop elements from the list .

Other list methods


HW:

Q5. Create a Tuple which stores fruit names


Q6.Write a program in Python that identifies a number is positive or
negative?

Q7.Program in Python to find the candidate is eligible to vote?


DATA VISUALIZATION
LINE CHART
8. Program to draw line chart with appropriate x and y axis label.

Plotting a line chart of date versus temperature


by adding Label on X and Y axis, and adding a
Title and Grids to the chart.
import matplotlib.pyplot as plt
date=["25/12","26/12","27/12"]
temp=[8.5,10.5,6.8]
plt.plot(date, temp,marker='*',markersize=10,
color='green',linewidth=2,
linestyle='dashdot',markerfacecolor=”y”,
markeredgecolor=”r”)
plt.xlabel("Date") #add the Label on x-axis
plt.ylabel("Temperature")
#add the Label on y-axis
plt.title("Date wise Temperature") #add the
title to the chart
plt.grid(True) #add gridlines to the
background
plt.savefig(r“C:\Users\gaurav\OneDrive\Deskt
op\Line.jpg”)
plt.show()
9.Line chart
height=[121.9,124.5,129.5,134.6,139.7,147.3,152.4,157.5,162.6]
weight=[19.7,21.3,23.5,25.9,28.5,32.1,35.7,39.6,43.2]

Let us plot a line chart where:


i. x axis will represent weight
ii. y axis will represent height
iii. x axis label should be “Weight in kg”
iv. y axis label should be “Height in cm”
v. colour of the line should be green
vi. use * as marker
vii. Marker size as10
viii. The title of the chart should be “Average
weight with respect to average height”.
ix. Line style should be dashed
x. Linewidth should be 2.

import matplotlib.pyplot as plt


import pandas as pd
height=[121.9,124.5,129.5,134.6,139.7,147.3,152.4,157.5,162.6]
weight=[19.7,21.3,23.5,25.9,28.5,32.1,35.7,39.6,43.2]
df=pd.DataFrame({"height":height,"weight":weight})
#Set xlabel for the plot
plt.xlabel('Weight in kg')
#Set ylabel for the plot
plt.ylabel('Height in cm')
#Set chart title:
plt.title('Average weight with respect to average height')
#plot using marker'-*' and line colour as green
plt.plot(weight,height,marker='*',markersize=10,color='green
',linewidth=2, linestyle='dashdot')
plt.show()

‘’’Main code
plt.plot(weight,height,marker='*',markersize=10,
color='green',linewidth=2, linestyle='dashdot')’’’
BAR CHART
10. Create a bar chart for the following data with appropriate x label, y label,
title.

Bar chart
import matplotlib.pyplot as plt

# Manual data setup


labels = ('Python', 'Java', 'JavaScript', 'C#', 'PHP', 'C,C++', 'R')
sizes = [29.9, 19.1, 8.2, 7.3, 6.2, 5.9, 3.7]

plt.bar(labels,size)
# layout configuration
plt.ylabel('Usage in %')
plt.xlabel('Programming Languages')

# Save the chart file


plt.savefig('filename.png')
# Print the chart
plt.show()

#barh for horizontal bar


# color= “R”, “B”,”G”
# edgecolor='Green',linewidth=2,linestyle='--'
PIE CHART
Q11.Draw a pie chart for following data :
import matplotlib.pyplot as plt

Num= [35, 25, 25, 15]


Fruits=["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(NUM,labels=Fruits,explode=myexplode,
shadow = True)
plt.show()
Giving Shadow and explode effect in pie chart

import matplotlib.pyplot as plt


import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels = mylabels, explode = myexplode, shadow = True)
plt.show()
Revision taken in lab

1.Program on List

List =[20,30,50,60,12,123,70]
List.append(40)
print(List)

List.extend([10,90])
print(List)

List.remove(40)
print(List)

List.pop(0)
print(List)

List.sort()
print(List)

List.sort(reverse=True)
print(List)

List.insert(90,2)
print(List)

2.Program on dictionary
Dictionary on rollno and name
dict1={"1":"A","2":"B","3":"C"}
print(dict1) #printing dictionary
print(dict1["1"]) #printing A through key 1
print(dict1["2"]) #printing B through key 2
dict1[“4”]= “D” # add element 4 to dictionary
print(dict1)
dict1[2]="X" # change value mutability property
print(dict1)
dict1.clear() # to clear the contents of dictionary

Note :Always print dictionary after adding/updating key and value


TERM-2 PRACTICAL FILE CONTENT
CV Program in Python
Q12. How to Display an OpenCV image in Python with Matplotlib?

# import required module

import cv2

# read the Image by giving path


image = cv2.imread('gfg.png')

# display that image


cv2.imshow('GFG', image)

OR
# import required module
import cv2
import matplotlib.pyplot as plt

# read image
image = cv2.imread('gfg.png')

# call imshow() using plt object


plt.imshow(image)

# display that image


plt.show()
Q13.CV Program
Grayscaling is the process of converting an image from other color spaces e.g.
RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and
complete white.

# import required modules


import cv2
import matplotlib.pyplot as plt

# read the image


image = cv2.imread('Tomatoes.png')

# convert color image into grayscale image


img1 = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# plot that grayscale image with Matplotlib


# cmap stands for colormap
plt.imshow(img1, cmap='gray')

# display that image


plt.show()
Q14. # Import opencv
import cv2

# Use the second argument or (flag value) zero


# that specifies the image is to be read in grayscale mode
img = cv2.imread('C:\\Documents\\full_path\\tomatoes.jpg', 0)
cv2.imshow('Grayscale Image', img)
cv2.waitKey(0)
# Window shown waits for any key pressing event
cv2.destroyAllWindows()

Q15. Drawing a point on the image.


from matplotlib import image
from matplotlib import pyplot as plt

# to read the image stored in the working directory


data = image.imread('sunset-1404452-640x480.jpg')

# to draw a point on co-ordinate (200,300)


plt.plot(200, 350, marker='v', color="white")
plt.imshow(data)
plt.show()
Q16. Draw a line on the image
from matplotlib import image
from matplotlib import pyplot as plt

# to read the image stored in the working directory


data = image.imread('sunset-1404452-640x480.jpg')

# to draw a line from (200,300) to (500,100)


x = [200, 500]
y = [300, 100]
plt.plot(x, y, color="white", linewidth=3)
plt.imshow(data)
plt.show()
Q17.Draw two intersecting lines crossing each other to make X.
● Python3

from matplotlib import image


from matplotlib import pyplot as plt

# to read the image stored in the working directory


data = image.imread('sunset-1404452-640x480.jpg')

# to draw first line from (100,400) to (500,100)


# to draw second line from (150,100) to (450,400)
x1 = [100, 500]
y1 = [400, 100]
x2 = [150, 450]
y2 = [100, 400]
plt.plot(x1, y1, x2, y2, color="white", linewidth=3)
plt.axis('off')
plt.imshow(data)
plt.show()

Output :
CSV FILES

A CSV (Comma Separated Values) file is a form of plain text document which uses
a particular format to organize tabular information. CSV file format is a bounded
text document that uses a comma to distinguish the values. Every row in the
document is a data log. Each log is composed of one or more fields, divided by
commas. It is the most popular file format for importing and exporting
spreadsheets and databases.

Reading a CSV File

There are various ways to read a CSV file that uses either the CSV module or the
pandas library.
· csv Module: The CSV module is one of the modules in Python which
provides classes for reading and writing tabular information in CSV
file format.
· pandas Library: The pandas library is one of the open-source Python
libraries that provide high-performance, convenient data structures
and data analysis tools and techniques for Python programming.

Program-18 Write a Python program to read a CSV file :

# opening the CSV file

with open('Giants.csv', mode ='r')as file:

# reading the CSV file

csvFile = csv.reader(file)

# displaying the contents of the CSV file


for lines in csvFile:

print(lines)

(OR)

import csv

f= open

'C:\\Users\\ICT 02\\Desktop\\SUDHEER.csv', mode ='r')

csvFile = csv.reader(f)

for lines in csvFile:

print(lines)

Output:

['Organization', 'CEO', 'Established']

['Alphabet', 'Sundar Pichai', '02-Oct-15']

['Microsoft', 'Satya Nadella', '04-Apr-75']

['Amazon', 'Jeff Bezos', '05-Jul-94']

OR

Using pandas.read_csv() method: It is very easy and simple to read a CSV file
using pandas library functions. Here read_csv() method of pandas library is used to
read data from CSV files.

import pandas

# reading the CSV file

csvFile = pandas.read_csv('Giants.csv')

# displaying the contents of the CSV file

print(csvFile)
Program-19

Write a Pandas program to read a csv file from a specified source and print the
first 5 rows.

Sample Solution :

Python Code :

import pandas as pd

pd.set_option('display.max_rows', 50)

pd.set_option('display.max_columns', 50)

diamonds = pd.read_csv('C://AI/Gr 10/Practical file/diamonds.csv')

print("First 5 rows:")

print(diamonds.head())

Sample Output:

First 5 rows:

carat cut color clarity depth table price x y z

0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43

1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31

2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31

3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63

4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2


NLP Programs(extra)
Q20. Program to print Stop words in English

import nltk
from nltk.corpus import stopwords
print(stopwords.words('english'))
{‘ourselves’, ‘hers’, ‘between’, ‘yourself’, ‘but’, ‘again’, ‘there’, ‘about’, ‘once’,
‘during’, ‘out’, ‘very’, ‘having’, ‘with’, ‘they’, ‘own’, ‘an’, ‘be’, ‘some’, ‘for’, ‘do’, ‘its’,
‘yours’, ‘such’, ‘into’, ‘of’, ‘most’, ‘itself’, ‘other’, ‘off’, ‘is’, ‘s’, ‘am’, ‘or’, ‘who’, ‘as’,
‘from’, ‘him’, ‘each’, ‘the’, ‘themselves’, ‘until’, ‘below’, ‘are’, ‘we’, ‘these’, ‘your’, ‘his’,
‘through’, ‘don’, ‘nor’, ‘me’, ‘were’, ‘her’, ‘more’, ‘himself’, ‘this’, ‘down’, ‘should’, ‘our’,
‘their’, ‘while’, ‘above’, ‘both’, ‘up’, ‘to’, ‘ours’, ‘had’, ‘she’, ‘all’, ‘no’, ‘when’, ‘at’, ‘any’,
‘before’, ‘them’, ‘same’, ‘and’, ‘been’, ‘have’, ‘in’, ‘will’, ‘on’, ‘does’, ‘yourselves’,
‘then’, ‘that’, ‘because’, ‘what’, ‘over’, ‘why’, ‘so’, ‘can’, ‘did’, ‘not’, ‘now’, ‘under’, ‘he’,
‘you’, ‘herself’, ‘has’, ‘just’, ‘where’, ‘too’, ‘only’, ‘myself’, ‘which’, ‘those’, ‘i’, ‘after’,
‘few’, ‘whom’, ‘t’, ‘being’, ‘if’, ‘theirs’, ‘my’, ‘against’, ‘a’, ‘by’, ‘doing’, ‘it’, ‘how’,
‘further’, ‘was’, ‘here’, ‘than’}
Note: You can even modify the list by adding words of your choice in the english
.txt. file in the stopwords directory.

You might also like