# Nikhil Akki's blog # Top 10 Python best practices

## Some examples of Python Best Practices for Clean and Efficient Code

1. Follow PEP 8 style guide: PEP 8 is the official style guide for Python code. Following PEP 8 can make your code more readable and easier to maintain.

`````` # Good
"""
Calculates the area of a circle with given radius.
"""
return 3.14 * radius ** 2

def calculateArea(r):
return 3.14 * r ** 2
``````
2. Use descriptive names: Use descriptive and meaningful names for variables, functions, classes, and modules. This makes your code more understandable to others.

`````` # Good
def calculate_salary(employee):
"""
Calculates the salary for the given employee.
"""
hourly_rate = employee.hourly_rate
hours_worked = employee.hours_worked
salary = hourly_rate * hours_worked
return salary

def calc(employee):
hr = employee.hr
hw = employee.hw
sal = hr * hw
return sal
``````
3. Write docstrings: Write docstrings for all functions, classes, and modules. Docstrings describe what the code does and how to use it.

`````` # Good
def find_largest_number(numbers):
"""
Returns the largest number from a list of numbers.

Args:
numbers: list of integers

Returns:
largest number in the list
"""
return max(numbers)

def largest(numbers):
return max(numbers)
``````
4. Avoid global variables: Avoid using global variables as they can cause unexpected behaviour and make your code harder to understand and test.

`````` # Good
def calculate_tax(amount):
tax_rate = 0.10
tax = amount * tax_rate
return tax

tax_rate = 0.10

def calculate_tax(amount):
tax = amount * tax_rate
return tax
``````
5. Use list comprehensions: Use list comprehensions instead of loops to create lists. List comprehensions are more concise and readable.

`````` # Good
numbers = [1, 2, 3, 4, 5]
squares = [num ** 2 for num in numbers]

numbers = [1, 2, 3, 4, 5]
squares = []
for num in numbers:
squares.append(num ** 2)
``````
6. Use context managers: Use context managers to properly manage resources such as files, sockets, and database connections.

`````` # Good
with open('file.txt', 'r') as f:
# do something with data

f = open('file.txt', 'r')
# do something with data
f.close()
``````
7. Avoid unnecessary complexity: Keep your code simple and easy to understand. Avoid unnecessary complexity and don't over-engineer solutions.

`````` # Good
def calculate_average(numbers):
return sum(numbers) / len(numbers)

def calculate_average(numbers):
total = 0
for num in numbers:
total += num
average = total / len(numbers)
return average
``````
8. Write unit tests: Write unit tests for your code to ensure that it works as expected and to catch any bugs or errors early.

`````` # Good
def test_calculate_area():
assert calculate_area(5) == 78.5
assert calculate_area(0) == 0
assert calculate_area(-5) == 78.5

return 3.14 * radius ** 2
``````
9. Use virtual environments: Use virtual environments to isolate your Python environment and avoid dependency conflicts.

`````` # Good
\$ python3 -m venv venv
\$ source venv/bin/activate
(myenv) \$ pip install requests

\$ pip install requests
``````
10. Optimize code: Optimize your code for speed and efficiency. This includes using built-in functions and libraries, avoiding unnecessary computation, and using appropriate data structures.

``````# Good
def sum_squares(numbers):
return sum(num**2 for num in numbers)