Python __eq__ Magic Method

To customize the behavior of the equality operator x == y, override the __eq__() dunder method in your class definition. Python internally calls x.__eq__(y) to compare two objects using x == y. If the __eq__() method is not defined, Python will use the is operator per default that checks for two arbitrary objects whether they … Read more

Python __div__() Magic Method

The Python __div__() magic method overrides the division operation for a custom object in Python 2. In Python 3, it was replaced by the __truediv__() for a / b and __floordiv__() dunder methods for a // b. The Python __truediv__() method is called to implement the normal division operation / called true division. For example … Read more

The Sieve of Eratosthenes in Python

Finding prime numbers is of critical importance for practical applications such as cryptography. Many public-key methods are only safe from a cryptographic point of view because it’s generally inefficient and slow to compute the prime factors of large numbers. As you go over the article, feel free to watch my explainer video on the Sieve … Read more

Python Input/Output – JSON

Over your career as a Data Scientist, there may be instances where you will work with data to/from a DataFrame to JSON format. This article shows you how to manipulate this data using the above functions. This article covers the commonly used parameters for each function listed above. For a complete list of all parameters … Read more

Python __invert__() Magic Method

Syntax object.__invert__(self) The Python __invert__() method implements the unary arithmetic operation bitwise NOT ~. So, when you cal ~x, Python will internally call x.__invert__() to obtain the inverted object. If the method is not implemented, Python will raise a TypeError. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To … Read more

Python __int__() Magic Method

Syntax object.__int__(x) The Python __int__() method implements the built-in int() function. So, when you call int(x), Python attempts to call x.__int__(). If the return value is not an integer, Python will raise a TypeError. If it’s not implemented, Python attempts to call x.__index__() instead, and only if this is not implemented either, it raises a … Read more

Python Palindromes One-Liner

This one-liner introduces another basic computer science term: palindromes. Similar to anagrams, palindromes are a popular coding interview question. First things first: What is a Palindrome? β€œA palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10201.β€œ [source] … Read more

Python Input/Output – The Clipboard

Over your career as a Data Scientist, there may be instances where you will work with data to/from the system Clipboard. This article shows you how to manipulate this data. Preparation Before any data manipulation can occur, one (1) new library will require installation. The Pandas library enables access to/from a DataFrame. To install this … Read more