Home » Python

bin() function with example in Python

Python bin() function: Here, we are going to learn about the bin() function in Python with example.
Submitted by IncludeHelp, on March 31, 2019

Python bin() function

bin() function is a library function in Python, it is used to get the binary value of a number, it accepts a number and returns an equivalent binary string.

Syntax:

    bin(number)

Parameter: number – a valid number whose value can be converted to the binary.

Return value: It returns string containing binary value of given number.

Example:

    Input:
    num1 = 55   # number (+ve)
    num2 = -55  # number (-ve)

    print(bin(num1))
    print(bin(num2))

    Output:
    0b110111
    -0b110111

Python code to convert number to binary string

# python code to convert number to binary
# an example of bin() function in Python

num1 = 55   # number (+ve)
num2 = -55  # number (-ve)

# converting to binary 
bin_str = bin(num1)
print("Binary value of ", num1, " is = ", bin_str)

bin_str = bin(num2)
print("Binary value of ", num2, " is = ", bin_str)

Output

Binary value of  55  is =  0b110111
Binary value of  -55  is =  -0b110111





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.