Don't compare a variable to 0
if there is no need:
Good example:
if number_of_students:
pass
Bad example:
if number_of_students != 0:
pass
- Use one style of quotes in your code. Double quotes are preferable.
- Use
( )
while importing multiple classes/modules:
Good example:
from app.main import (
KnightWithArmour,
KnightWithoutArmour,
SuperHeroKnightWithGun
)
Bad example:
from app.main import KnightWithArmour, \
KnightWithoutArmour, \
SuperHeroKnightWithGun
- Use descriptive error messages:
Good example:
if error:
raise CustomError("We have an error on the line 16.")
Bad examples:
if error:
raise CustomError
if error:
raise CustomError("CustomError")
- Avoid using unnecessary
else
:
Good example:
def go_to_cinema(condition: bool) -> str:
if condition:
return "We are going to the cinema!"
return "We are not going to the cinema("
Bad example:
def go_to_cinema(condition: bool) -> str:
if condition:
return "We are going to the cinema!"
else:
return "We are not going to the cinema("
- Use annotation, it is a good practice:
Good example:
def multiply_by_2(number: int) -> int:
return number * 2
Bad example:
def multiply_by_2(number):
return number * 2
Add comments, prints, and functions to check your solution when you write your code. Don't forget to delete them when you are ready to commit and push your code.