Python Tutorial - Conventions and PEP 8

The Power of Social Conventions and Python Enhancement Protocol 8 (PEP 8)

As we explore the world of software engineering, it's essential to recognize that different cultures and communities follow distinct guidelines that help them run smoothly. This phenomenon is known as social conventions, which can be seen in various aspects of life, such as greetings depending on the community setting. For instance, saying hello by shaking hands, bowing, or bumping fists might make sense in certain contexts.

Similarly, in the world of software engineering, there are conventions that differ based on the language and community. Fortunately, Python Enhancement Protocol 8 (PEP 8) is not an unwritten rule; we can turn to it for guidance. PEP 8 is the de facto style guide for Python code, providing us with the knowledge to format our code in a way that maximizes readability.

Readability is crucial in software engineering, as code is read much more often than it is written. Therefore, following PEP 8 best practices can greatly improve the overall quality of our code. Let's examine an example of poorly formatted code that violates PEP 8 guidelines. The code in question has several issues: the module import isn't at the top of the file, spacing and indentation are inconsistent, and the lack of line breaks makes it difficult to discern when one idea finishes and the next begins.

Even without knowing the specific PEP 8 rules being broken, it's clear that this code is not the most readable chunk. Fortunately, with a little bit of effort, we can rewrite the same code to conform to PEP 8 guidelines, using whitespace appropriately and following the agreed-upon rules. The result is significantly more readable code that accomplishes the same task.

So, how do we keep up with the many rules defined in PEP 8? Thankfully, there are tools available that can check our code for us, just like a spell checker. Personally, I use an IDE that flags violations as soon as I write a bad line of code. Additionally, other options such as PI Code Style package can be utilized. PI Code Style can check code in multiple files at once and outputs descriptions of the violations along with information on exactly where we need to go to fix the issue.

To install and use PI Code Style from the shell, simply follow the provided example code. We'll also explore running PI Code Style from a Python script, using the command-line interface to check the contents of the file containing our poorly formatted code. Note that the output has been truncated for this example, but it shows us the exact location of any violations by displaying the file name, line number, and column number where the problem occurred.

Moreover, PI Code Style outputs a human-readable description of the PEP 8 violation and an error code. For more information on the possible error codes, please refer to the package's documentation. We've covered the importance of PEP 8 and how it can improve our code's readability. Now that we've discussed these essential guidelines, let's move on to some exercises to get hands-on experience with PI Code Style and other tools.

In conclusion, understanding social conventions and Python Enhancement Protocol 8 is crucial in software engineering. By recognizing the importance of readability and utilizing tools like PI Code Style package, we can improve our coding skills and produce high-quality code that meets the standards of PEP 8 guidelines.

"WEBVTTKind: captionsLanguage: engreat work on those exercises in our short time together we've seen how helpful the Python community can be not only have they provided some excellent packages for us to use but they've also provided documentation to help us use them something you probably know from real life is that different cultures and communities follow different guidelines that can help them run smoothly for example this can be seen in different greetings depending on which community setting you're in it might make sense to say hello by shaking hands bowing or bumping fists these unwritten rules are known as social conventions the world of software engineering also has conventions that differ based on the language and community luckily for pythonista z' these conventions aren't unwritten we can turn to Python enhancement protocol 8 or pep 8 pep 8 is the de facto style guide for Python code it lets us know how to format our code to be as readable as possible and to quote pep 8 code is read much more often than it is written so readability is not something that should be overlooked let's see an example here we have some code that violates pep 8 best practices to put it simply the code here is hard to read a few problems are the module import isn't at the top of the file the spacing and indentation is inconsistent and the lack of line breaks makes it difficult to tell when one idea finishes and the next one begins even without knowing the specific pep 8 rules being broken you can probably tell this isn't the most readable chunk of code let's see what the same chunk of code looks like after being rewritten to conform to pep 8 much better by following the agreed-upon rules in pep 8 and using whitespace appropriately this code became much more readable despite accomplishing the same exact task so how is it possible to keep up with the many rules defined in pep 8 thankfully for you and me there are tools that can check your code for you just like a spell checker personally I use an IDE that Flags violations as soon as I write a bad line of code but there are other options one in particular that you'll be exposed to in this course is the PI code style package PI code style can check code in multiple files at once and it outputs descriptions of the violations along with information to let you know exactly where you need to go to fix the issue here is some example code on how to install and use PI code style from the shell as you'll see in the exercises later you can also run PI code style from a Python script here we use the command-line interface to check the contents of the file dick to array dot pi that contains our poorly formatted code from before note that the output we see on this slide has been truncated the output shows us the exact location of any violations by showing the file name line number and column number where the problem occurred note that the output does not use zero-based indexing additionally pi code style outputs a human readable description of the pep 8 violation and an error code a complete list of pi code styles possible error codes can be seen in the packages documentation ok we've covered the importance of pep 8 and how it can improve your codes readability let's move on to some exercises to get some handsgreat work on those exercises in our short time together we've seen how helpful the Python community can be not only have they provided some excellent packages for us to use but they've also provided documentation to help us use them something you probably know from real life is that different cultures and communities follow different guidelines that can help them run smoothly for example this can be seen in different greetings depending on which community setting you're in it might make sense to say hello by shaking hands bowing or bumping fists these unwritten rules are known as social conventions the world of software engineering also has conventions that differ based on the language and community luckily for pythonista z' these conventions aren't unwritten we can turn to Python enhancement protocol 8 or pep 8 pep 8 is the de facto style guide for Python code it lets us know how to format our code to be as readable as possible and to quote pep 8 code is read much more often than it is written so readability is not something that should be overlooked let's see an example here we have some code that violates pep 8 best practices to put it simply the code here is hard to read a few problems are the module import isn't at the top of the file the spacing and indentation is inconsistent and the lack of line breaks makes it difficult to tell when one idea finishes and the next one begins even without knowing the specific pep 8 rules being broken you can probably tell this isn't the most readable chunk of code let's see what the same chunk of code looks like after being rewritten to conform to pep 8 much better by following the agreed-upon rules in pep 8 and using whitespace appropriately this code became much more readable despite accomplishing the same exact task so how is it possible to keep up with the many rules defined in pep 8 thankfully for you and me there are tools that can check your code for you just like a spell checker personally I use an IDE that Flags violations as soon as I write a bad line of code but there are other options one in particular that you'll be exposed to in this course is the PI code style package PI code style can check code in multiple files at once and it outputs descriptions of the violations along with information to let you know exactly where you need to go to fix the issue here is some example code on how to install and use PI code style from the shell as you'll see in the exercises later you can also run PI code style from a Python script here we use the command-line interface to check the contents of the file dick to array dot pi that contains our poorly formatted code from before note that the output we see on this slide has been truncated the output shows us the exact location of any violations by showing the file name line number and column number where the problem occurred note that the output does not use zero-based indexing additionally pi code style outputs a human readable description of the pep 8 violation and an error code a complete list of pi code styles possible error codes can be seen in the packages documentation ok we've covered the importance of pep 8 and how it can improve your codes readability let's move on to some exercises to get some hands\n"