Path: blob/main/resources/week-1/IntroductionToCourse.ipynb
3223 views
Kernel: Python 3
You are currently looking at version 1.1 of this notebook. To download notebooks and datafiles, as well as get help on Jupyter notebooks in the Coursera platform, visit the Jupyter Notebook FAQ course resource.
The Python Programming Language: Functions
In [1]:
Out[1]:
3
In [2]:
Out[2]:
2
`add_numbers` is a function that takes two numbers and adds them together.
In [3]:
Out[3]:
3
'add_numbers' updated to take an optional 3rd parameter. Using `print` allows printing of multiple expressions within a single cell.
In [4]:
Out[4]:
3
6
`add_numbers` updated to take an optional flag parameter.
In [5]:
Out[5]:
Flag is true!
3
Assign function `add_numbers` to variable `a`.
In [6]:
Out[6]:
3
The Python Programming Language: Types and Sequences
Use `type` to return the object's type.
In [7]:
Out[7]:
str
In [8]:
Out[8]:
NoneType
In [9]:
Out[9]:
int
In [10]:
Out[10]:
float
In [11]:
Out[11]:
function
Tuples are an immutable data structure (cannot be altered).
In [12]:
Out[12]:
tuple
Lists are a mutable data structure.
In [13]:
Out[13]:
list
Use `append` to append an object to a list.
In [14]:
Out[14]:
[1, 'a', 2, 'b', 3.3]
This is an example of how to loop through each item in the list.
In [15]:
Out[15]:
1
a
2
b
3.3
Or using the indexing operator:
In [16]:
Out[16]:
1
a
2
b
3.3
Use `+` to concatenate lists.
In [17]:
Out[17]:
[1, 2, 3, 4]
Use `*` to repeat lists.
In [18]:
Out[18]:
[1, 1, 1]
Use the `in` operator to check if something is inside a list.
In [19]:
Out[19]:
True
Now let's look at strings. Use bracket notation to slice a string.
In [20]:
Out[20]:
T
T
Th
This will return the last element of the string.
In [21]:
Out[21]:
'g'
This will return the slice starting from the 4th element from the end and stopping before the 2nd element from the end.
In [22]:
Out[22]:
'ri'
This is a slice from the beginning of the string and stopping before the 3rd element.
In [23]:
Out[23]:
'Thi'
And this is a slice starting from the 4th element of the string and going all the way to the end.
In [24]:
Out[24]:
's is a string'
In [25]:
Out[25]:
Christopher Brooks
ChristopherChristopherChristopher
True
`split` returns a list of all the words in a string, or a list split on a specific character.
In [26]:
Out[26]:
Christopher
Brooks
Make sure you convert objects to strings before concatenating.
In [27]:
Out[27]:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-9d01956b24db> in <module>
----> 1 'Chris' + 2
TypeError: can only concatenate str (not "int") to str
In [ ]:
Dictionaries associate keys with values.
In [ ]:
In [ ]:
Iterate over all of the keys:
In [ ]:
Iterate over all of the values:
In [ ]:
Iterate over all of the items in the list:
In [ ]:
You can unpack a sequence into different variables:
In [ ]:
In [ ]:
In [ ]:
Make sure the number of values you are unpacking matches the number of variables being assigned.
In [ ]:
The Python Programming Language: More on Strings
In [ ]:
In [ ]:
Python has a built in method for convenient string formatting.
In [ ]:
Reading and Writing CSV files
Let's import our datafile mpg.csv, which contains fuel economy data for 234 cars.
mpg : miles per gallon
class : car classification
cty : city mpg
cyl : # of cylinders
displ : engine displacement in liters
drv : f = front-wheel drive, r = rear wheel drive, 4 = 4wd
fl : fuel (e = ethanol E85, d = diesel, r = regular, p = premium, c = CNG)
hwy : highway mpg
manufacturer : automobile manufacturer
model : model of car
trans : type of transmission
year : model year
In [ ]:
`csv.Dictreader` has read in each row of our csv file as a dictionary. `len` shows that our list is comprised of 234 dictionaries.
In [ ]:
`keys` gives us the column names of our csv.
In [ ]:
This is how to find the average cty fuel economy across all cars. All values in the dictionaries are strings, so we need to convert to float.
In [ ]:
Similarly this is how to find the average hwy fuel economy across all cars.
In [ ]:
Use `set` to return the unique values for the number of cylinders the cars in our dataset have.
In [ ]:
Here's a more complex example where we are grouping the cars by number of cylinder, and finding the average cty mpg for each group.
In [ ]:
Use `set` to return the unique values for the class types in our dataset.
In [ ]:
And here's an example of how to find the average hwy mpg for each class of vehicle in our dataset.
In [ ]:
The Python Programming Language: Dates and Times
In [ ]:
`time` returns the current time in seconds since the Epoch. (January 1st, 1970)
In [ ]:
Convert the timestamp to datetime.
In [ ]:
Handy datetime attributes:
In [ ]:
`timedelta` is a duration expressing the difference between two dates.
In [ ]:
`date.today` returns the current local date.
In [ ]:
In [ ]:
In [ ]:
The Python Programming Language: Objects and map()
An example of a class in python:
In [ ]:
In [ ]:
Here's an example of mapping the `min` function between two lists.
In [ ]:
Now let's iterate through the map object to see the values.
In [ ]:
The Python Programming Language: Lambda and List Comprehensions
Here's an example of lambda that takes in three parameters and adds the first two.
In [ ]:
In [ ]:
Let's iterate from 0 to 999 and return the even numbers.
In [ ]:
Now the same thing but with list comprehension.
In [ ]: