University Institute of Engineering Department of Computer Science & Engineering
University Institute of Engineering Department of Computer Science & Engineering
University Institute of Engineering Department of Computer Science & Engineering
Experiment: 1.1
2. Tool Used:
Google Colaboratory
4. Code:
a) file handling
try:
fp = open('abc.txt') # Open the file in reading mode
for line in fp: # Print line by line
print (line)
fp.close() # Close the file
except:
print("Error!! No such file exist")
try:
with open('abc.txt', 'r') as f:
line = f.read()
print (line)
except:
print("Error!! No such file exist")
fp = open('textdata.txt','w') # Open the file in writing mode
for i in range(1,11):
fp.write(str(i) + "\n") # Writing to the file line by line
fp.close()
print ("Writing done !! \nOpen result.txt to view the content")
b)data manipulation
import pandas as pd
a1 = [1, 3, 5, 7, 9, 2, 4, 6, 8]
a2 = pd.Series(a1)
print(a2)
import pandas as pd
a1 = [1, 3, 5, 7, 9, 2, 4, 6, 8]
University Institute of Engineering
Department of Computer Science & Engineering
a2 = ['a','b','c','d','e','f','g','h','i']
a3 = pd.Series(a1, a2)
#a3 = pd.Series(a2, a1)
print(a3
import pandas as pd
d1 = {'Oranges':3, 'Apples':4, 'Mangoes':2, 'Banana':12}
d2 = pd.Series(d1)
print (d2)
print (type(d2))
import pandas as pd
a1 = [[1,3,5],[2,4,6]]
a2 = pd.Series(a1)
print (a2)
c)data visualization
Line Plot
x = [0, 1, 2, 3, 4, 5, 6]
y = [i **2 for i in x ]
plt.plot(x, y)
plt.show()
y1 = [50, 40, 70, 80, 20]
y2 = [80, 20, 20, 50, 60]
y3 = [70, 20, 60, 40, 60]
University Institute of Engineering
Department of Computer Science & Engineering
y4 = [80, 20, 20, 50, 60]
plt.plot(x, y1, 'g', label='Enfield', linewidth=2)
plt.plot(x, y2, 'c', label='Honda', linewidth=1)
plt.plot(x, y3, 'k', label='Yahama', linewidth=3)
plt.plot(x, y4, 'y', label='KTM', linewidth=2)
plt.title('Bike details in line plot')
plt.ylabel('Distance in kms')
plt.xlabel('Days')
plt.legend() #Adding Legends
plt.show()
Bar Plot
x = [50, 60, 30, 70, 20]
y = ["A", "B", "C", "D", "E"]
plt.bar(y, x, color = "green")
plt.show()
plt.bar(x1, y1, label="BMW", color='r')
x2 = [.75, 1.75, 2.75, 3.75, 4.75]
y2 = [80, 20, 20, 50, 60]
plt.bar(x2, y2, label="Audi", color='y')
University Institute of Engineering
Department of Computer Science & Engineering
plt.xlabel('Days')
plt.ylabel('Distance (kms)')
plt.title('Information')
plt.legend()
plt.show()
Scatter plot
x = [35, 20, 29, 40, 57] # x-axis values
y = [100, 50, 80, 40, 200] # Y-axis values
plt.scatter(x, y) # Function to plot scatter
plt.xlabel('Salary * 1000')
plt.ylabel('Age)')
plt.title('Age Vs Salary')
plt.show() # function to show the plot
x2 = [8, 8.5, 9, 9.5, 10, 10.5, 11]
y2 = [3, 3.5, 3.7, 4, 4.5, 5, 5.2]
plt.scatter(x1, y1, label='High income low saving', color='r')
plt.scatter(x2, y2, label='Low income high savings', color='b')
plt.xlabel('Saving*100')
plt.ylabel('Income*1000')
plt.title('Scatter Plot')
plt.legend()
plt.grid()
plt.show()
University Institute of Engineering
Department of Computer Science & Engineering
Pie plot
share = [3,4,2,1]
bikes = ['Enfield','Honda','Yahama','KTM']
plt.pie(share, labels=bikes, shadow= True, explode=(0,0.1,0,0),
autopct='%1.1f%%')
plt.title('Bike Shares')
plt.show()
a)file handling
1)hello world
2)hello world
3) Writing done !!
b)data manipulation
0 1
1 3
2 5
3 7
4 9
5 2
6 4
7 6
8 8
dtype: int64
a 1
b 3
c 5
d 7
e 9
University Institute of Engineering
Department of Computer Science & Engineering
f 2
g 4
h 6
i 8
dtype: int64
Oranges 3
Apples 4
Mangoes 2
Banana 12
dtype: int64
<class 'pandas.core.series.Series'>
0 [1, 3, 5]
1 [2, 4, 6]
dtype: object
CodeText
3)Data visualization
University Institute of Engineering
Department of Computer Science & Engineering
University Institute of Engineering
Department of Computer Science & Engineering
University Institute of Engineering
Department of Computer Science & Engineering
1.Remember the concepts related to Pandas,create Dataframes and write csv files.
5.Design and develop modular programs for real-world problems using control structure and selection
structure.