Sometimes you need to view an integer in binary form. This is common while learning binary number systems, working with bitwise operations, or understanding how computers store data.
Writing your own conversion logic for every number takes extra work. The Python bin() Function converts an integer into binary with a single function call.
What it is: The bin() function is a built-in Python function that converts an integer into its binary representation. It returns the result as a string that begins with the 0b prefix, which indicates binary notation.
See a quick example to understand how the Python bin() Function works.
You can also explore its real-world use cases to see where this function is commonly used.
Before exploring practical examples and use cases, first learn its syntax, parameters, and return value.
💡 Tip: Use the bin() function whenever you need the binary representation of an integer. To explore more built-in functions, visit the
Python Built-in Functions Learning Guide.
Syntax, Parameters, Return Value and Examples: Python bin() Function
The section below covers the syntax, parameters, return value, and a quick example of the Python bin() Function.
Syntax
bin(number)
Parameters
| Parameter | Description |
|---|---|
number |
The integer that will be converted into binary. Objects that implement the __index__() method are also accepted. |
Return Value
| Return Value | Description |
|---|---|
str |
Returns the binary representation of the given integer as a string with the 0b prefix. |
Quick Example
The following example converts a decimal number into binary.
number = 10
result = bin(number)
print(result)
# Output:
0b1010
The bin() function converts the integer 10 into binary and returns the result as a string. The 0b prefix shows that the value is in binary format.
How the Python bin() Function Works
- The
bin()function accepts an integer as input. - Python converts the integer into its binary representation.
- The converted value is returned as a string.
- The returned string always begins with the
0bprefix. - Negative integers keep the minus sign before the binary value.
- The original integer remains unchanged after the conversion.
Examples: Python bin() Function
The examples below show different ways to use the Python bin() Function.
Example 1: Convert a Decimal Number into Binary
number = 25
print(bin(number))
# Output:
0b11001
Explanation: The bin() function converts the decimal value 25 into its binary representation. The returned string starts with 0b, which identifies it as a binary value.
Example 2: Convert Zero into Binary
print(bin(0))
# Output:
0b0
Explanation: Zero is also converted into binary. Python represents it as 0b0, following the same format used for all other integer values.
Example 3: Convert a Negative Integer
print(bin(-12))
# Output:
-0b1100
Explanation: The bin() function also works with negative integers. Python places the minus sign before the binary representation.
Example 4: Remove the 0b Prefix
number = 13
binary = bin(number)[2:]
print(binary)
# Output:
1101
Explanation: The value returned by bin() is a string. Using string slicing removes the first two characters, leaving only the binary digits.
Example 5: Convert User Input into Binary
number = int(input("Enter a number: "))
print(bin(number))
# Sample Output:
Enter a number: 18
0b10010
Explanation: The input() function returns a string, so int() converts it into an integer first. The bin() function then converts that integer into binary.
Example 6: Convert Multiple Numbers into Binary
numbers = [3, 7, 12, 20]
for number in numbers:
print(number, "->", bin(number))
# Output:
3 -> 0b11
7 -> 0b111
12 -> 0b1100
20 -> 0b10100
Explanation: The bin() function can be used inside a loop to convert several integers one after another. Each value is converted independently.
Example 7: Handle an Invalid Argument
try:
print(bin(5.5))
except TypeError:
print("bin() requires an integer value")
# Output:
bin() requires an integer value
Explanation: The bin() function accepts only integers or objects that implement the __index__() method. Passing a floating-point value raises a TypeError.
Use Cases: When to use the bin() Function
The Python bin() Function is useful in situations like these:
- Converting decimal numbers into binary format.
- Learning binary number systems.
- Understanding bitwise operations.
- Displaying binary values while debugging.
- Working with low-level programming concepts.
- Teaching number system conversions.
Key Takeaways: bin() Function
Here are the main points to remember about the Python bin() Function:
- The
bin()function converts an integer into binary. - It returns the result as a string.
- The returned string always begins with the
0bprefix. - It supports both positive and negative integers.
- It also accepts objects that implement the
__index__()method. - Passing an unsupported data type raises a
TypeError.
The bin() function provides a quick way to convert integers into binary without writing your own conversion logic. It is useful for learning binary numbers, working with bitwise operations, and checking binary values while debugging programs.