Introduction to Python
Run the hidden code cell below to import the data used in this course.
1 hidden cell
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 hereExplore 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')?
float = number w/ decimals (floating point) int = integer str = string bool = t/f
Python Lists Use square brackets ex -> fam = [1.73, 1.68, 1.71, 1.89]
List names a collection of values fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89] fam2 = [["Liz", 1.73],etc]
Function/behavior of a list
The first in a list is 0 (0 indexing), the last in a list is -1 3:5 = only 4th and 5th, the 6th (5+1) is not included
Manipulating Lists change/add/remove from lists
"fam[7] = 1.86"
fam[0:2] - changes first and second list items BECAUSE index 2 is NOT included
del(fam[2]) removes element at index 2
name of list = reference to the list
copy a list by y = list(x)
y = x[:]
otherwise Y ACTUALLY equals X
Functions Piece of reusable code, solves particular task type returns variable type max returns highest number in a list
round(#, # of decimal places)
How to find functions? Learn w/ time standard tasks probably exist -- Google it!
methods: functions that belong to objects
EVERYTHING IS AN OBJECT each object has methods associated, depending on type
Append adds to end of list
Packages in Python Directory of python scripts NumPy Matplotlib scikit-lib --> http://pip.readthedocs.org/en/stable/installing get-pip.py python3 get-pip.py pip3 install numpy
Python can't do calculations on lists; but it can on arrays with NumPy
NumPy arrays only contain one type; if you try to mix types, it will convert them to one single type
Attribute - no round brackets
2D arrays ; rows and colums. First index = row, second index = column Just a : selects whole row/column
np.mean = mean function from the mean
corrcoef = correlated? std = standard deviation