Advanced Topics in Python
Learn some of the more complex aspects of Python, including data structures, list comprehensions, list slicing, and lambda expressions.
StartAdvanced Topics in Python
Lesson 1 of 2
- 1Let’s start with iterating over a dictionary. Recall that a dictionary is just a collection of keys and values. d = { “Name”: “Guido”, “Age”: 56, “BDFL”: True } print d.items() # => [(‘BDFL’…
- 2While .items() returns an array of tuples with each tuple consisting of a key/value pair from the dictionary: * The .keys() method returns a list of the dictionary’s keys, and * The .values() me…
- 3For iterating over lists, tuples, dictionaries, and strings, Python also includes a special keyword: in. You can use in very intuitively, like so: for number in range(5): print number, d = { …
- 4Let’s say you wanted to build a list of the numbers from 0 to 50 (inclusive). We could do this pretty easily: my_list = range(51) But what if we wanted to generate a list according to some logic…
- 5Here’s a simple example of list comprehension syntax: new_list = [x for x in range(1, 6)] # => [1, 2, 3, 4, 5] This will create a new_list populated by the numbers one to five. If you want those…
- 6Great work! Now it’s time for you to create a list comprehension all on your own. c = [‘C’ for x in range(5) if x < 3] print c The example above creates and prints out a list containing [‘C’, ‘C…
- 7Sometimes we only want part of a Python list. Maybe we only want the first few elements; maybe we only want the last few. Maybe we want every other element! List slicing allows us to access elemen…
- 8If you don’t pass a particular index to the list slice, Python will pick a default. to_five = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’] print to_five[3:] # prints [‘D’, ‘E’] print to_five[:2] # prints [‘A’, ‘…
- 9We have seen that a positive stride progresses through the list from left to right. A negative stride progresses through the list from right to left. letters = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’] print l…
- 10A positive stride length traverses the list from left to right, and a negative one traverses the list from right to left. Further, a stride length of 1 traverses the list “by ones,” a stride lengt…
- 11Great work! See? This list slicing business is pretty straightforward. Let’s do one more, just to prove you really know your stuff.
- 12One of the more powerful aspects of Python is that it allows for a style of programming called functional programming, which means that you’re allowed to pass functions around just as if they w…
- 13Lambda functions are defined using the following syntax: my_list = range(16) filter(lambda x: x % 3 == 0, my_list) Lambdas are useful when you need a quick function to do some work for you. If …
- 15First, let’s review iterating over a dict.
- 16Good! Now let’s take another look at list comprehensions. squares = [x ** 2 for x in range(5)]
- 17Great! Next up: list slicing. str = “ABCDEFGHIJ” start, end, stride = 1, 6, 2 str[start:end:stride] You can think of a Python string as a list of characters.
- 18Last but not least, let’s look over some lambdas. my_list = range(16) filter(lambda x: x % 3 == 0, my_list) We’ve given you another (slightly different) garbled. Sort it out with a filter() and …
What you'll create
Portfolio projects that showcase your new skills