example :1Write a program to display line chart from (2,5) to (9,10).
import matplotlib.pyplot as plt
# Define the points
x_values = [2, 9]
y_values = [5, 10]
# Create the line chart
plt.plot(x_values, y_values,color='purple', marker='*')
# Add labels and title
plt.title('Line Chart from (2,5) to (9,10)')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Set the limits for x and y axes
plt.xlim(0, 10)
plt.ylim(0, 12)
# Show grid
plt.grid()
# Display the plot
plt.show() OUTPUT:
#example:2 calculate BMI USING NUMPY ARRAY
# Create 2 new lists height and weight
height = [1.87, 1.87, 1.82, 1.91, 1.90, 1.85]
weight = [81.65, 97.52, 95.25, 92.98, 86.18, 88.45]
print(type(height))
# Import the numpy package as np
import numpy as np
# Create 2 numpy arrays from height and weight
np_height = np.array(height)
np_weight = np.array(weight)
print(type(np_height))
print(np_height)
print(np_weight)
# Calculate bmi
bmi = np_weight / np_height ** 2
# Print the result
print(bmi)
OUTPUT:
<class 'list'>
<class 'numpy.ndarray'>
[1.87 1.87 1.82 1.91 1.9 1.85]
[81.65 97.52 95.25 92.98 86.18 88.45]
[23.34925219 27.88755755 28.75558507 25.48723993 23.87257618 25.84368152]
3.Write a program to display a scatter chart for the following points (2,5),
(9,10),(8,3),(5,7),(6,18).
import matplotlib.pyplot as plt
# Define the points
x_values = [2, 9, 8, 5, 6]
y_values = [5, 10, 3, 7, 18]
# Create the scatter chart
plt.scatter(x_values, y_values, color='green', marker='*')
# Add labels and title
plt.title('Scatter Chart for Given Points')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Set the limits for x and y axes
plt.xlim(0, 10)
plt.ylim(0, 20)
# Show grid
plt.grid()
# Display the plot
plt.show() OUTPUT:
4.Write a program to calculate mean, median and mode using Numpy.
import numpy as np
import pandas as pd
# create a sample salary table
salary = pd.DataFrame({
'employee_id': ['001', '002', '003', '004', '005', '006', '007',
'008', '009', '010'],
'salary': [50000, 65000, 55000, 45000, 70000, 60000, 55000, 45000,
80000, 70000]
})
print(salary)
# calculate mean
mean_salary = np.mean(salary['salary'])
print('Mean salary:', mean_salary)
# calculate median
median_salary = np.median(salary['salary'])
print('Median salary:', median_salary)
# calculate mode
mode_salary = salary['salary'].mode()[0]
print('Mode salary:', mode_salary)
output:
employe_id salary
0 001 50000
1 002 65000
2 003 55000
3 004 45000
4 005 70000
5 006 60000
6 007 55000
7 008 45000
8 009 80000
9 010 70000
Mean salary: 59500.0
Median salary: 57500.0
Mode salary: 45000
5. Write a program to add the elements of the two lists.
import numpy as np
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
#print(test_list1)
print("Original list 1 : " + str(test_list1))
print("Original list 2 : " + str(test_list2))
# using numpy.sum() to add two lists
res_array = np.array(test_list1) + np.array(test_list2)
print("Resultant array is : " + str(res_array))
res_list = res_array.tolist()
# printing resultant list
print("Resultant list is : " + str(res_list))
output:
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant array is : [ 5 8 10 8 18]
Resultant list is : [5, 8, 10, 8, 18]
6. # To calculate Area and Perimeter of a rectangle
L=int(input("Length"))
B=int(input("Breadth"))
Area=L*B
Perimeter=2*(L+B)
print("area:", Area)
print("Perimeter:",Perimeter)
output:
Length 15
Breadth 17
area: 255
Perimeter: 64
7. #sample program for string data types
str="artificial+intelligence"
print(str)
print(str[0]);
print(str[2:8])
print(str[4:9])
print(str[3])
print(str[3:])
print(str*2)
print(str+'good morning')
#updating string
var1='python programming'
print(var1)
print(var1[:12])
print("updated string:",var1[:18]+'language')
output:
artificial+intelligence
a
tifici
ficia
i
ificial+intelligence
artificial+intelligenceartificial+intelligence
artificial+intelligencegood morning
python programming
python progr
updated string: python programminglanguage
8. #sample program for list data types
list=[29,750,37,12,345,67,89,67,78,89]
list1=['home','sweet ']
print(list)
print(list[0]);
print(list[2:8])
print(list[3:9])
print(list[3])
print(list[3:])
print(list1*2 )
print(list+list1)
output:
[29, 750, 37, 12, 345, 67, 89, 67, 78, 89]
29
[37, 12, 345, 67, 89, 67]
[12, 345, 67, 89, 67, 78]
12
[12, 345, 67, 89, 67, 78, 89]
['home', 'sweet ', 'home', 'sweet ']
[29, 750, 37, 12, 345, 67, 89, 67, 78, 89, 'home', 'sweet ']
9. # sample program for list function()
list=[10,20,30,40,50,60]
print(list)
list.append(70)
print(list)
list.pop(4)
print(list)
list.count(60)
print(list)
list.reverse()
print(list)
list.sort()
print(list)
output:
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 70]
[10, 20, 30, 40, 60, 70]
1
[70, 60, 40, 30, 20, 10]
[10, 20, 30, 40, 60, 70]
10. #sample program for tuple data types
tuple=('physics',786,2.25,'chemistry',750,10,20,30,40)
print(tuple);
tuple1=('computer',999)
print(tuple[0]);
print(tuple[2:8])
print(tuple[3:9])
print(tuple[3])
print(tuple[3:])
print(tuple*2 )
print(tuple+tuple1)
output:
('physics', 786, 2.25, 'chemistry', 750, 10, 20, 30, 40)
physics
(2.25, 'chemistry', 750, 10, 20, 30)
('chemistry', 750, 10, 20, 30, 40)
chemistry
('chemistry', 750, 10, 20, 30, 40)
('physics', 786, 2.25, 'chemistry', 750, 10, 20, 30, 40, 'physics', 786, 2.25,
'chemistry', 750, 10, 20, 30, 40)
('physics', 786, 2.25, 'chemistry', 750, 10, 20, 30, 40, 'computer', 999)
11. #sample program for condition statement(if…elif…)
a = int(input("Enter a number : "))
if a%2 == 0 and a >50:
# Even and > 50
print("Your number is even and greater than 50.")
elif a%2 == 0 and a <50:
# Even and < 50
print("Your number is even and smaller than 50.")
elif a%2 == 0 and a == 50:
# Even and == 50
print("Your number is even and equal to 50.")
else:
print ("Your number is odd.",)
output:
Enter a number : 204
Your number is even and greater than 50.
12. #sample program for ( dictionary')
dict={}
dict['one']='this is dictionary'
dict[2]='python programming'
tinydict={'name':'priya','code':1080,'dept':'admin'}
print(dict['one'])
print(dict[2])
print(tinydict)
print(tinydict.keys())
print(tinydict.values())
output:
this is dictionary
python programming
{'name': 'priya', 'code': 1080, 'dept': 'admin'}
dict_keys(['name', 'code', 'dept'])
dict_values(['priya', 1080, 'admin'])
13.# sample program for BAR GRAPH
import matplotlib.pyplot as plt
Info = ['gold', 'Silver', 'Bronze', 'Total']
Australia =[80, 59, 59,198]
plt.bar(Info, Australia)
plt.ylim(0, 200)
plt.xlabel("Medal type")
plt.ylabel("Australia Medal count")
plt.show( )
OUTPUT:
14. # sample program IN Python USING CSV FILE
# Import pandas as pd
import pandas as pd
# Import the cars.csv data: cars
cars = pd.read_csv("cars.csv",sep=',')
# Display the first 10 rows
print(cars.head(10))
OUTPUT:
Car Name Model Price
0 Toyota Camry 24000
1 Honda Civic 22000
2 Ford Focus 19000
3 Chevrolet Malibu 23000
4 Nissan Altima 25000
5 Hyundai Elantra 21000
6 Volkswagen Jetta 20000
7 Subaru Impreza 23000
8 Kia Forte 21000
9 Mazda CX-5 27000
15. # Simple program in Python USING PANDAS
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"],
"capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],
"area": [8.516, 17.10, 3.286, 9.597, 1.221],
"population": [200.4, 143.5, 1252, 1357, 52.98] }
import pandas as pd
br = pd.DataFrame(dict)
print(br)
# Set the index for brics
br.index = ["BR", "RU", "IN", "CH", "SA"]
# Print out brics with new index values
print(br)
OUTPUT:
country capital area population
0 Brazil Brasilia 8.516 200.40
1 Russia Moscow 17.100 143.50
2 India New Dehli 3.286 1252.00
3 China Beijing 9.597 1357.00
4 South Africa Pretoria 1.221 52.98
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Dehli 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98