Logic For Patterns In Python