Notes

`bin()` Function in Python [ English ]

< Prev Next >

1. Introduction

The bin() function in Python is a built-in function used to convert an integer number into its binary representation. The binary number is returned as a string prefixed with 0b, which indicates that the number is in base-2 (binary system).

Binary numbers are widely used in computer systems and digital electronics, where numbers are represented using only two digits: 0 and 1.

2. Syntax

bin(number)

Parameter

3. Return Value

4. Basic Example

print(bin(10))

Output

0b1010

Explanation

5. Example with Another Number

print(bin(7))

Output

0b111

Explanation

6. Removing the 0b Prefix

Sometimes we may want only the binary digits without the prefix.

num = 10
binary = bin(num)

print(binary[2:])

Output

1010

Explanation

7. Example with Negative Numbers

print(bin(-8))

Output

-0b1000

Explanation

8. Practical Example

number = 25

binary_value = bin(number)

print("Decimal:", number)
print("Binary:", binary_value)

Output

Decimal: 25
Binary: 0b11001

Explanation

9. Summary

< Prev Next >