Skip to content
1 hidden cell
Introduction to Python
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 here
he uses IPython shell
print(variable)
type(variable)
int()float()str()list()convert to int, etc
y=x+y appends two lists
list_name[3:5]inclusive start exclusive end; 0 indexing; going from end of string last element is -1 indexing
list_name[1][2] for a list of lists you can reference an item inside a sublist
lists y=x different than y=list(x); in the first changing y changes x as well because they point to the same place; in the second changing y does not change x as y is a copy of x and they do not point to the same place
extend a list y=x+["a","b"]; changing an element of y does not change list x
del(x[1]) delete elements of a list
del(areas[-4:-2]) -2 is excluded as it is the end; notice that backwards slice goes still from left to right
Functions
max(), len(), print(), type()
help(function_name) for example help(round) or ?round
methods are functions that belong to objects; objects are floats, strings, lists, etc
in Python everything is an object and has methods associated with it
string methods, list methods for example object.method() list_name.index("mom")=5
Methods are called with the dot notation object.method
packages -> combination of python scripts need to be installed from the terminal,pip3 install numpy then import numpy as np then import package as p or from package import x as y
numpy and math for example; numpy has array which is more useful than list because you can perform calcs, such as multiply the arrays, it will multiply; one array with heights one array with weights, to calculate BMIs you can just do one calculation;
arrays can only contain elements of one type
lists and arrays behave differently
2D arrays
2d_array.shape gives the size of the array say 2x5
2d_array[0][2] or 2d_array[0,2] same thing it returns an element first row and third element in that row
numpy has array and statistics as well
create data using numpy random
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')?