Chevron Left Icon
Advanced Topics in Python
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Let’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’…
  2. 2
    While .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…
  3. 3
    For 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 = { …
  4. 4
    Let’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…
  5. 5
    Here’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…
  6. 6
    Great 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…
  7. 7
    Sometimes 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…
  8. 8
    If 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’, ‘…
  9. 9
    We 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…
  10. 10
    A 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…
  11. 11
    Great work! See? This list slicing business is pretty straightforward. Let’s do one more, just to prove you really know your stuff.
  12. 12
    One 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…
  13. 13
    Lambda 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 …
  14. 14
    All right! Time to test out filter() and lambda expressions. cubes = [x ** 3 for x in range(1, 11)] filter(lambda x: x % 3 == 0, cubes) The example above is just a reminder of the syntax.
  15. 15
    First, let’s review iterating over a dict.
  16. 16
    Good! Now let’s take another look at list comprehensions. squares = [x ** 2 for x in range(5)]
  17. 17
    Great! 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.
  18. 18
    Last 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 …
  1. 1
    Welcome to an intro level explanation of bitwise operations in Python! Bitwise operations might seem a little esoteric and tricky at first, but you’ll get the hang of them pretty quickly. *Bitwis…
  2. 2
    When we count, we usually do it in base 10. That means that each place in a number can hold one of ten values, 0-9. In binary we count in base two, where each place can hold one of two values: 0 or…
  3. 3
    All right! Time to practice counting in binary. To make sure you’ve got the hang of it, fill out the rest of the numbers all the way up to twelve. Please do not use the str() method or any oth…
  4. 4
    Excellent! The biggest hurdle you have to jump over in order to understand bitwise operators is learning how to count in base 2. Hopefully the lesson should be easier for you from here on out. Th…
  5. 5
    Python has an int() function that you’ve seen a bit of already. It can turn non-integer input into an integer, like this: int(“42”) # ==> 42 What you might not know is that the int function actu…
  6. 6
    The next two operations we are going to talk about are the left and right shift bitwise operators. These operators work by shifting the bits of a number over by a designated number of slots. The b…
  7. 7
    The bitwise AND (&) operator compares two numbers on a bit level and returns a number where the bits of that number are turned on if the corresponding bits of both numbers are 1. For example: …
  8. 8
    The bitwise OR (|) operator compares two numbers on a bit level and returns a number where the bits of that number are turned on if either of the corresponding bits of either number are 1. For …
  9. 9
    The XOR (^) or exclusive or operator compares two numbers on a bit level and returns a number where the bits of that number are turned on if either of the corresponding bits of the two number…
  10. 10
    The bitwise NOT operator (~) just flips all of the bits in a single number. What this actually means to the computer is actually very complicated, so we’re not going to get into it. Just know that …
  11. 11
    A bit mask is just a variable that aids you with bitwise operations. A bit mask can help you turn specific bits on, turn others off, or just collect data from an integer about which bits are on o…
  12. 12
    You can also use masks to turn a bit in a number on using |. For example, let’s say I want to make sure the rightmost bit of number a is turned on. I could do this: a = 0b110 # 6 mask = 0b1 # 1 de…
  13. 13
    Using the XOR (^) operator is very useful for flipping bits. Using ^ on a bit with the number one will return a result where that bit is flipped. For example, let’s say I want to flip all of the …
  14. 14
    Finally, you can also use the left shift ( >) operators to slide masks into place. a = 0b101 # Tenth bit mask mask = (0b1 << 9) # One less than ten desired = a ^ mask Let’s say that I want to …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo