Module (9%)
Section (64%)

Now it’s time to get familiar with Python core syntax expressions and their corresponding magic methods. The following tables will help you in situations where you'd like to implement a custom method for a Python core operation, as the tables enumerate the most popular operators and functions that own magic method counterparts. Treat it as a list from a universal map, unrelated to any special data type.

Comparison methods

Function or operatorMagic methodImplementation meaning or purpose
==__eq__(self, other)equality operator
!=__ne__(self, other)inequality operator
<__lt__(self, other)less-than operator
>__gt__(self, other)greater-than operator
<=__le__(self, other) less-than-or-equal-to operator
>=__ge__(self, other) greater-than-or-equal-to operator

Numeric methods

Unary operators and functions

Function or operatorMagic methodImplementation meaning or purpose
+__pos__(self)unary positive, like a = +b
-__neg__(self)unary negative, like a = -b
abs()__abs__(self)behavior for abs() function
round(a, b)__round__(self, b)behavior for round() function

Common, binary operators and functions

Function or operatorMagic methodImplementation meaning or purpose
+__add__(self, other)addition operator
-__sub__(self, other)subtraction operator
*__mul__(self, other)multiplication operator
//__floordiv__(self, other)integer division operator
/__div__(self, other)division operator
%__mod__(self, other)modulo operator
**__pow__(self, other)exponential (power) operator

Augumented operators and functions

By augumented assignment we should understand a sequence of unary operators and assignments like a += 20

Function or operatorMagic methodImplementation meaning or purpose
+=__iadd__(self, other)addition and assignment operator
-=__isub__(self, other)subtraction and assignment operator
*=__imul__(self, other)multiplication and assignment operator
//=__ifloordiv__(self, other)integer division and assignment operator
/=__idiv__(self, other)division and assignment operator
%=__imod__(self, other)modulo and assignment operator
**=__ipow__(self, other)exponential (power) and assignment operator