An operator is a symbol in Python syntax that specifies an operation on values or objects. Operators are shorthand: each operation is implemented as a method for the relevant object type. section 33.0 section 33.1
Table 33.1. A few special methods in Python
| Category |
Operator |
Method name |
|---|---|---|
| Mathematical operations | + - * / | __add__ __sub__ __mul__ __truediv__ |
| Comparisons | == < > | __eq__ __lt__ __gt__ |
| Others | print() and str() Create an object—for example, some_object = ClassName() | __str__ __init__ |
Common operator symbols map to special-method names, such as + to __add__ and * to __mul__. section 33.1
Adding fractions
Python cannot add two Fraction objects until the class defines the operation. Implementing __add__ lets the + operator combine their numerators and denominators and return a new Fraction. section 33.1
Operators also help with practical calculations: dividing minutes by 60 gives decimal hours, while the remainder operator (%) provides another way to find leftover minutes. section 6.3






