Python – Beyond the Basics
Austin Bingham and Robert Smallshire
ByCourse info
Course info
Description
Python – Beyond the Basics builds directly on the foundations laid in our introductory Python course, Python Fundamentals. Python is a great dynamic language for web development, big data, science, and scripting. In this course we add breadth and depth to your Python skills, exploring the topics you'll need to create robust and readable applications of any size. On completing this course, you'll be familiar with the majority of Python techniques and constructs used in Python programs. Crucially, we'll also advise you on when – and when not – to use the different tools available in Python to best effect, to maximize the quality of your code, your productivity, and the joy inherent in coding in Python.
Section Introduction Transcripts
Strings and Representations
Hello. My name is Austin Bingham, and welcome to the fifth module of Python: Beyond the Basics. In this module we'll look at string representations of objects in Python, and in particular we'll cover the important, but often confused differences between repr() and str(). Understanding and properly using the various string representations in Python is important for writing maintainable, debuggable, and human-friendly programs. In this module we'll show you what you need to know to use them properly. As you already know, Python supports two primary ways of making string representations of objects, the function's repr() and str(). Each of these can take any object as an argument and produce a string representation of some form. These two functions rely on the special methods dunder-repr and dunder-str to generate the strings they produce, and it's possible for class designers to control these string representations by defining those functions. Here's a quick example. Here the class Point2D defines both dunder-str, which returns a simple format, and dunder-repr, which returns a more complete unambiguous format. If we print both string representations, we can see how the free functions str() and repr() use these methods. So, the big question is why are there two representations, and what are they used for?
Implementing Collections
Hello. My name is Robert Smallshire. Welcome to the ninth module of the Python: Beyond the Basics course where we'll demonstrate how to go beyond the built-in collection types by creating your own collections. In our Python Fundamentals course we showed how the different built-in collections can be categorized according to which protocols they support. In this module we'll build a new and fully functioning collection type of our own design. This will require us to implement a series of different protocols, each of which represents a different capability of collections. The protocols we'll cover are the Container Protocol, which allows us to test for item membership in a collection; the Sized Protocol, which allows us to determine the number of items in a collection; the Iterable Protocol, which we encountered in an earlier module of this course; here we'll utilize that knowledge directly; the Sequence Protocol, which supports random read access to collections; and the Set Protocol, which supports various set operations. There are many further protocols for different collections, which we won't cover here, but we'll give you a good grounding in how to implement protocols so you can build on the foundations we provide.