def in_binary(n):
    if n == 0:
        return [] # base case
    else:
        digit = n & 0x1 # bit mask
        return in_binary(n >> 1) + [digit] # recursion

def from_binary(b):
    # add value of each digit, using enumerate to get its place
    # list comprehensions are Pythonic
    return sum([(2**place) * value
                for place, value in enumerate(b)])

two = in_binary(2)
print from_binary(two + two)