Best Practices

Python Style Guide

Comment


# Single line comment 

# Multiple 
# line 
# comment
          

Doc String


"""Single line docstring"""

"""
Multiple 
line 
docstring
"""
          

Variable


stuff = 'stuff'
          

String


'Single line string'

''' 
Multiple 
line 
string
'''
          

Integer


42
          

Boolean


True
# or 
False
          

List


['stuff', 'things']
          

Tuple


('stuff', 'things')
          

Dictionary


{'stuff_key': 'stuff_value', 'things_key': 'things_value'}
          

For Loop


# Iterate list or tuple
for thing in things:
    print(thing)

# Iterate dictionary
for key, value in stuff.items():
    print(key, value)
          

While Loop


thing = True
while thing is True:
    print(thing)
    thing = False
          

If, Elif, Else


if 1 > 42:
    print('One')
elif 42 < 1:
    print('The answer')
else:
    print('Maths is fun')
          

Function


def stuff():
    """Get stuff"""
    stuff = ['stuff', 'more stuff', 'other stuff']
    return stuff
          

Class


class StuffAndThings(object):
    """Stuff and things"""
    def __init__(self, stuff, things):
        self.stuff = stuff
        self.things = things

    def get_stuff():
        """Get stuff"""
        return self.stuff

    def get_things():
        """Get things"""
        return self.things      
          

Using Modules


import os
          






















Updated: 2017-10-02