Python __le__() Magic Method

Short summary: To customize the behavior of the less than or equal to operator x <= y, override the __le__() dunder method in your class definition. Python internally calls x.__le__(y) to obtain a return value when comparing two objects using x <= y. The return value can be any data type because any value can … Read more

Python __lt__() Magic Method

Short summary: To customize the behavior of the less than operator x < y, override the __lt__() dunder method in your class definition. Python internally calls x.__lt__(y) to obtain a return value when comparing two objects using x < y. The return value can be any data type because any value can automatically converted to … Read more

Python __gt__() Magic Method

Short summary: To customize the behavior of the greather than operator x > y, override the __gt__() dunder method in your class definition. Python internally calls x.__gt__(y) to obtain a return value when comparing two objects using x > y. The return value can be any data type because any value can automatically converted to … Read more

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 __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

Finxter.com Puzzles by Topic

Arithmetic Arithmetic Operations https://app.finxter.com/learn/computer/science/314 Integer Arithmetic https://app.finxter.com/learn/computer/science/381 Python Float Arithmetic https://app.finxter.com/learn/computer/science/320 OOP Basics of OOP https://app.finxter.com/learn/computer/science/527 OOP https://app.finxter.com/learn/computer/science/606 OOP – Representation https://app.finxter.com/learn/computer/science/594 Filter Filter and Recursion https://app.finxter.com/learn/computer/science/493 Set, Lambda, and Filter https://app.finxter.com/learn/computer/science/399 The Filter Function https://app.finxter.com/learn/computer/science/375 DataFrames DataFrames and Datatypes https://app.finxter.com/learn/computer/science/614 DataFrames and Series https://app.finxter.com/learn/computer/science/612 Merging DataFrames https://app.finxter.com/learn/computer/science/621 Add Column To DataFrame https://app.finxter.com/learn/computer/science/616 DataFrame Creation … Read more