美国云服务器的python中bin函数的用法:bin函数主要是用来返回一个整形int或者一个长整形long int的二进制表示,相当于是用来获取数字的二进制值;bin函数的语法格式:“bin(x)”,这里x指的是int或者long int数字,并且必须为整数。
具体实例分析:
实例1
print(bin(3))
print(type(bin(3)))
print(bin(-10))
print(type(bin(-10)))
运行以上代码返回的结果为:
0b11
<class ‘str’>
-0b1010
<class ‘str’>
实例2
将数字转换为二进制字符串的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)
输出结果为:
Binary value of 55 is = 0b110111
Binary value of -55 is = -0b11011