1 00:00:00,410 --> 00:00:04,278 Guiding the poetry of Python, PEP 20 is written in verse. 2 00:00:04,278 --> 00:00:10,090 PEP 20, or The Zen of Python, influences a lot of how you think about Python code. 3 00:00:10,090 --> 00:00:12,820 It's actually an Easter egg in Python itself that you can access from 4 00:00:12,820 --> 00:00:14,540 inside any Python shell. 5 00:00:14,540 --> 00:00:15,890 Let's check it out. 6 00:00:15,890 --> 00:00:20,192 So, I'm just in my Python shell and I want to import the library this. 7 00:00:20,192 --> 00:00:22,850 This is actually almost kind of a, a joke or an Easter egg. 8 00:00:24,420 --> 00:00:25,570 I'll let you read the whole thing for 9 00:00:25,570 --> 00:00:28,380 yourself on your own computer or in workspaces. 10 00:00:28,380 --> 00:00:31,230 But we're going to talk about some of the more important lines, 11 00:00:31,230 --> 00:00:34,210 not necessarily in any specific order. 12 00:00:34,210 --> 00:00:36,680 Explicit is better than implicit. 13 00:00:36,680 --> 00:00:38,770 Python doesn't like to do things in the background. 14 00:00:38,770 --> 00:00:40,620 The language was designed so 15 00:00:40,620 --> 00:00:44,170 that you have to specify what you want to do all the time. 16 00:00:44,170 --> 00:00:47,480 This is why, for example, you can't add numbers to strings. 17 00:00:47,480 --> 00:00:49,950 That would require changing the string to a number or 18 00:00:49,950 --> 00:00:53,000 the number to a string behind the scenes. 19 00:00:53,000 --> 00:00:56,730 This goes against Python's rule of avoiding implicit actions. 20 00:00:56,730 --> 00:00:58,270 Readability counts. 21 00:00:58,270 --> 00:01:01,430 Going along with rules of PEP 8, Python wants to be readable. 22 00:01:01,430 --> 00:01:05,020 Anytime you find yourself trying to figure out what a chunk of code does, 23 00:01:05,020 --> 00:01:08,480 not because you haven't seen the functions before, but because you can't tell which 24 00:01:08,480 --> 00:01:11,770 variable's which, it's a good sign that you need more readable code. 25 00:01:13,080 --> 00:01:15,720 White space and well-named variables, functions and 26 00:01:15,720 --> 00:01:18,350 classes all lead to better readability. 27 00:01:18,350 --> 00:01:21,520 Why worry about formatting when you can just add in comments? 28 00:01:21,520 --> 00:01:23,820 Comments are where the insanity goes. 29 00:01:23,820 --> 00:01:27,330 Clean code doesn't need nearly as many explanatory comments. 30 00:01:27,330 --> 00:01:30,540 Special cases aren't special enough to break the rules. 31 00:01:30,540 --> 00:01:34,570 This is advice to use similar approaches to similar problems. 32 00:01:34,570 --> 00:01:38,858 This is also the reason that Python uses, say, the len function to get the length of 33 00:01:38,858 --> 00:01:42,977 things instead of relying on each type to give its own solution to that problem. 34 00:01:42,977 --> 00:01:45,719 It's the same for looping over items, deleting variables and 35 00:01:45,719 --> 00:01:49,200 collection members, and many other common scenarios. 36 00:01:49,200 --> 00:01:52,575 This rule has a corollary though, that practicality beats purity. 37 00:01:52,575 --> 00:01:55,270 It's better to have a practical solution than one that 38 00:01:55,270 --> 00:01:58,010 fits some false ideal of perfection. 39 00:01:58,010 --> 00:02:00,080 Beautiful is better than ugly. 40 00:02:00,080 --> 00:02:02,530 And lastly, my favorite and one of my reasons for 41 00:02:02,530 --> 00:02:06,190 sticking with Python over every other language I've investigated, 42 00:02:06,190 --> 00:02:08,468 your code should be pleasant to look at and read. 43 00:02:08,468 --> 00:02:11,540 Following PEP 8 will help with this a lot. 44 00:02:11,540 --> 00:02:15,080 No one wants to wade through a mess of single-letter variables, deeply-nested for 45 00:02:15,080 --> 00:02:18,520 loops and conflicting comments just to fix a bug. 46 00:02:18,520 --> 00:02:19,790 Coding should be pleasant. 47 00:02:20,940 --> 00:02:23,350 Hopefully, these few tidbits, along with the rest of PEP 20, 48 00:02:23,350 --> 00:02:25,870 will help you to write more pleasant Python code. 49 00:02:25,870 --> 00:02:30,240 If you need to see the list again, just run import this in your Python shell. 50 00:02:30,240 --> 00:02:32,040 Now let's get back to more utilitarian topics.