Learn Python Coding
40.4K subscribers
702 photos
36 videos
24 files
502 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
def process_data(data):
if data is None:
return "Error: No data provided."
if not isinstance(data, list) or not data:
return "Error: Invalid data format."

# ... logic is now at the top level ...
print("Processing data...")
return "Done"


#Python #CleanCode #Programming #BestPractices #CodingTips

━━━━━━━━━━━━━━━
By: @DataScience4
9. Use isinstance() for Type Checking
(It's safer and more robust than type() because it correctly handles inheritance.)

Cluttered Way (brittle, fails on subclasses):
class MyList(list): pass
my_list_instance = MyList()
if type(my_list_instance) == list:
print("It's a list!") # This will not print

Clean Way (correctly handles subclasses):
class MyList(list): pass
my_list_instance = MyList()
if isinstance(my_list_instance, list):
print("It's an instance of list or its subclass!") # This prints


10. Use the else Block in try/except
(Clearly separates the code that runs on success from the try block being monitored.)

Cluttered Way:
try:
data = my_ risky_operation()
# It's not clear if this next part can also raise an error
process_data(data)
except ValueError:
handle_error()

Clean Way:
try:
data = my_risky_operation()
except ValueError:
handle_error()
else:
# This code only runs if the 'try' block succeeds with NO exception
process_data(data)


#Python #CleanCode #Programming #BestPractices #CodeReadability

━━━━━━━━━━━━━━━
By: @DataScience4
10👍3
Do not violate the Single Responsibility Principle 🎯

A function should do one thing, and do it well.

This function does too much:

def calculate_final_total(
price: float,
quantity: int,
discount_rate: float,
tax_rate: float
) -> float:
# Calculate the subtotal
subtotal = price * quantity

# Apply the discount
discounted_amount = subtotal * (1 - discount_rate)

# Calculate the tax
final_total = discounted_amount * (1 + tax_rate)

return final_total


The problem here is that the calculation of the subtotal, discount, and tax are all combined into one function. Any change to one of these steps can affect the entire calculation.

It's better to break down the logic into smaller, more specialized functions:

def calculate_subtotal(price: float, quantity: int) -> float:
return price * quantity

def apply_discount(subtotal: float, discount: float) -> float:
return subtotal * (1 - discount)

def calculate_tax(amount: float, tax_rate: float) -> float:
return amount * (1 + tax_rate)


This is much better.

Smaller functions with a single task are easier to test with unit tests because they have fewer dependencies and require less mocking.

Furthermore, isolated components are easier to reuse in different parts of the application or pipeline without bringing in unnecessary dependencies.

Therefore, keep your functions simple and focused.

One function – one responsibility. 📝

#Python #Coding #SoftwareDevelopment #CleanCode #Programming #BestPractices

Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
2