Summary

  • Tuples are immutable sequence types

    • Literal syntax is optional parentheses around a comma-separated list.

    • Notable syntax for single element tuples utilizing the trailing comma.

    • Tuple unpacking - useful for multiple return values and swapping

  • Strings

    • String concatenation is most efficiently performed with the join() method rather than the addition or augmented assignment operators.

    • The partition() method is a useful and elegant string parsing tool.

    • The format() method provided a powerful means of replacing placeholders with stringified values.

  • Ranges

    • The range objects represent arithmetic progressions.

    • The enumerate() built-in function is often a superior alternative to range() for generating loop counters

  • Lists

    • Lists support indexing from the end of the list with negative indices

    • Slice syntax allows us to copy all, or part, of a list.

    • The full slice is a common Python idiom for copying lists, although the copy() method and list() constructor are less obscure.

    • List (and other collection) copies in Python are shallow copies. References are copied, but the referenced objects are not.

  • Dictionaries map from keys to values

    • Iteration and membership testing with dictionaries is done with respect to the keys.

    • The keys(), values() and items() methods provide views onto the different aspects of a dictionary, allowing convenient iteration.

  • Sets store an unordered collection of unique elements.

    • Sets support powerful set-algebra operations and predicates.

    • The built in collections are can be organized according to which protocols they support, such as iterable, sequence and mapping.

In passing we have also discovered that:

  • Underscore is commonly used for dummy or superfluous variables

  • The pprint module supports pretty printing of compound data structures.

Back in Python 2 days range() was a function which returned a list. The Python 3 version of range is much more efficient, useful and powerful. This, of course, brings to mind the classic joke: The two hardest problems in programming are naming, cache coherence, and off-by-one errors. Arguably, it's poor design to have a module containing functions of the same name, because of this issue.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset