python bin函数
Python bin()函数 (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.
bin()函数是Python中的一个库函数,用于获取数字的二进制值,它接受数字并返回等效的二进制字符串。
Syntax:
句法:
bin(number)
Parameter: number – a valid number whose value can be converted to the binary.
参数: number –一个有效的数字,其值可以转换为二进制。
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代码
# 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
翻译自: https://www.includehelp.com/python/bin-function-with-example.aspx
python bin函数