Javatpoint Logo
Javatpoint Logo

Python bin() Method

Python bin() method is used to get binary value. It takes an integer argument and returns a binary string prefixed with "0b". It returns an error TypeError, if the argument type is not an integer. The signature of the function is given below.

Signature

Parameters

x: It is an integer value.

Return

It returns binary string of an integer value.

Let's see some examples of bin() method to understand it's functionality.

Python bin() Method Example 1

This simple example returns binary value of an integer value 10. See the example below.

Output:

0b1010

Python bin() Method Example 2

If the argument is not an integer, it returns an error. See the example below.

Output:

TypeError: 'float' object cannot be interpreted as an integer

Python bin() Method Example 3

This example compares two binaries whether both are same or not.

Output:

Both are same 0b101 0b101

Next TopicPython Functions




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA