bin() Builtin Function

Python – bin()

Python bin() builtin function is used to get the binary version of given integer value.

In this tutorial, you will learn the syntax of bin() function, and then its usage with the help of example programs.

Syntax

The syntax of bin() function is

bin(x)

where

ParameterDescription
xAn integer.

Examples

1. Convert integer to binary

In the following program, we take an integer value in x and convert it to binary using bin() function.

Python Program

x = 52
output = bin(x)
print(f'x      : {x}')
print(f'bin(x) : {output}')
Run Code Copy

Output

x      : 52
bin(x) : 0b110100

2. Convert negative integer to binary

In the following program, we take a negative integer value, and convert it into binary using bin() function.

Python Program

x = -7
output = bin(x)
print(f'x      : {x}')
print(f'bin(x) : {output}')
Run Code Copy

Output

x      : -7
bin(x) : -0b111

Summary

In this tutorial of Python Examples, we learned the syntax of bin() function, and how to convert a given integer value into binary value using bin() with examples.

Related Tutorials

Code copied to clipboard successfully 👍