python bin函数_bin()函数以及Python中的示例

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函数

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值