^^Guidelines to improve the readability of code.

code is read much more often than it is written

Code lay-out

Indentation Use 4 spaces per indentation level. Don't use tabs. Never mix tabs and spaces.
Maximum Line Length Limit all lines to a maximum of 79 characters.

Whitespace in Expressions and Statements

    Deprecato
1
spam(ham[1], {eggs: 2})
spam( ham[ 1 ], { eggs: 2 } )
2
if x == 4: print x, y; x
if x == 4 : print x , y ; x
3
spam(1)
spam (1)
4
dict['key'] = list[index]
dict ['key'] = list [index]
5
x = 1
y = 2
long_variable = 3
x             = 1
y             = 2
long_variable = 3
6
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
7:8
def complex(real, imag=0.0):
return magic(r=real, i=imag)
def complex(real, imag = 0.0):
return magic(r = real, i = imag)

 

Avoid extraneous whitespace in the following situations:

  1. Immediately inside parentheses, brackets or braces.
  2. Immediately before a comma, semicolon, or colon:
  3. Immediately before the open parenthesis that starts the argument list of a function call:
  4. Immediately before the open parenthesis that starts an indexing or slicing:
  5. More than one space around an assignment (or other) operator to align it with another.
  6. Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
    Use spaces around arithmetic operators.
  7. Don't use spaces around the '=' sign when used to indicate a   keyword argument or a default parameter value.
  8. Compound statements (multiple statements on the same line) are generally discouraged.

 

Python coders from non-English speaking countries: please write your comments in English, unless you are 120% sure that the code will never be read by people who don't speak your language.
 

Don't do this:
    x = x + 1                 # Increment x
  But sometimes, this is useful:
    x = x + 1                 # Compensate for border

Robust code