DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

the PEP 8 style guide is to use four spaces of indentation
before each block of code. Why four spaces? Won’t one
space work? Yes, it will, but your code blocks will be hard
to align, and you will end up having to do extra work.


The alignment issue becomes especially important when
you nest loops and conditional statements, as each loop
needs to correspond to another block of code, indented
using spaces. Many text editors allow you to view
whitespace, and some even give you a visual indication of
what is in a code block. Figure 3-2 shows this in Atom.


Figure 3-2 Spaces and Code Blocks in Atom


Comments in Python are created by entering # or a
string of three quotation marks (either single or double
quotation marks). One very important good practice
when coding is to write a description of what is
happening in code that is not obvious. You probably will
not want to write a comment for a simple print
statement, but describing the output of a nested function
would be useful for anyone who needs to make additions
to your code in the future or to remind yourself why you
did what you did during that late night caffeine-fueled
coding session. The # is used to comment out a single
line so the Python interpreter ignores it. Here is an
example:


Click here to view code image

Free download pdf