Workspace
Martina Priatelová/

Introduction to Python

0
Beta
Spinner
# Convert positions and heights to numpy arrays: np_positions, np_heights
np_positions = np.array (positions)
np_heights = np.array (heights)

# Heights of the goalkeepers: gk_heights
gk_heights = np_heights [np_positions == "GK"]

# Heights of the other players: other_heights
other_heights = np_heights [np_positions != "GK"]

# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median (gk_heights)))

# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))
# Import numpy
import numpy as np

# Print mean height (first column)
avg = np.mean(np_baseball[:,0])
print("Average: " + str(avg))

# Print median height. Replace 'None'
med = np.median (np_baseball [:,0])
print("Median: " + str(med))

# Print out the standard deviation on height. Replace 'None'
stddev = np.std (np_baseball [:, 0])
print("Standard Deviation: " + str(stddev))

# Print out correlation between first and second column. Replace 'None'
corr = np.corrcoef (np_baseball [:,0], np_baseball [:, 1])
print("Correlation: " + str(corr))
# baseball is available as a regular list of lists

# Import numpy package
import numpy as np

# Create np_baseball (2 cols)
np_baseball = np.array(baseball)

# Print out the 50th row of np_baseball
print (np_baseball [49, :])

# Select the entire second column of np_baseball: np_weight_lb
np_weight_lb = np_baseball [:, 1]

# Print out height of 124th player
print (np_baseball [123, 0])

Introduction to Python

Run the hidden code cell below to import the data used in this course.

# Store weight and height lists as numpy arrays
np_weight_lb = np.array(weight_lb)
np_height_in = np.array(height_in)

# Print out the weight at index 50
print (np_weight_lb [50])

# Print out sub-array of np_height_in: index 100 up to and including index 110
print (np_height_in [100:111])
# height_in and weight_lb are available as a regular lists

# Import numpy
import numpy as np

# Calculate the BMI: bmi
np_height_m = np.array(height_in) * 0.0254
np_weight_kg = np.array(weight_lb) * 0.453592
bmi = np_weight_kg / np_height_m ** 2

# Create the light array
light = bmi < 21

# Print out light
print (light)

# Print out BMIs of all baseball players whose BMI is below 21
print (bmi [light])
# Definition of radius
r = 192500

# Import radians function of math package
from math import radians

# Travel distance of Moon over 12 degrees. Store in dist.
dist = r * radians (12)

# Print out dist
print (dist)

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

# string to experiment with: place
place = "poolhouse"

# Use upper() on place: place_up
place_up = place.upper () 

# Print out place and place_up
print (place_up)
print (place)
# Print out the number of o's in place
print (place.count("o"))
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Sum of kitchen and bedroom area: eat_sleep_area
eat_sleep_area = (areas [3] + areas [-3])

# Print the variable eat_sleep_area
print (eat_sleep_area)

Add your notes here

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# house information as list of lists
house = [["hallway", hall],
         ["kitchen", kit],
         ["living room", liv],
         ["bedroom", bed],
         ["bathroom", bath]]

# Print out house
print (house)

# Print out the type of house
print (type (house))
# Create lists first and second
first = [11.25, 18.0, 20.0]
second = [10.75, 9.50]

# Paste together first and second: full
full = first + second

# Sort full in descending order: full_sorted
full_sorted = sorted (full, reverse = True)

# Print out full_sorted
print (full_sorted)
# Add your code snippets here



  • AI Chat
  • Code