Best Practices

Python Style Guide

Comment

python
# Single line comment

# Multiple
# line
# comment

Doc String

python
"""Single line docstring"""

"""
Multiple
line
docstring
"""

Variable

python
stuff = 'stuff'

String

python
'Single line string'

'''
Multiple
line
string
'''

Integer

python
42

Boolean

python
True

# or

False

List

python
['stuff', 'things']

Tuple

python
('stuff', 'things')

Dictionary

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

For Loop

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

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

While Loop

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

If, Elif, Else

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

Function

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

Class

python
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

python
import os
# python