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
Create Lists
As opposed to int, bool, etc., a list is a compound data type; you can group values together: a = "is" b = "nice" my_list = ["my", "list", a, b] After measuring the height of your family, you decide to collect some information on the house you're living in.
Create Lists with different types
A list can contain any Python type. Although it's not really common, a list can also contain a mix of Python types including strings, floats, booleans, etc.
List of lists
As a Data Scientist, you'll often be dealing with a lot of data, and it wil make sense to group some of this data.
Instead of creating a flat list containing strings and floats, representing the names and areas of the rooms in your house you can create a list of lists.
Subset and Conquer
Subsetting Python lists is a piece of cake. Take the code sample below, which creates a list x then selects "b" from it. Remeber that this is the 2nd element, so it has index 1. You can also use negative indexing.
x = ["a", "b", "c", "d"] x[1] x[-3] # same result
Subset and calculate
After you've extracted values from a list, you can use them to perform additional calculations. Take this example, where the 2nd and 4th elements of a list x are extracted. The strings that result are pasted together using the + operator:
x = ["a", "b", "c", "d"] print(x[1] + x[3])
Slicing and Dicing
Selecting single values from a list is just one part of the story. It's also possible to slice your list, which means selecting multiple elements from your list. Use the following syntax:
my_list = [start: end]
The start index will be included, while the end index is not. The code sample below shows an example. A list with "b" and "c", corresponding to indexes 1 and 2, are selected from a list x:
x = ["a", "b", "c", "d"] x[1:3]
The elements with index 1 and 2 are included, while the element with index 3 is not.
#Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]
#Use slicing to create downstairs
downstairs = areas[6:0]
#Use slicing to create upstairs
upstairs = areas[6:10]
#Printout downstairs and upstairs
print(downstairs)
print(upstairs)
Slicing and Dicing (2)
In the video, Hugo first discussed the syntax where you specify both where to begin and end the slice of your list:
my_list[begin:end]
However, its also possible not to specify these indexes. If you don't specify the begin index, Python figures out that you want to start your slice at the beginning of your list. If you don't specify the end index, the slice will go all the way to the last element of your list. To experiment with this, try the following commands in the IPython shell:
x = ["a", "b", "c", "d"] x[:2] x[2:] x[:]
Replace List Elements
Replacing list elements is pretty easy. Simply subset the list and assign new values to the subset. You can select single elements or you can change entire list slices at once.
Use the IPython shell to experiment with the commands below. Can you tell what's happening and why?
x = ["a", "b", "c", "d"] x[1] = "r" x[2:] = ["s", "t"]
Extend a List
If you can change elements in a list, you sure want to be able to add elements to it, right? You can use the + operator:
x = ["a", "b", "c", "d"] y = x + ["e", "f"]