Skip to content
Introduction to Python
Introduction to Python
Run the hidden code cell below to import the data used in this course.
# Importing course packages; you can add more too!
import numpy as np
import math
# Import columns as numpy arrays
baseball_names = np.genfromtxt(
fname="baseball.csv", # This is the filename
delimiter=",", # The file is comma-separated
usecols=0, # Use the first column
skip_header=1, # Skip the first line
dtype=str, # This column contains strings
)
baseball_heights = np.genfromtxt(
fname="baseball.csv", delimiter=",", usecols=3, skip_header=1
)
baseball_weights = np.genfromtxt(
fname="baseball.csv", delimiter=",", usecols=4, skip_header=1
)
baseball_ages = np.genfromtxt(
fname="baseball.csv", delimiter=",", usecols=5, skip_header=1
)
soccer_names = np.genfromtxt(
fname="soccer.csv",
delimiter=",",
usecols=1,
skip_header=1,
dtype=str,
encoding="utf",
)
soccer_ratings = np.genfromtxt(
fname="soccer.csv",
delimiter=",",
usecols=2,
skip_header=1,
encoding="utf",
)
soccer_positions = np.genfromtxt(
fname="soccer.csv",
delimiter=",",
usecols=3,
skip_header=1,
encoding="utf",
dtype=str,
)
soccer_heights = np.genfromtxt(
fname="soccer.csv",
delimiter=",",
usecols=4,
skip_header=1,
encoding="utf",
)
soccer_shooting = np.genfromtxt(
fname="soccer.csv",
delimiter=",",
usecols=8,
skip_header=1,
encoding="utf",
)Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
Add your notes here
# Add your code snippets here
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
# Adapt list areas
areas = ["hallway", hall, "kitchen", kit, "living room", liv, "bedroom", bed, "bathroom", bath]
# Print areas
print(areas)Explore Datasets
Use the arrays imported in the first cell to explore the data and practice your skills!
- Print out the weight of the first ten baseball players.
- What is the median weight of all baseball players in the data?
- Print out the names of all players with a height greater than 80 (heights are in inches).
- Who is taller on average? Baseball players or soccer players? Keep in mind that baseball heights are stored in inches!
- The values in
soccer_shootingare decimals. Convert them to whole numbers (e.g., 0.98 becomes 98). - Do taller players get higher ratings? Calculate the correlation between
soccer_ratingsandsoccer_heightsto find out! - What is the average rating for attacking players (
'A')?
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Create areas_copy
## areas_copy = areas
### areas_copy = list(areas)
areas_copy = areas[:]
# Change areas_copy
areas_copy[0] = 5.0
# Print areas
print(areas)on inner workings of list. you can use list() or [:]
# string to experiment with: place
place = "poolhouse"
# Use upper() on place: place_up
place_up = place.upper() #capitalize all letters, changing to all caps
# Print out place and place_up
print(place)
print(place_up)
# Print out the number of o's in place
print(place.count('o'))methods
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Use append twice to add poolhouse and garage size
areas.append("poolhouse")
areas.append("garage size")
# Print out areas
print(areas)
# Reverse the orders of the elements in areas
areas.reverse() #note: always add ()
# Print out areas
print(areas)# Definition of radius
r = 0.43
# Import the math package
import math
# Calculate C
C = 2 * math.pi * r
# Calculate A
A = math.pi * (r ** 2)
# Build printout
print("Circumference: " + str(C))
print("Area: " + str(A))from scipy.linalg import inv as my_inv <-- creating a local name for a function from a package
# 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])# Create baseball, a list of lists
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
# Import numpy
import numpy as np
# Create a 2D numpy array from baseball: np_baseball
np_baseball = np.array(baseball)
# Print out the type of np_baseball
print(type(np_baseball))
# Print out the shape of np_baseball
print(np_baseball.shape)