Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Course Conclusion
Lecture: Lightning review

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So let's just take a few minutes and review what we have studied and what we've learned.
0:03 We started out looking at PEP 8
0:06 and many people when they first start looking at Pythonic code
0:09 and thinking about these idioms and these concepts,
0:11 they might feel that, well, PEP 8 is what is Pythonic code, it defines how we write Pythonic code.
0:18 Yes it does touch on some of the things you should do in Python,
0:20 it does talk about how you should write your code
0:22 and even some of the underlying patterns that you use,
0:26 but there is so much more, as you now know.
0:28 We talked about many foundational concepts
0:31 and in some senses it's just kind of a grouping
0:33 of a really important core concepts that didn't fit into their own category.
0:38 So we talked about thruthiness, booleans,
0:40 leveraging the truthiness of objects, testing for None, remember, use "is", not "== None",
0:46 we talked about multiple tests against the single value,
0:49 remember we saw that Pythonic way to do this is value "in" collection
0:54 or set of options, using "random.choice" to find a random item out of a sequence;
0:59 we talked about string formatting,
1:02 we saw that when you write scripts that are meant to be run as programs
1:05 and chained together Unix style, you should really add exit codes for the error conditions,
1:10 and we also saw flat is better than nested, in practice.
1:15 We next turned our attention to dictionaries and you saw a dictionary
1:18 is really a core part of the Python language.
1:21 You saw there were tremendous performance benefits
1:23 for using dictionaries for random lookup by values
1:26 other than the index compared to other collection types such as list.
1:30 We saw that using __slots__ in certain limited situations can dramatically improve
1:35 Python's performance around memory.
1:38 Python 3.5 introduced a new way to merge dictionaries,
1:42 we saw that dictionaries can be a stand-in for switch statements,
1:45 and we saw that dictionaries are isomorphic to JSON.
1:48 Then we turned our attention to generators and collections,
1:51 generators as in methods using the yied keyword
1:53 as well as generator expressions using the parentheses
1:57 instead of the square brackets like a list comprehension.
1:59 We talked about using "in" to test for whether an item is in a collection or not,
2:03 we saw slicing is a core Pythonic concept
2:06 and can even be applied to outside of basic sequences like lists and so on,
2:11 to things like databases.
2:13 For methods we saw that "yield" and "yield return" let us write simple
2:16 and highly efficient generator methods;
2:19 and finally, we saw that if we have a generator,
2:22 regardless of where it comes from, whether it's a generator method
2:24 or a generator expression, we saw a cool trick for summing up
2:28 or counting the number of items in a generator.
2:31 One of the core building blocks for Python of course is functions.
2:35 And we saw the functions are first class citizens
2:37 we saw that lambda expressions let us write small concise functions
2:41 to be passed around as arguments,
2:43 we talked about how using return values as errors from functions,
2:47 while sometimes OK is generally, as a rule of thumb, frowned upon,
2:51 and we should use errors and exception handling
2:54 rather than return types to indicate failure,
2:57 we talked about different ways to add polymorphism or overloading to functions,
3:01 we talked about default values, variable number of arguments,
3:04 keyword arguments, mapping dictionaries to and from keyword arguments
3:09 all those sorts of things, and finally we saw one of the Python gotchas
3:12 of which there is not many, but using a mutable instance
3:16 or a mutable type for a default value.
3:19 Next, we came to import antigravity,
3:22 talked about how amazing packages are and what the idioms
3:25 around using packages and your own modules are in Python.
3:29 So remember, try to avoid wildcard imports,
3:32 "from package import *", not super-Pythonic.
3:35 If you are building a module that is meant to be reused
3:38 but also sometimes run as a program,
3:40 you saw the "main", the __main__ convention in action.
3:44 We also briefly touched on virtual environments,
3:46 using requirements.txt to help the consumer of your application
3:51 know what they need to install.
3:54 Moving from functional programming into object-oriented programming
3:57 we saw that classes and object oriented programming in Python are very important,
4:01 and a key cornerstone of the language,
4:03 we saw the right and the wrong way to add fields to the classes,
4:05 we talked about data encapsulation and data hiding
4:08 with underscore field name being protected
4:10 and double underscore field name actually getting mangled by the Python runtime
4:14 so that it's effectively private, although you saw that it's not really super hidden,
4:18 you can still get to it.
4:19 And we saw that properties, both read-only and read/write
4:22 are very powerful ways to encapsulate and protect our data
4:26 and let the class manage it more itself.
4:30 Loops in Python are interesting because they are fairly different
4:33 than most other languages, we saw there is no numerical "for" loop,
4:37 there is only a sequence based "for...in" loop, but there are things to add into there,
4:41 sequences we can use that will give us variants that are basically numerical "for" loops.
4:47 So remember we can "for...in" over a range and that will give us numbers in sequence,
4:50 if we have a sequence and we want to "for...in" over that and get the index,
4:54 we enumerate over that and project or unpack that
4:58 into the tuple "index, value" and then we work with that.
5:01 We also saw that loops have an "else" block but we should forget that fact.
5:05 Tuples are powerful lightweight containers often used in Python.
5:09 And, we saw that we can unpack those into variables,
5:12 so let's do something like "x,y =" tuple of length 2,
5:16 put the first item in "x" and the second item in "y" and so on,
5:19 and we can use that for all sorts of cool tricks,
5:21 we'll use that when we do a "for...in" loop over enumerate,
5:24 we used that if we wanted simulate returning multiple values from a method, remember,
5:28 technically that's not what happens, but that's how it appears in code, which is wonderful;
5:33 we finally saw that while tuples are great, working with them
5:36 can be error-prone and kind of unclear, so upgrading them to named tuples
5:40 via "collection.namedtuple" is a really nice way to work with them.
5:44 Finally, we took a look around the ecosystem for Python
5:47 and said "there are so many amazing packages outside of Python",
5:51 let's just round out the course by making a point
5:54 that you should first look to things like PyPi,
5:57 the Python package index and GitHub
5:59 for the various building blocks or lego pieces of your application
6:03 rather than trying to write them all from scratch.