Before anything else: best practices when it comes to writing Python scripts. Now, if you’re new to this whole shebang, don’t worry we’ll keep it simple and easy to understand.
1️ Keep your code clean and concise. This means avoiding unnecessary lines of code that can be condensed into a single line or function call. For example:
# Before
# The following script checks if the value of x is equal to 0 and prints a corresponding message.
if x == 0: # Checks if x is equal to 0
print("x is zero") # Prints "x is zero" if x is equal to 0
else:
print("x is not zero") # Prints "x is not zero" if x is not equal to 0
# After
# The following script checks if the value of x is equal to 0 and prints a corresponding message.
print("x is" if x == 0 else "not zero") # Prints "x is" if x is equal to 0, otherwise prints "not zero"
2️ Use descriptive variable names. This makes it easier for other developers (or your future self) to understand what the code does and why. For example:
# Before
# The variable "total" is used to store the sum of numbers from 1 to 9.
total = 0
# A for loop is used to iterate through the numbers from 1 to 9.
for i in range(1, 10):
# The current number is added to the total.
total += i
# After
# The variable "total_of_numbers" is used to store the sum of numbers from 1 to 9.
total_of_numbers = sum([i for i in range(1, 10)])
# A list comprehension is used to create a list of numbers from 1 to 9.
# The sum() function is then used to calculate the total of the numbers in the list.
# This results in the same outcome as the original script, but with more descriptive variable names and a more concise code structure.
3️ Use comments sparingly. As the saying goes: “If it’s not broken, don’t fix it.” If your code is clear and concise enough to explain itself, then there’s no need for a comment. However, if you do feel like adding a comment, make sure it adds value and doesn’t just repeat what the code already says. For example:
# Before
# Setting variables x and y to 5 and 10 respectively
x = 5
y = 10
# Calculating the sum of x and y and assigning it to variable z
z = x + y
# After
# Assigning values 5 and 10 to variables x and y respectively
x, y = 5, 10
# Calculating the sum of x and y and assigning it to variable z
z = x + y
4️ Use whitespace effectively. This means using indentation to separate code blocks (like if statements or loops) and adding blank lines between functions for readability. For example:
# Before
def calculate_total(numbers):
total = 0
for num in numbers:
total += num
return total
# After
def calculate_total(numbers):
"""Calculates the sum of all given numbers"""
total = 0 # Initialize a variable to store the sum of numbers
for num in numbers: # Loop through each number in the given list
total += num # Add the current number to the total
return total # Return the final sum of numbers
5️ Use docstrings to document your code. This is especially important if you’re working on a larger project with multiple developers or plan to share your code publicly. Docstrings should include a brief summary of what the function does, as well as any input and output parameters. For example:
# Define a function called calculate_total that takes in a list of numbers as input
def calculate_total(numbers):
"""Calculates the sum of all given numbers"""
# Initialize a variable called total to keep track of the sum
total = 0
# Use a for loop to iterate through each number in the input list
for num in numbers:
# Add the current number to the total
total += num
# Return the final total
return total
And that’s it, These are just a few best practices to help you write Python scripts like a pro. Remember to keep your code clean and concise, use descriptive variable names, avoid unnecessary comments, use whitespace effectively, and document your code with docstrings.