0% found this document useful (0 votes)
16 views8 pages

Exp 4 To 10

The document describes 10 experiments related to file operations, reading and writing data to files, working with dictionaries in Python, using Pandas to work with dataframes, computing statistics like mean, standard deviation and variance of arrays, calculating covariance matrices, and generating histograms. Each experiment provides sample Python code to demonstrate the concept along with expected output.

Uploaded by

lalitlawaniya2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Exp 4 To 10

The document describes 10 experiments related to file operations, reading and writing data to files, working with dictionaries in Python, using Pandas to work with dataframes, computing statistics like mean, standard deviation and variance of arrays, calculating covariance matrices, and generating histograms. Each experiment provides sample Python code to demonstrate the concept along with expected output.

Uploaded by

lalitlawaniya2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment 4: Write a program to perform different file operations.

a) Write a program to store the information of N students in a file.

import pickle

file=open("newbfile.dat","ab")

d={}

a=int(input("enter range"))

for i in range(a):

d["Rollno"]=int(input("enter rollno"))

d["name"]=input("enter name")

d["marks"]=int(input("enter marks"))

pickle.dump(d,file)

print("written successfully")

file.close()

Output:
Experiment 5: Write a program to read the information of students from a file and print them on
Screen.

import pickle

file=open("newbfile.dat","rb")

try:

while True:

l=pickle.load(file)

print(l)

except EOFError:

pass

file.close()

Output:
Experiment 6. Write the Programs to show the operation using Pandas.

a)

b)
Experiment 7.: Write a program to demonstrate working with dictionaries in python.

college = { "name": "Chandigarh University", "code": "CU", "id": "CU1234" }

print(college)

college["location"] = "Kharar"

print(college)

college["location"] = "Gharuan"

print(college)

college.pop("code")

print(college)

print("length of college is:",len(college))

mycollege= college.copy()

print(mycollege)

Output:
7. (b) Create a program to show the performance of Batsmen using Dictionary.

import pandas as pd
squad = {'Batsmen': {'Rohit Sharma': {'Matches': 206,
'Runs': 8010,
'Average':47.4,
'Highest Score': 264 },
'Shikhar Dhawan': {'Matches':128,
'Runs': 5355,
'Average': 44.62,
'Highest Score': 143},
'Virat Kohli': {'Matches': 227,
'Runs': 10843,
'Average': 59.58,
'Highest Score': 183}}
df = pd.DataFrame(squad['Batsmen'])
print(df)

Output:
Experiment 8: Program to compute the mean, standard deviation, and variance of a given array
along the second axis.

import numpy as np

# Original array

x = np.arange(5)

print(x)

r11 = np.mean(x)

r12 = np.average(x)

print("\nMean: ", r11, r12)

r21 = np.std(x)

r22 = np.sqrt(np.mean((x - np.mean(x)) ** 2))

print("\nstd: ", r21, r22)

r31 = np.var(x)

r32 = np.mean((x - np.mean(x)) ** 2)

print("\nvariance: ", r31, r32)


Experiment 9. Covariance matrix of two given arrays

import numpy as np

array1 = np.array([1, 2])

array2 = np.array([1, 2])

# Original array1

print(array1)

# Original array2

print(array2)

# Covariance matrix

print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))


Experiment 10. Compute the histogram of nums against the bins.

import numpy as np

import matplotlib.pyplot as plt

nums = np.array([0.5, 0.7, 1.0, 1.2, 1.3, 2.1])

bins = np.array([0, 1, 2, 3])

print("nums: ",nums)

print("bins: ",bins)

print("Result:", np.histogram(nums, bins))

plt.hist(nums, bins=bins)

plt.show()

You might also like