Magic Methods
Method | Purpose | Example Usage |
---|
__init__ | Initializes object attributes during instantiation. | p = Person("Alice", 25) |
__lt__ | Defines behavior for < operator for custom objects. | print(n1 < n2) |
__str__ | Returns a human-readable string representation of an object. | print(p) or str(p) |
- Methods starting and ending with double underscores. They are defined by built-in classes in Python and commonly used for operator overloading
- For more examples, see docs
Count Occurrences of Elements
Throwaway Variable
_
is the throwaway variable. This is a way to indicate to other developers that the loop variable is intentionally not going to be used.