Python

Floor division in Python

Python Floor Division — A Complete Guide to the // Operator

In Python, the double-backslash operator (//) is the floor division operator. Floor division means dividing and rounding down to the nearest integer. For example: The physical interpretation of the floor division is about sharing quantities evenly. For example, given 7 apples and 3 eaters, how many full apples does each person get? The answer is […]

Python Floor Division — A Complete Guide to the // Operator Read More »

delete a Python object with the del statement

Python del Statement

In Python, you can use the del statement to destroy an object. The syntax is simple: For example, let’s create a variable and delete it: Output: What Can Be Deleted with ‘del’ in Python? In Python, everything is an object. The del statement can be used to delete objects. Thus you can delete almost anything

Python del Statement Read More »

add method in Python

Python __add__() Method

The __add__() method in Python specifies what happens when you call + on two objects. When you call obj1 + obj2, you are essentially calling obj1.__add__(obj2). For example, let’s call + on two int objects: Output: This works, because int implements the __add__() method behind the scenes. To make a custom object support addition with

Python __add__() Method Read More »

The __abs__ method in Python

Python __abs__() Method

The __abs__() method in Python specifies what happens when you call the built-in abs() function on an object. For example, let’s call abs() on a negative number: Output: The result is the absolute value of the negative number, that is, the distance from 0. The important part here is to understand that calling abs(n) is

Python __abs__() Method Read More »