Python In-Place Bitwise Right-Shift Operator

Python’s in-place bitwise right-shift operator x >>= y calculates the right-shift operation x >> y, and assigns the result to the first operands variable name x. You can set up the in-place right-shift behavior in your own class by overriding the magic “dunder” method __irshift__(self, other) in your class definition. The expression x >>= y … Read more

Python In-Place Bitwise AND Operator

Python’s in-place bitwise AND operator x &= y calcualtes bitwise-and x & y and assigns the result to the first operand x. To set it up for your own class, override the magic “dunder” method __iand__(self, other) in your class definition. The expression x &= y is syntactical sugar for the longer-form x = x … Read more

Python In-Place Exponentiation Operator

Python provides the in-place exponentiation operator x **= y that raises x to the power of y using x ** y and assigns the result to the first operands variable name x. You can set up the in-place exponentiation behavior for your own class by overriding the magic “dunder” method __ipow__(self, other) in your class … Read more

Python In-Place Integer Division Operator

Python’s in-place integer division operator x //= y divides two objects in-place by calculating x // y and assigning the result to the first operands variable name x. Set up in-place integer (or floor) division for your own class by overriding the magic “dunder” method __floordiv__(self, other) in your class definition. The expression x /= … Read more

Python In-Place Modulo Operator

Python provides the operator x %= y to calculate the modulo operation x % y, and assign the result in-place to the first operands variable x. You can set up the in-place modulo behavior for your own class by overriding the magic “dunder” method __imod__(self, other) in your class definition. The expression x %= y … Read more

Python In-Place Division Operator

Python’s in-place division operator x /= y divides two objects in-place by calculating x / y and assigning the result to the first operands variable name x. Set up in-place division for your own class by overriding the magic “dunder” method __truediv__(self, other) in your class definition. The expression x /= y is syntactical sugar … Read more

Python In-Place Multiplication Operator

Python provides the operator x *= y to multiply two objects in-place by calculating the product x * y and assigning the result to the first operands variable name x. You can set up the in-place multiplication behavior for your own class by overriding the magic “dunder” method __imul__(self, other) in your class definition. The … Read more

Python In-Place Subtraction Operator

Python provides the operator x -= y to subtract two objects in-place by calculating the difference x – y and assigning the result to the first operands variable name x. You can set up the in-place subtraction behavior for your own class by overriding the magic “dunder” method __isub__(self, other) in your class definition. The … Read more

Python Count Characters Except Empty Spaces

In Python, a string includes not only the alphanumeric characters and symbols, but also all whitespaces.  Consider this simple example: We have a variable called mystring, and it is assigned to 3 characters a, b, and c.  Note we have separated each character with a space, so when calling the len() function we get the … Read more

Random Forest Classifier with sklearn

Does your model’s prediction accuracy suck but you need to meet the deadline at all costs? Try the quick and dirty β€œmeta-learning” approach called ensemble learning. In this article, you’ll learn about a specific ensemble learning technique called random forests that combines the predictions (or classifications) of multiple machine learning algorithms. In many cases, it … Read more

Python In-Place Addition Operator

Python provides the operator x += y to add two objects in-place by calculating the sum x + y and assigning the result to the first operands variable name x. You can set up the in-place addition behavior for your own class by overriding the magic “dunder” method __iadd__(self, other) in your class definition. The … Read more

How to Generate Random Integers in Python?

The most idiomatic way to create a random integer in Python is the randint() function of the random module. Its two arguments start and end define the range of the generated integers. The return value is a random integer in the interval [start, end] including both interval boundaries. For example, randint(0, 9) returns an integer … Read more