The syntax of bin() method is:
The bin() method takes a single parameter:
The bin() method returns the binary string equivalent to the given integer.
If not specified an integer, it raises a TypeError exception highlighting the type cannot be interpreted as an integer.
number = 5
print('The binary equivalent of 5 is:', bin(number))
When you run the program, the output will be:
The binary equivalent of 5 is: 0b101
The prefix 0b represents that the result is a binary string.
apple = 1
orange = 2
grapes = 2
return self.apple + self.orange + self.grapes
print('The binary equivalent of quantity is:', bin(Quantity()))
The binary equivalent of quantity is: 0b101
Here, we've sent an object of class Quantity to the bin() method.
The bin() method doesn't raise an error even if the object Quantity is not an integer.
This is because, we have implemented the __index__() method which returns an integer (sum of fruit quantities). This integer is then supplied to the bin() method.