-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mikeckennedy/write-python…
- Loading branch information
Showing
63 changed files
with
4,197 additions
and
4,189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,84 @@ | ||
0:01 Hello and welcome to the course Write Pythonic Code Like a Seasoned Developer. | ||
0:04 My name is Michael Kennedy and we are going to be on this journey together | ||
0:07 to help you write more readable, more efficient and more natural Python code. | ||
0:13 So what is Pythonic code anyway? | ||
0:01 Hello and welcome to the course Write Pythonic Code Like a Seasoned Developer. | ||
0:04 My name is Michael Kennedy and we are going to be on this journey together | ||
0:07 to help you write more readable, more efficient and more natural Python code. | ||
0:13 So what is Pythonic code anyway? | ||
0:16 When developers are new to Python, they often hear this phrase Pythonic | ||
0:20 and they ask what exactly does that mean? | ||
0:23 In any language, there is a way of doing things naturally | ||
0:26 and a way that kind of fight the conventions to the language. | ||
0:30 When you work naturally with the language features and the runtime features, | ||
0:33 this is called idiomatic code, | ||
0:35 and in Python when you write idiomatic Python we call this Pythonic code. | ||
0:40 One of the challenges in Python is it's super easy to get started | ||
0:43 and kind of learn the basics and start writing programs | ||
0:46 before you really master the language. | ||
0:48 And what that often means is people come in from other languages | ||
0:51 like C++ or Java or something like that, | ||
0:53 they will take algorithms or code that they have and bring it over to Python | ||
0:58 and they will just tweak the syntax until it executes in Python, | ||
1:00 but this often uses the language features of let's just focus on Java. | ||
1:04 In Python there is often a more concise, more natural way of doing things, | ||
1:10 and when you look at code that came over from Java, | ||
0:20 and they ask what exactly does that mean? | ||
0:23 In any language, there is a way of doing things naturally | ||
0:26 and a way that kind of fight the conventions of the language. | ||
0:30 When you work naturally with the language features and the runtime features, | ||
0:33 this is called idiomatic code, | ||
0:35 and in Python when you write idiomatic Python we call this Pythonic code. | ||
0:40 One of the challenges in Python is it's super easy to get started | ||
0:43 and kind of learn the basics and start writing programs | ||
0:46 before you really master the language. | ||
0:48 And what that often means is people come in from other languages | ||
0:51 like C++ or Java or something like that, | ||
0:53 they will take algorithms or code that they have and bring it over to Python | ||
0:57 and they will just tweak the syntax until it executes in Python, | ||
1:00 but this often uses the language features of - let's just focus on Java. | ||
1:04 In Python there is often a more concise, more natural way of doing things, | ||
1:09 and when you look at code that came over from Java, | ||
1:11 we'll just take a really simple example- | ||
1:13 if you have a class and the class has a get value and set value, | ||
1:13 if you have a class and the class has a get value and set value, | ||
1:17 because in Java that is typically the way you do encapsulation, | ||
1:20 people bring those classes in this code over and migrate it to Python, | ||
1:24 they might still have this weird getter setter type of code. | ||
1:28 And that would look completely bizzare in Python | ||
1:31 because we have properties for example. | ||
1:33 So we are going to look at this idea of Pythonic code, | ||
1:36 now it's pretty easy to understand but it turns out to be fairly hard to make concrete, | ||
1:40 you'll see a lot of blog posts and things of people trying to put structure | ||
1:44 or examples behind this concept of Pythonic code. | ||
1:20 and people bring those classes in this code over and migrate it to Python, | ||
1:24 they might still have this weird getter/setter type of code. | ||
1:28 And that would look completely bizzare in Python | ||
1:31 because we have properties for example. | ||
1:33 So we are going to look at this idea of Pythonic code, | ||
1:36 now, it's pretty easy to understand but it turns out to be fairly hard to make concrete, | ||
1:40 you'll see a lot of blog posts and things of people trying to put structure | ||
1:44 or examples behind this concept of Pythonic code. | ||
1:48 We are going to go through over 50 examples of things I consider Pythonic | ||
1:53 and by the end you'll have many examples, | ||
1:56 patterns and so on to help you have a solid grip on what Pythonic code is. | ||
2:01 So what is Pythonic code and why does it matter? | ||
1:53 and by the end you'll have many examples, | ||
1:56 patterns and so on to help you have a solid grip on what Pythonic code is. | ||
2:02 So what is Pythonic code and why does it matter? | ||
2:05 Well, when you write Pythonic code, | ||
2:07 you are leveraging the experience of 25 years of many thousands, | ||
2:10 maybe millions of developers, | ||
2:12 these guys and girls have worked in this language day in and day out | ||
2:16 for the last 25 years and they really perfected the way | ||
2:19 of working with classes, functions, loops, and so on, | ||
2:23 and when you are new especially, | ||
2:25 it's very helpful to just study what those folks have done and mimic that. | ||
2:29 When you write Pythonic code, you are writing code | ||
2:32 that is specifically tuned to the CPython runtime. | ||
2:35 The CPython interpreter and the Python language have evolved together, | ||
2:40 they have grown up together, | ||
2:42 so the idioms of the Python language are of course matched | ||
2:44 or paired well with the underlying runtime, | ||
2:47 so writing Pythonic code is an easy way | ||
2:07 you are leveraging the experience of 25 years of many thousands, | ||
2:10 maybe millions of developers, | ||
2:12 these guys and girls have worked in this language day in and day out | ||
2:16 for the last 25 years and they really perfected the way | ||
2:19 of working with classes, functions, loops, and so on, | ||
2:23 and when you are new especially, | ||
2:25 it's very helpful to just study what those folks have done and mimic that. | ||
2:29 When you write Pythonic code, you are writing code | ||
2:32 that is specifically tuned to the CPython runtime. | ||
2:35 The CPython interpreter and the Python language have evolved together, | ||
2:40 they have grown up together, | ||
2:42 so the idioms of the Python language are of course matched | ||
2:44 or paired well with the underlying runtime, | ||
2:47 so writing Pythonic code is an easy way | ||
2:50 to write code that the interpreter expects to run. | ||
2:54 When you write Pythonic code, | ||
2:54 When you write Pythonic code, | ||
2:56 you are writing code that is easily read and understood by Python developers. | ||
2:59 A Python developer can look at standard idiomatic Python | ||
3:03 and just glance at sections and go, | ||
3:05 "oh, I see what they are doing here, I see what they are doing there, bam, bam, bam" | ||
3:09 and quickly understand it. | ||
3:11 If instead it's some algorithm that is taken from another language with other idioms, | ||
3:15 the experienced developer has to read through and try to understand | ||
3:20 what is happening at a much lower level, | ||
3:23 and so your code is more readable to experienced developers | ||
3:27 and even if you are new will become probably more readable to you | ||
3:03 and just glance at sections and go, | ||
3:05 "oh, I see what they are doing here, I see what they are doing there, bam, bam, bam" | ||
3:09 and quickly understand it. | ||
3:10 If instead it's some algorithm that is taken from another language with other idioms, | ||
3:15 the experienced developer has to read through and try to understand | ||
3:20 what is happening at a much lower level, | ||
3:23 and so your code is more readable to experienced developers | ||
3:27 and even if you are new will become probably more readable to you | ||
3:30 if you write Pythonic code. | ||
3:33 One of the super powers of Python is that it is a very readable | ||
3:36 and simple language without giving up the expressiveness of the language. | ||
3:40 People coming from other languages that are less simple, | ||
3:43 less clean and easy to work with | ||
3:46 will bring those programming practices or those idioms over | ||
3:49 and they will write code that is not as simple as it could be in Python | ||
3:32 One of the super powers of Python is that it is a very readable | ||
3:36 and simple language without giving up the expressiveness of the language. | ||
3:40 People coming from other languages that are less simple, | ||
3:43 less clean and easy to work with | ||
3:46 will bring those programming practices or those idioms over | ||
3:49 and they will write code that is not as simple as it could be in Python | ||
3:53 even though maybe it was a simple as it could be in C. | ||
3:56 So when you write Pythonic code, | ||
3:58 you are often writing code that is simpler and cleaner | ||
4:01 than otherwise would be the case. | ||
4:03 If you are working on an open source project, | ||
4:05 it will be easier for other contributors to join in because like I said, | ||
4:08 it's easier for them to read and understand the code at a glance, | ||
4:11 and they will more naturally know what you would expect them to write. | ||
4:14 If you are working on a software team, | ||
4:17 it's easier to onboard new Python developers into your team | ||
4:20 if you are writing idiomatic code, | ||
4:22 because if they already know Python | ||
4:24 it's much easier for them to | ||
4:26 your large project. | ||
3:57 you are often writing code that is simpler and cleaner | ||
4:00 than otherwise would be the case. | ||
4:02 If you are working on an open source project, | ||
4:04 it will be easier for other contributors to join in because like I said, | ||
4:08 it's easier for them to read and understand the code at a glance, | ||
4:11 and they will more naturally know what you would expect them to write. | ||
4:14 If you are working on a software team, | ||
4:16 it's easier to onboard new Python developers into your team | ||
4:20 if you are writing idiomatic code, | ||
4:22 because if they already know Python | ||
4:23 it's much easier for them to grok | ||
4:26 your large project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,76 @@ | ||
0:01 What areas are we going to cover in this class? | ||
0:03 Well, we are going to start with the foundations and this concept called pep 8. | ||
0:06 So, pep 8 is a standardized document | ||
0:09 that talks about the way code should be formatted, | ||
0:11 and even some of the Pythonic ideas and Pythonic code examples. | ||
0:15 However, we are going to go way beyond pep 8 in this course, | ||
0:19 and so we'll probably spend 15 minutes talking about pep 8 | ||
0:22 and then we'll move onto other foundational items. | ||
0:25 Then we are going to focus on dictionaries, | ||
0:28 dictionaries play a super important role in Python, | ||
0:01 What areas are we going to cover in this class? | ||
0:03 Well, we are going to start with the foundations and this concept called PEP 8. | ||
0:06 So, PEP 8 is a standardized document | ||
0:09 that talks about the way code should be formatted, | ||
0:11 and even some of the Pythonic ideas and Pythonic code examples. | ||
0:16 However, we are going to go way beyond PEP 8 in this course, | ||
0:19 and so we'll probably spend 15 minutes talking about PEP 8 | ||
0:22 and then we'll move onto other foundational items. | ||
0:25 Then we are going to focus on dictionaries, | ||
0:28 dictionaries play a super important role in Python, | ||
0:30 they are basically the backing store for classes, | ||
0:34 they are used for data exchange all over the place, | ||
0:37 and there are a lot of interesting use cases and ways | ||
0:40 in which dictionaries are used in a language. | ||
0:43 We are going to talk about a lot of interesting aspects | ||
0:45 and optimal ways to use and leverage dictionaries. | ||
0:48 Next up are working with collections, | ||
0:51 things called list comprehensions and generator expressions. | ||
0:55 And we'll see that Python has a lot of interesting flexibility | ||
0:58 around working with sequences, | ||
1:01 and we'll see the best way to do this here. | ||
1:04 Next, functions and methods. | ||
1:06 This will include the use of things like lambda expressions for small inline methods, | ||
1:11 as well as returning multiple values from methods and that sort of things. | ||
1:14 There is a lot to look at to write Pythonic functions. | ||
1:17 One of the great powers of Python | ||
1:19 is the ability to import or pip install a whole variety of packages, | ||
1:25 there is even a great xkcd cartoon about importing packages in Python, | ||
1:31 and we'll see that there are a lot of interesting Pythonic conventions | ||
1:33 around working with packages and modules. | ||
1:36 Next up, we are going to look at classes and objects. | ||
1:40 Object oriented programming in Python is a key corner stone concept, | ||
1:44 even though it may play a slightly less important role | ||
1:47 than languages like Java and CSharp, | ||
1:50 still, classes are really important | ||
1:52 and there is a lot of idiomatic conventions around working with classes, | ||
1:55 we'll focus on that in this section. | ||
1:57 Python has a lot of powerful ways of working with loops, | ||
2:01 one of the first giveaways if somebody is brand new to Python | ||
2:04 is they are not using loops correctly, | ||
2:07 so we'll talk about when and how you should use loops | ||
2:10 and we'll even talk about the controversial else clause | ||
2:12 for for in and while loops. | ||
2:14 Next, we'll talk about tuples. | ||
2:16 Tuples are smallish, read-only collections | ||
2:18 that let you package up related possible heterogeneous data | ||
2:23 and pass it around, | ||
2:25 If we go into a basic database queries and the built in db api | ||
2:28 you'll see that the rows come back as tuples. | ||
2:31 Some of the powerful techniques we'll learn about loops involve tuples | ||
2:34 and we'll see that tuples in general play a really important role, | ||
2:37 and there is some powerful and useful conventions | ||
2:40 around working with tuples in Python. | ||
2:42 Finally, we are going to look beyond the standard library, | ||
2:45 with something I am calling Python for Humans; | ||
2:47 one of the great powers of Python is the ability to go out to PyPi | ||
2:51 and grab one of the over 80 000 packages, | ||
0:34 they are used for data exchange all over the place, | ||
0:37 and there are a lot of interesting use cases and ways | ||
0:40 in which dictionaries are used in a language. | ||
0:43 We are going to talk about a lot of interesting aspects | ||
0:45 and optimal ways to use and leverage dictionaries. | ||
0:49 Next up are working with collections, | ||
0:51 things called list comprehensions and generator expressions. | ||
0:55 And we'll see that Python has a lot of interesting flexibility | ||
0:58 around working with sequences, | ||
1:01 and we'll see the best way to do this here. | ||
1:04 Next, functions and methods. | ||
1:06 This will include the use of things like lambda expressions for small inline methods, | ||
1:11 as well as returning multiple values from methods and that sort of things. | ||
1:14 There is a lot to look at to write Pythonic functions. | ||
1:17 One of the great powers of Python | ||
1:20 is the ability to import or pip install a whole variety of packages, | ||
1:25 there is even a great xkcd cartoon about importing packages in Python, | ||
1:31 and we'll see that there are a lot of interesting Pythonic conventions | ||
1:33 around working with packages and modules. | ||
1:36 Next up, we are going to look at classes and objects. | ||
1:40 Object oriented programming in Python is a key cornerstone concept, | ||
1:44 even though it may play a slightly less important role | ||
1:47 than languages like Java and C#, | ||
1:50 still, classes are really important | ||
1:52 and there is a lot of idiomatic conventions around working with classes, | ||
1:55 we'll focus on that in this section. | ||
1:57 Python has a lot of powerful ways of working with loops, | ||
2:01 one of the first giveaways if somebody is brand new to Python | ||
2:04 is they are not using loops correctly, | ||
2:07 so we'll talk about when and how you should use loops | ||
2:10 and we'll even talk about the controversial else clause | ||
2:12 for "for...in" and "while" loops. | ||
2:14 Next, we'll talk about tuples. | ||
2:16 Tuples are smallish, read-only collections | ||
2:18 that let you package up related possibly heterogeneous data | ||
2:23 and pass it around, | ||
2:25 If we go into a basic database queries and the built in DB API | ||
2:28 you'll see that the rows come back as tuples. | ||
2:31 Some of the powerful techniques we'll learn about loops involve tuples | ||
2:34 and we'll see that tuples in general play a really important role, | ||
2:37 and there is some powerful and useful conventions | ||
2:40 around working with tuples in Python. | ||
2:42 Finally, we are going to look beyond the standard library, | ||
2:45 with something I am calling Python for Humans; | ||
2:47 one of the great powers of Python is the ability to go out to PyPi | ||
2:51 and grab one of the over 80 000 packages, | ||
2:54 install them using pip or something like this | ||
2:57 and add amazing powers to your application. | ||
3:00 People who are new to Python often skip this step | ||
3:03 and they look at something they have to do and are just like | ||
3:07 ok I think I can implement it in these 20 lines of code. | ||
3:10 It's very likely that there is already a package out there | ||
3:14 that you can use to do this, | ||
3:16 so we are going to study two packages one for http | ||
3:18 and one for database access to really bring home this point of look to PyPi | ||
3:24 and look to open source first before you start writing your own algorithms. | ||
3:28 Of course, over time, we may add more topics than what are described here, | ||
3:32 I am sure as more and more people take this class they will say, | ||
3:35 "Hey Michael, did you think about having this", | ||
3:38 or "I also consider this little bit to be idiomatic." | ||
3:40 Now I don't want to just grab every single detail that I can find, | ||
3:44 that is possibly Pythonic code and cram it in here, | ||
3:48 I want to cover the stuff that's most important and not waste your time, | ||
3:50 but of course, I am sure we'll hear about some new ones | ||
3:53 that are great and those may be folded in over time. | ||
|
||
2:57 and add amazing powers to your application. | ||
3:00 People who are new to Python often skip this step | ||
3:03 and they look at something they have to do and are just like | ||
3:07 OK I think "I can implement it in these 20 lines of code". | ||
3:10 It's very likely that there is already a package out there | ||
3:14 that you can use to do this, | ||
3:16 so we are going to study two packages one for HTTP | ||
3:18 and one for database access to really bring home this point of look to PyPi | ||
3:24 and look to open source first before you start writing your own algorithms. | ||
3:28 Of course, over time, we may add more topics than what are described here, | ||
3:32 I am sure as more and more people take this class they will say, | ||
3:35 "Hey Michael, did you think about having this", | ||
3:38 or "I also consider this little bit to be idiomatic." | ||
3:40 Now I don't want to just grab every single detail that I can find, | ||
3:44 that is possibly Pythonic code and cram it in here, | ||
3:48 I want to cover the stuff that's most important and not waste your time, | ||
3:50 but of course, I am sure we'll hear about some new ones | ||
3:53 that are great and those may be folded in over time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
0:01 We are going to write a lot of code in this course. | ||
0:02 And you want to download it and play with it | ||
0:04 and experiment with it and so on | ||
0:07 so of course I am going to put this into a github repository, | ||
0:10 here you can see github.com/mikeckennedy/write-pythonic-code-demos | ||
0:14 so I recommend that you go out there | ||
0:17 and star this so you have it as a reference. | ||
0:01 We are going to write a lot of code in this course. | ||
0:02 And you want to download it and play with it | ||
0:04 and experiment with it and so on | ||
0:06 so of course I am going to put this into a GitHub repository, | ||
0:09 here you can see github.com/mikeckennedy/write-pythonic-code-demos | ||
0:14 so I recommend that you go out there | ||
0:16 and star this so you have it as a reference. |
Oops, something went wrong.