Best Practices
This is our definitive list of guidelines, suggestions, recommendations, etc
Numbers 1 - 10 are aimed at Beginner Programmers.
1) Use fstrings for string formatting
2) Start your comments where the code starts. (VBA - 6)
SS
Make the indentation of the comment match the indentation of the code it refers to.
Do not left align all your comments.
3) Use meaningful definition names, variable names, etc. (VBA - 10)
Use "snake_case" for variables and functions
Use a name that describes the task they perform.
use descriptive names for loop variables
You should try and use a consistent naming convention.
user_name
calculate_total()
Constants - ALL CAPITALS
4) import only what you need
This makes the dependencies clearer
Group and order the imports in this order - standard library, third-party, local
from math import sqrt
5) use join() to concatenate strings not (+)
This is faster
join("one", "two", "three")
6) use isinstance() for type checking not type()
if isinstance(value, int)
7) Use get() for dictionary access with defaults
age = person.get("age",0)
8) do not use "==" to compare with none, use is
if value is None
9) use "in" to check membership, not loops
if item in my_list
10) use with statement for file handling
with open("file.txt") as f
data = f.read()
11) do not use mutable defaults in function arguments
def add_item(item, items=None)
if items is None
items = []
12) use sorted() instead of sort() when you want a new list
This does not modify the original, it returns a new list that is sorted
new_list = sorted(my_list)
13) use any() and all() for checking multiple conditions
if any(x > 100 for x in values)
14) use zip to iterate over multiple sequences together
for name, age in zip(names, ages)
15) use len() to check if a sequence is empty, not truthiness
if (len(my_list) > 0)
16) use startswith() and endswith() for string checks
Avoid using arrsays and index like if filename[-5:] = ".xlsx"
if filename.startswith("ab")
if filename.endswith(".xlsx")
17) use enumerate() when you need both index and value
for index, item in enumerate(items)
18) document your definitions with docstrings
def add(a, b)
"""adds two numbers"""
return a + b
Coding standards
link - peps.python.org/pep-0008/
if a_string != "":
...
# Simplifies to
if a_string:
...
if a_number != 0:
...
# Simplifies to
if a_number:
...
def check_senior(age: int) -> bool:
# This `if` statement is unnecessary!
if age > 60:
return True
else:
return False
# Exactly equivalent to
def check_senior(age: int) -> bool:
return age > 60
def check_toddler(age: int):
# The `== True` part is redundant!
return (age >= 5) == True
def check_toddler(age: int):
# Much better!
return age >= 5
# Defining a function to reverse a string
def reverse_a_string():
# Reading input from console
a_string = input("Enter a string")
new_strings = []
# Storing length of input string
index = len(a_string) # Reversing the string using while loop
while index:
index -= 1
new_strings.append(a_string[index])
#Printing the reversed string
print(''.join(new_strings))
reverse_a_string()
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext