Udacity CS 212: Starting out

I’m already halfway through CS 212 (today is day 2 of starting), and have learned so much more! Below I will document some new knowledge of Python I have gained.

First we start of with a new data type, tuple.

Tuples are constructed using parentheses, and contains items separated by commas, similar to lists. For example (1, 2, 3) is a tuple.
Tuples are also non-mutable, which means that like strings, once a tuple is created, you will not be able to modify the entries in the tuple.
A tuple and a list is similar in that they both below to a type of data called Sequence Type, and hence many methods which are available for lists can also be used on tuples:

x in tuple
x not in tuple
tuple[0] # first item in tuple
len(tuple) # number of items in the tuple

Additionally, I learned new methods which are available to both list and tuple types:

min(tuple) # returns the smallest item in tuple
max(tuple) # returns the largest item in tuple
tuple.index(i) # returns the index of the first occurrence of i in tuple
tuple.count(i) # returns the number of occurrence of i in tuple

Next, I learned another new data type, set, which is an unordered collection of distinct objects.

Sets are mutable, items in sets can be removed, and new items can be added. Because sets are unordered, it cannot be indexed: position of elements are not recorded, only existence is. In other words:

len(set) # will give you the number of items in the set
x in set # will tell you if x is in set

However, set[0] will not work.
The most common use of sets are for membership testing. Suppose I have a list of users who read my blog in the last hour:

users = ['Jack', 'John', 'Joanne']
# as users continue to visit my blog, I append their names to the list as long as the visit was within the same hour, if Jack visits again within the same hour...
users = ['Jack', 'John', 'Joanne', 'Jack']
# what if I want to find the number of unique visitors? Do I iterate through every item to search for a repeat? No...
unique_users = set(users) # this will return set(['Jack', 'John', 'Joanne'])

Next, I learned about assert statements

Assert statements are really useful for debugging:

assert big(2,3) == 3

Let’s say you have a procedure named big that takes in two parameters and returns the number which is bigger. When the above statement runs, what it basically does is it evaluates the expression big(2,3) and checks if the result is the same as 3, which is your expected result. If it is, the assert statement is successful and your program runs normally. If the assertion fails, your program will throw an error and stop at the assert statement.

Lastly, I learned about a new way to iterate through lists:

hand = ['3D', '4C, '6H'] # a hand with 3diamons, 4clubs, 6hearts
suits = [s for r,s in hand] # iterates through hand, the breaks each element into r,s pair, where the first letter is r and second is s, then extracts only the s, and append them to the list called suits
print suits # will give me ['D', 'C', 'H']

This Udacity post references Udacity CS 212 Unit 1 Chapters 1 to 21.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s