python 为何要学16进制_python为什么中文显示的都是16进制的?

2013-03-29 回答

int类是将指定进制下的数字转换为十进制数字

你在python命令下输入help(int),会出现下面这段话

class int(object)

| int(x=0) -> int or long

| int(x, base=10) -> int or long

|

| convert a number or string to an integer, or return 0 if no arguments

| are given. if x is floating point, the conversion truncates towards zero.

| if x is outside the integer range, the function returns a long instead.

|

| if x is not a number or if base is given, then x must be a string or

| unicode object representing an integer literal in the given base. the

| literal can be preceded by '+' or '-' and be surrounded by whitespace.

| the base defaults to 10. valid bases are 0 and 2-36. base 0 means to

| interpret the base from the string as an integer literal.

你碰到的问题在可以引用上面这段英文来解释

if x is not a number or if base is given, then x must be a string or

unicode object representing an integer literal in the given base.

翻译过来是如果参数不是数字类型,或者base参数给出了,那么x必须是基于指定进制(默认是十进制)下的数字序列的字符串,所以下面举得例子有的成功有的报错:

int('123')#可以成功,执行,123十进制数字内

int('a123')#报错,因为a不在十进制数内

int('a123',16)#可以成功,因为a123,都是十六进制内的基本数字

int('010101',2)#可以成功,因为0和1都是二进制的基本数字。

楼上的也说了,组成二进制的数字是只有0和1的,你的输入中可是包含了非二进制的数字呢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值