Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Course
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM Tools
Machine Learning
Machine Learning & AI
Machine Learning Tools
Manage Data
MLOps
Natural Language Processing
Newsletter Archive
NumPy
Pandas
Polars
PySpark
Python Helpers
Python Tips
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Python Tips

Avoid Side Effects When Using List in a Function

When using a Python list as an argument in a function, you might inadvertently change its value. For example, in the code above, using the append method ends up changing the values of the original list. 

If you want to avoid this side effect, use copy with a list or deepcopy with a nested list in a function. 

My previous tips on good Python practices.

Avoid Side Effects When Using List in a Function Read More »

Dictionary as an Alternative to If-Else

It is common to use the else statement to cover the cases that the if statement doesn’t cover. For example, in the code above, we use the else statement when the item is not on the price list. 

This method works, but we query the dictionary twice and use two statements just to return almost the same thing. We can reduce the redundancy in the code with the get method.

get looks up a key and returns the default value when a key doesn’t exist.

My previous tips on Python dictionary.

Dictionary as an Alternative to If-Else Read More »

Inheritance in Python

Have you ever had multiple classes that have similar attributes and methods? If so, use inheritance to organize your classes.

Inheritance allows us to define a parent class and child classes. A child class inherits all the methods and attributes of the parent class. 

In the code above, we define the parent class to be Dog and the child classes to be Dachshund and Poodle. With class inheritance, we avoid repeating the same piece of code multiple times.

Inheritance in Python Read More »

Post-init: Add Init Method to a Data Class

With a data class, you don’t need an init method to assign values to its attributes. However, sometimes you might want to use an init method to initialize certain attributes.

That is when dataclass’s __post_init__ comes in handy.  

In the code above, I use __post_init__ to initialize the attribute info using the attributes names and ages.

My previous tips on dataclass.

Post-init: Add Init Method to a Data Class Read More »

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran