I recently did most of Dr Chuck’s Python course on Coursera, and documented most of what I’d learnt. Here are my notes, and few comments on the experience.
I’d recommend it for an introduction. It’s focused on text processing, but includes all the conceptual elements I expected. Coupled with Swaroop’s course: A bite of Python and Learn Python the Hard Way, it represents pretty much the entirity of my Python courses to date.
Week 1: introduction - why we program
I can’t say I really learnt anything in the first week, but it was definitely fun. Dr Chuck is a charismatic and enthusiastic teacher and his lectures, that are kind of like fireside chats, certainly got me interested in the course.
Week 2: variables and expressions
Still pretty basic, but I definitely learned some stuff this week.
Expression evaluation order
When Python evaluates expressions the following hierarchy of operators is used: Parentheses; Powers; Division, multiplication, remainder; Addition, subtraction; Left to right.
Floats in expressions
When evaluating an expression that contains a float the evaluation is done with integers until the float is evaluated, then after that the rest of the expression is evaluated as if all numbers were floats
An improvement on the elevator program
My version of Dr Chuck’s Euro > US elevator floor converter. Mine accepts 0 and negative floors
Week 3: conditional code
Indenting and blocks
Don’t mix tabs and spaces when you indent your code, infact, don’t use tabs at all, indent your code blocks by 4 spaces. To set this is Sublime Text, go to Preferences > Settings - User, and add:
The if statement
You can do single line if statements like this:
You don’t need to have an else in a conditional, you can just have if and elif(s):
I was curious about why Dr Chuck didn’t use parentheses when writing out if conditionals, turns out for conditional statements they do the same thing as for evaluating any simple expression, i.e. to force hierachy of evaluation! So, they are really not necessary for simple conditions like x < 5.
I think I will still use them though, for consistency if nothing else.
Try/except is used to deal with errors, by default it will only deal with run time errors, e.g. non-existent variables. An if statement will never raise an exception on it’s own. You have to use ‘raise’ to get that to happen.
It may be possible to use ‘assert’, but that has its own issues apparently.
Week 4: functions
I’m sad to say I didn’t really learn anything new about functions from Dr Chuck. To be fair, he did say functions wouldn’t really feature in the course. I would suggest looking elsewhere for function related tutorials.
Format for a function:
Week 5: loops and iteration
A loop that never runs is known as a zero trip loop, for example:
Use continue in a loop to return to the start of the loop, for example:
While type loops are known as indefinite loops, they will contune to run until some condition becomes false, the opposite are definite loops, i.e. iteration
Loop idioms was a phrase I’d not come across before, Dr Chuck gave examples including counting, adding and averaging loops. More on idioms on wikipedia.
Week 6: Strings
Best thing I learnt this week is the dir function. Use it to discover built in methods for an object, e.g. a variable that points to a string object:
Access a character from a string using indexing:
Get length of string:
Looping through strings:
‘in’ as an operator:
A simple string parsing exercise:
Week 7: You Have Arrived
Didn’t make any notes this week.
Week 8: Lists
Lists are mutable! Use the range function to create a list of integers:
Not sure when you would need it really, but if you want to use the index of an item in a list when looping through it you can combine range() and len() to do that:
Lists can be concatinated (+) and sliced ([n:n]) just like strings. Interesting that concatination builds a new list from the existing lists, I thought it might have created a new list of nested lists, but no!
An aside, two logiccal operators to remember: in and not in:
Interesting discussion on alternatives to loop idioms discussed in week 5:
The idea of adding values to a list via a loop and then later doing other things with that list, i.e. with those values, is a powerful one!
The split() method splits a string into a list:
Split() method can be really useful for extracting specific elements form large strings, e.g text files. Use the “double split” method, split on white space, then refine by taking important items form the list and spliting those with other chracters.
A technique form preventing errors in your programs, for example, if it appears specific data being reed might be causing problems. Basically debug to figure out what data is causing the issue and then write a statement to deal with that data before the bit of your program that crashes is reached.
Typically this would be an “if x: continue” statement.
Week 9: Dictionaries
Didn’t make any notes this week.
Week 10: Tuples
Basically immutable lists. They are iterable like lists, but you can’t change them once created so you can’t sort, append etc. So why use them? Because they are more efficient than lists. Useful for assigning multiple variables in one statement:
Useful for iterating over keys and values in dictionaries:
Tuples are comparable:
Because they are comparable they are sortable, so are useful for sorting dictionaries by key:
You can combine all the above and use the
sorted() method to output sorted keys and values:
Dr Chuck talked a little bit about refactoring, suggesting once you understand the long form of writing many of these idioms, you can often refactor them into ‘more dense’ versions. An example of this being if oyu wanted to get the top 5 most common words in some text data: