Whitespace is used appropriately to…
Logically separate pieces of your algorithm
Let’s say you are writing a function to return the sum of the first n
numbers in the list that are greater than 0:
def sum_of_first_n(numbers, n):
"""
return the sum of the first n numbers in a list that are greater than 0
Args:
numbers (List[float]): The list of numbers that we will use to compute the sum
Assumptions: numbers includes at least n numbers that are greater than 0
n (int): The number of numbers that should be included in the sum
Assumptions: n >= 1
Returns:
float: the sum of the first n numbers in numbers that are > 0
"""
keep_numbers = []
for number in numbers:
if number > 0:
keep_numbers.append(number)
first_n = keep_numbers[:n]
total = 0
for number in first_n:
total += number
return total
The code is more readable if you put some space between the logically distinct pieces of your algorithm and add comments to explain what you are doing:
def sum_of_first_n(numbers, n):
"""
return the sum of the first n numbers in a list that are greater than 0
Args:
numbers (List[float]): The list of numbers that we will use to compute the sum
Assumptions: numbers includes at least n numbers that are greater than 0
n (int): The number of numbers that should be included in the sum
Assumptions: n >= 1
Returns:
float: the sum of the first n numbers in numbers that are > 0
"""
# create list of numbers to keep that are > 0
keep_numbers = []
for number in numbers:
if number > 0:
keep_numbers.append(number)
# get first n numbers
first_n = keep_numbers[:n]
# compute sum
total = 0
for number in first_n:
total += number
# return the final sum
return total
Make expressions more readable
Please separate operators such as +
, -
, %
, /
, >
, ==
, etc., from variables using one space. Do not separate variables or literals from colons (:
) or parentheses ((
, )
). Function calls should always be directly followed by parentheses without extra spaces.
Here’s an example where whitespace could be improved:
if n>3 or n % 2==0 :
print ( "Even or greater than three" )
This is the preferred whitespace:
if n > 3 or n % 2 == 0:
print("Even or greater than three")
Consistent style is used for variable, class, and function names
It’s fine to use snake_case
or camelCase
in your code. In my examples, I will use snake_case
.
This is fine (snake case):
def compute_cirlce_area(the_radius):
"""
Computes the area of a circle
Arguments:
the_radius (float): the radius of the circle
Returns:
float: the area of the circle
"""
pi = 3.14
the_area = pi * the_radius ** 2
return the_area
This is also fine (camel case):
def computeCircleArea(theRadius):
"""
Computes the area of a circle
Arguments:
theRadius (float): the radius of the circle
Returns:
float: the area of the circle
"""
pi = 3.14
theArea = pi * theRadius ** 2
return theArea
But please don’t mix them like this:
def compute_circleArea(theRadius):
"""
Computes the area of a circle
Arguments:
theRadius (float): the radius of the circle
Returns:
float: the area of the circle
"""
pi = 3.14
the_area = pi * theRadius ** 2
return the_area
Line length does not exceed 100 characters
This one’s pretty self-explanatory. Avoid things like this:
def compute_circle_areas(radii):
"""
Computes the sum of areas of multiple circles defined by radii
Arguments:
radii (List[float]): a list of radii of circles
Returns:
float: the area circles
"""
the_total_area = 0
for radius in radii:
the total_area = the total_area + radius * radius * 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
return the_total_area
How do you know how long your lines are?
Thonny allows you to add a vertical line to your editor so that you can see if your lines exceed 100 characters. The GIF below demonstrates how to use that setting:
But how do I avoid this?
There are a few options! This is probably what I would do:
def compute_circle_areas(radii):
"""
Computes the sum of areas of multiple circles defined by radii
Arguments:
radii (List[float]): a list of radii of circles
Returns:
float: the area circles
"""
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
total = 0
for radius in radii:
total += radius ** 2 * pi
return total
But it is also acceptable to use extra variable:
def compute_circle_areas(radii):
"""
Computes the sum of areas of multiple circles defined by radii
Arguments:
radii (List[float]): a list of radii of circles
Returns:
float: the area circles
"""
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
total = 0
for radius in radii:
circle_area = radius * radius * pi
total += circle_area
return total
And if necessary, you can even use \
to break a line:
def compute_circle_areas(radii):
"""
Computes the sum of areas of multiple circles defined by radii
Arguments:
radii (List[float]): a list of radii of circles
Returns:
float: the area circles
"""
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
total = 0
for radius in radii:
total += radius ** 2 \
* pi
return total
I will not penalize you if a variable declaration uses more than 100 characters (although this is avoidable for strings) as long as you only use the assignment operator (=
).
“Commented out” code that is not functional is removed
It’s common to write code that doesn’t work and use comments to stop it from running as you try to write new code. However, once you have a final solution, please remove commented out code. Don’t submit code that looks like this:
def compute_circle_area(radius):
"""
Computes the area of a circle
Arguments:
radius (float): the radius of the circle
Returns:
float: the area of the circle
"""
# WHY DOESN'T THIS WORK???????
# pi = 3.14
# area = 3.14 * radius ^ 2
# return area
pi = 3.14
area = 3.14 * radius ** 2
return area