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

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

What Are Differences Between type() and isinstance()?

The main difference between type() and isinstance() is that type(object) returns the type of an object and isinstance(object, class) returns True if the object argument is an instance of the class argument or in a direct or indirect subclass relationship. To strengthen your understanding, let’s quickly recap the syntactical definitions of both functions: type(object) – … Read more

SVM sklearn: Python Support Vector Machines Made Simple

Support Vector Machines (SVM) have gained huge popularity in recent years. The reason is their robust classification performance – even in high-dimensional spaces: SVMs even work if there are more dimensions (features) than data items. This is unusual for classification algorithms because of the curse of dimensionality – with increasing dimensionality, data becomes extremely sparse … Read more

How to Fill a Python String with Spaces?

Problem Formulation Given a Python string s with length k<=n. How to fill the string with n-k empty spaces on the left so that the new string has length n? Here are some examples: INPUT: ‘a’, n = 2 OUTPUT: ‘a ‘ INPUT: ‘hi’, n = 4 OUTPUT: ‘hi ‘ INPUT: ‘hi’, n = 2 … Read more

How to Kill a While Loop with a Keystroke in Python?

To end a while loop prematurely in Python, press CTRL-C while your program is stuck in the loop. This will raise a KeyboardInterrupt error that terminates the whole program. To avoid termination, enclose the while loop in a try/except block and catch the KeyboardInterrupt. You can see the idea in the following code snippet: The … Read more