June 10, 2020, 2:56 PM
·python
programming
revised-post
Python is popular for its simplicity and readability. In this article, I'll share useful tips and tricks to help you write clean, readable, and efficient Python code.
Using Python's built-in names as variable names can override their default behavior, making them unavailable or causing errors.
# Bad
id = 2
zip = 567890
class = "physics"
# Good
user_id = 2
zip_code = 567890
class_ = "physics"
Use proper naming conventions for better readability. Camel case and snake case are the most common styles.
# Bad
secondary_Title = "hello world"
Name = "rubbie" # Use lowercase for variables
# Good
secondaryTitle = "hello world" # Camel case
secondary_title = "hello world" # Snake case
# Use PascalCase for classes
class ProximitySensor:
def read(self):
"""
1. Use triple quotes for docstrings.
2. Explain the purpose of the function clearly.
"""
pass
# Use UPPERCASE with underscores for constants
PI = 3.141592
OPERATIONAL_EFFICIENCY = 0.67
Using tabs can cause inconsistent formatting across different editors. Use spaces instead (preferably 4 per indentation level) to ensure consistent readability.
Avoid unnecessary conditional statements when assigning Boolean values.
# Poor
if True:
is_active = True
else:
is_active = False
boys, girls = 5, 19
if boys == girls:
equal = True
else:
equal = False
# Better
is_active = True
boys, girls = 5, 19
equal = boys == girls
# Instead of this
if is_active:
x = 1
else:
x = -1
# Do this
x = 1 if is_active else -1
Use underscores to separate digits in large numbers for better readability.
connections_accepted = 1_000_000_000_000
connections_rejected = 1_000
connections_total = connections_accepted + connections_rejected
Manually opening and closing files is prone to errors. Use with
statements to manage file resources efficiently.
# Bad
file = open("text.txt")
content = file.read()
file.close()
# Good
with open("text.txt") as file:
content = file.read()
Avoid manually tracking index variables when looping through lists.
# Bad
items = ["apple", "pear", "papaya", "mango"]
index = 0
for item in items:
print(index, item)
index += 1
# Good
for index, item in enumerate(items):
print(index, item)
# Start from index 1
for index, item in enumerate(items, start=1):
print(index, item)
zip
Instead of manually managing multiple lists, use zip()
.
# Bad
names = ["rubbie", "jerome", "carlie", "angie"]
hobbies = ["painting", "surfing", "cycling", "singing"]
index = 0
for name in names:
print(f"{name} loves {hobbies[index]}")
index += 1
# Good
for name, hobby in zip(names, hobbies):
print(f"{name} loves {hobby}")
If a loop variable is unnecessary, replace it with an underscore (_
).
# Bad
for i in range(6):
do_something()
# Good
for _ in range(6):
do_something()
Annotations help define function input and return types, making the code easier to understand.
# Without annotations
def add(a, b):
return a + b
# With annotations
def add(a: int, b: int) -> int:
return a + b
Annotations are optional but improve code readability, especially in larger projects.
These tips will help you write cleaner and more efficient Python scripts. There's always more to learn, but this is a great starting point!
/
Rubbie kelvin © 2025 Made with love & vue · Why does your portfolio look plain?