{"id":24787,"date":"2021-11-02T09:46:46","date_gmt":"2021-11-02T04:16:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=24787"},"modified":"2021-11-05T19:46:24","modified_gmt":"2021-11-05T14:16:24","slug":"python-bin-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-bin-method-with-examples\/","title":{"rendered":"Python bin() Method with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python String startswith() Method with Examples<\/a>
\nBinary Number System:<\/strong> The binary number system is a base two number system. Because computers can only understand binary numbers, the binary system is utilized (0 and 1).<\/p>\n

bin() Method in Python:<\/strong><\/p>\n

The bin() method converts and returns a given integer’s binary equivalent string. If the parameter is not an integer, the __index__() method must be used to return an integer.<\/p>\n

Syntax:<\/strong><\/p>\n

bin(number)<\/pre>\n

Parameters<\/strong><\/p>\n

The bin() method accepts a single parameter:<\/p>\n

number<\/strong> – An integer number for which the binary equivalent must be calculated.
\nIf the value is not an integer, the __index__() method should be used to return an integer.<\/p>\n

Return Value:\u00a0<\/strong><\/p>\n

The binary string equivalent to the given integer is returned by the bin() method.<\/p>\n

If an integer is not specified, a TypeError exception is thrown, indicating that the type cannot be interpreted as an integer.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 2<\/pre>\n

Output:<\/strong><\/p>\n

The given number's{ 2 } binary equiavalent string =  0b10<\/pre>\n

Note:<\/strong><\/p>\n

The prefix 0b denotes that the outcome is a binary string.<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 10<\/pre>\n

Output:<\/strong><\/p>\n

The given number's{ 10 } binary equiavalent string =  0b1010<\/pre>\n

bin() Method with Examples in Python<\/h2>\n