Avoid using parenthesis around if statements, unless the statement expands
on multiple lines or you need to nest your conditions.

Bad Examples:

if(x==3):print"condition is true"if(x==3andy==4):print"condition is true"

Good Examples:

ifx==3:print"condition is true"ifx==3andy==4:print"condition is true"if(extremely_long_variable==3andanother_long_variable==4andyet_another_variable==5):print"condition is true"if(2+3+4)-(1+1+1)==6:print"condition is true"

Consider refactoring your statement into a function if it becomes too long,
or the meaning isn’t clear.

Avoid throwing Exception. Use one of Python’s built-in Exceptions, or create
your own custom Exception. A list of exceptions is available on the Python documentation website.

Bad Example:

defget_user(userid):user=session.query(User).get(userid)ifnotuser:raiseException("User not found")

Good Example:

classUserNotFoundError(LookupError):def__init__(self,userid):message="user with id %s not found"%useridLookupError.__init__(self,message)defget_user(userid):user=session.query(User).get(userid)ifnotuser:raiseUserNotFoundError(userid)

Never use except: without specifying any exception type. The reason is that it will also catch important exceptions, such as KeyboardInterrupt and OutOfMemory exceptions, making your program unstoppable or continuously failing, instead of stopping when wanted.