Python bin function

Python bin() function is binary function and one of  Python built-in function. bin() function is used to get the binary value of integer number. binary value is the value in the form of 0 and 1.

Syntax of bin()

bin(number)
  • bin is function name
  • number is any integer value whose binary value need to be calculated.

 

How bin() function works

Let’s see how bin() function works by taking example.

#Python bin() function

num=5
print("binary value of",num,"is:",bin(num))

Output

binary value of 5 is: 0b101

Explanation

  • In above example, we have to calculate the binary value of number 5.
  • so, when we apply bin function over number 5, it gives output 0b101, here 0b is binary indication and 101 is binary value.

How to calculate binary value of number

If you don’t know how to get binary value of any integer without any function, here we will describe.

for calculating binary value, binary value is calculated in 2power  .

  • first place is 2 .
  • 2nd place is 21.
  • 3rd place is 22
  • and so on.

 

python-bin-function

 

 

hence in our example, binary value of 5 is calculated as

22+ 2 = 4+1=5

 

Please follow and like us:
READ  Use of Python hex() function with example

One Comment to “Python bin function”

Comments are closed.