24-25 Gr-10-Ai Practical File Python Term-1 and Term-2!24!25 2
24-25 Gr-10-Ai Practical File Python Term-1 and Term-2!24!25 2
PRACTICAL FILE
GRADE-10
Academic Year-2024-25
PREPARED BY
STUDENT NAME :
GRADE: SECTION:
INDEX
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.
‘’’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
plt.bar(labels,size)
# layout configuration
plt.ylabel('Usage in %')
plt.xlabel('Programming Languages')
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
import cv2
OR
# import required module
import cv2
import matplotlib.pyplot as plt
# read image
image = cv2.imread('gfg.png')
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.
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.
csvFile = csv.reader(file)
print(lines)
(OR)
import csv
f= open
csvFile = csv.reader(f)
print(lines)
Output:
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
csvFile = pandas.read_csv('Giants.csv')
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)
print("First 5 rows:")
print(diamonds.head())
Sample Output:
First 5 rows:
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.