- PEP 0008 – Style Guide for Python Code (https://www.python.org/dev/peps/pep-0008/)
- PEP 257 -- Docstring Conventions (https://www.python.org/dev/peps/pep-0257/)
Program Python like a pro! Code in the right-hand column better to employers and other places you might send your code. It also helps avoid some common pitfalls. Finally, most of the code you see will use the patterns in the right-column; practice writing code this way will help you understand code that you read, when you work on a team or with code from cookbooks and other projects.
| Anti-Patterns | Patterns | |
|---|---|---|
| Conditions | if condition is True: |
if condition: |
if condition is False: |
if not condition: |
|
if condition:
return True
else:
return False
|
return condition |
|
| Parentheses and Whitespace |
a =1
a= 1
a=1
|
a = 1 |
f (a, b) |
f(a, b) |
|
if(a == 1) |
if a == 1 |
|
| Indexing | s[len(s) - 1] |
s[-1] |
| Iteration |
i = 0
while i < n:
…
i += 1
|
for i in range(n):
…
|
i = start
while i <= stop:
…
i += 1
|
for i in range(start, stop + 1):
…
|
|
i = 0
while i < n:
…
i += 2
|
for i in range(0, n, 2):
…
|
|
for i in range(len(s)):
do something with s[i]
|
for c in s:
do something with c
|
|
for i in range(len(s)):
do something with i and s[i]
|
for i, c in enumerate(s):
do something with i and c
|