Lab 2 Numpy Arrays PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Lab 2

Lists, Numpy and


Functions
Mahrukh Khan
•Name a collection of values
•Contain any type
•Contain different types
Issues with List
Exercise
1) Given two lists,
first=[11.25, 18.0, 20.0, 10.75, 9.50]
second = [10.75, 9.50]
Use + to merge the contents into a new list named full. Call sorted()
function on full and specify the reverse argument to be True. Save the sorted list
as full_sorted. And print it.Use append() to add the elements 24.5 and 15.45 to
the full_sorted list. Print the list. Use the reverse() method to reverse the order
of the elements in list. Print out list once more.
2) Import the math package so that you can access the constant pi with math.pi.
Calculate the circumference and area of the circle .
Exercise(1)
3) Given are the height and weight of the main players of MLB (Major
League Baseball). And as a Data Scientist, you have been asked to
provide some statistics.
Height= [74, 74, 72, 72, 73, 69, 69, 71, 76, 71, 73, 73, 74, 74, 69, 70, 73, 75, 78, 79, 76, 74, 76, 72, 71, 75,
77, 74, 73, 74, 78, 73, 75, 73]
Weight= [180, 195, 195, 190, 230, 190, 200, 190, 190, 200, 200, 184, 200, 180, 219, 187, 200, 220, 205,
190, 170, 160, 215, 175, 205, 200, 214, 200, 190, 180, 205, 220, 190, 215]
Import the numpy package as np, so that you can refer to numpy with np.
Use np.array() to create a numpy array from height and weight .Convert all
height measurements from inches to meters and weight from pounds to
kilograms.(Hint: Multiply np_height with 0.0254 and np_weight)with 0.453592).
Calculate the BMI of each player. Use the following equation:
Exercise(2)

4) Create a 2D numpy array named ‘nplist2d’ from the list


list2d = [[180, 78.4], [215, 102.7], [210, 98.5], [188, 75.2]]
Print out the type and shape of list2d.
Print out the second column of the list.
Print out the 3rd row and 0th column.
Create numpy array np_height that is equal to first .column of the list .
Print out the mean and median of np_height.
Use np.std() on the np_height to calculate standard deviation.

You might also like