美文网首页
Python Notes (6) - Advanced Topi

Python Notes (6) - Advanced Topi

作者: SilentSummer | 来源:发表于2018-03-19 21:46 被阅读12次

    The note will introduce more advanced points about what we have learned in last several notes. It includes: iteration nation, list comprehensions, list slicing, lambdas, and introduction to bitwise operators.

    Python notes of open courses @Codecademy.

    Iteration Nation

    • dictionary.items(): iterators for dictionaries, which returns an array of tuples (key/value pairs) in a random order
    • keys(): returns an array of the dictionary's keys, in a random order
    • values(): returns an array of the dictionary's values, in a random order

    List Comprehensions

    • List comprehensions are a powerful way to generate lists using the for/in and if keywords we've learned.
    • E.g. 1, evens_to_50 = [i for i in range(51) if i % 2 == 0]
    • E.g. 2, doubles_by_3 = [x*2 for x in range(1,6) if (x*2)%3 == 0]

    List Slicing

    • List slicing allows us to access elements of a list in a concise manner.
    • Syntax: [start:end:stride]
      • start describes where the slice starts (inclusive)
      • end is where it ends (exclusive)
      • stride describes the space between items in the sliced list.
    • Omitting Indices
      • The default starting index is 0.
      • The default ending index is the end of the list.
      • The default stride is 1.
    • Reversing a List
      • [::-1]
      • A negative stride progresses through the list from right to left.

    Lambdas

    • Anonymous Functions
      • lambda x: x % 3 == 0 is the same as def by_three(x): return x % 3 == 0
      • Don't need to actually give the function a name.
      • It does its work and returns a value without one.
    • Lambda Syntax
      • lambda variable: variable expression
    • Filter
      • filter(lambda x: x % 3 == 0, list)
      • filter uses the lambda to determine what to filter, and the second argument list is the list it does the filtering on.

    Introduction to Bitwise Operators

    Bitwise operations are operations that directly manipulate bits.

    Binary Representation

    • The Base 2 Number System
      • In binary we count in base two, where each place can hold one of two values: 0 or 1.
      • Each decimal place in a binary number represents a power of two (or a bit).
      • Writing numbers in binary format by starting the number with 0b.
        • bin() takes an integer as input and returns the binary representation of that integer in a string.
          • After using the bin function, you can no longer operate on the value like a number.
          • You can also represent numbers in base 8 and base 16 using the oct() and hex() functions.
        • int("110", base): When given a string containing a number and the base that number is in, the function will return the value of that number converted to base ten.

    Introduction to Bitwise Operators

    • Bitwise operations

      • Right Shift: print 5 >> 4 # 0
      • Left Shift: print 5 << 1 # 10
      • Bitwise AND: print 8 & 5 # 0
      • Bitwise OR: print 9 | 4 # 13
      • Bitwise XOR: print 12 ^ 42 # 38
      • Bitwise NOT: print ~88 # -89
    • Right/Left Shift (>>/<<)

      • These operators work by shifting the bits of a number over by a designated number of slots.

      • Note that you can only do bitwise operations on an integer.

      • The block below shows how these operators work on the bit level. Note that in the diagram, the shift is always a positive integer:

        # Left Bit Shift (<<)  
        0b000001 << 2 == 0b000100 (1 << 2 = 4)
        0b000101 << 3 == 0b101000 (5 << 3 = 40)       
        
        # Right Bit Shift (>>)
        0b0010100 >> 3 == 0b000010 (20 >> 3 = 2)
        0b0000010 >> 2 == 0b000000 (2 >> 2 = 0) 
        
    • Bitwise AND (&)

      • It 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.
    • Bitwise OR (|)

      • It 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.
    • Bitwise XOR/Exclusive Or Operator (^)

      • It 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 numbers are 1, but not both.
    • Bitwise NOT (~)

      • It just flips all of the bits in a single number.

    Additional Examples

    • 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 or off.

          num  = 0b1100
          mask = 0b0100
          desired = num & mask
          if desired > 0:
              print "Bit was on"
      
    • Using | to turn a bit in a number on

    • 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, if you want to flip all of the bits in a, you might do this:
      a = 0b110 # 6
      mask = 0b111 # 7
      desired =  a ^ mask # 0b1
      
    • Slip and Slide

      • Using the left shift (<<) and right shift (>>) operators to slide masks into place.
      • If we want to obtain a tenth bit mask, we may use mask = (0b1 << 9) # One less than ten.

    External Resources

    相关文章

      网友评论

          本文标题:Python Notes (6) - Advanced Topi

          本文链接:https://www.haomeiwen.com/subject/qxlafftx.html