最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【问题解答】Python中非十进制数做参数会自动转换成十进制么?

Python crifan 3925浏览 0评论

问题:

Python中非十进制数做参数会自动转换成十进制么?-CSDN论坛

比如chr()函数,文档中说参数i应该是integer,但是输入0o47055、0x4e2d、0b100111000101101都可以得到字符“中”。
还有就是网上写的2进制、8进制、16进制互转,用内置函数的话都是用int()中转成10进制,再用bin()、oct()、hex()这几个内置函数转成相应的进制。但是我发现bin()可以直接输入8进制和16进制数,返回2进制数。
这是因为非十进制数做参数会自动转换成十进制的原因么?

回答:

其实是2个问题:

【问题1】比如chr()函数,文档中说参数i应该是integer,但是输入0o47055、0x4e2d、0b100111000101101都可以得到字符“中”。

回答:

首先要明白:不同进制之间的换算关系:

10进制 20013

等于 8进制 47055

等于 16进制 4e2d

等于 2进制 100111000101101

其次要明白:

很多编程语言,比如C,此处的Python等,对于数字的写法:

支持同一个数字,用不同的进制表示

即:

对于

  • 10进制的:20013

也可以写成:

  • 8进制:0o47055
  • 16进制:0x4e2d
  • 2进制:0b100111000101101

 

从代码角度来说,没有任何区别。

即:

chr(20013)
chr(0o47055)
chr(0x4e2d)
chr(0b100111000101101)

是一样的,没有区别的。

-》所以可以回复你的问题了:

这是因为非十进制数做参数会自动转换成十进制的原因么?

答:不是,因为本身输入的Integer的数字就是一样的,chr()函数并没有,也不需要做任何的转换。

顺带给出:

(1)不同进制的数字之间如何转换

可以利用网络,比如

google中搜:

进制转换

就可以找到:在线的,免费的,帮你在不同进制之间换算的网站,比如:

进制转换 – 在线工具

比如前面的,输入8进制的:47055

即可换算出 其他的进制,如图:

(2)关于python中chr

google搜:

chr python

可以找到:

Python chr() 函数 | 菜鸟教程

Built-in Functions — Python 3.8.1 documentation

“chr(i)

Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string ‘a’, while chr(8364) returns the string ‘€’. This is the inverse of ord().

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16).  ValueErrorwill be raised if i is outside that range.”

内置函数 — Python 3.7.6 文档

“chr(i)

返回 Unicode 码位为整数 i 的字符的字符串格式。例如,chr(97) 返回字符串 ‘a’,chr(8364) 返回字符串 ‘€’。这是 ord() 的逆函数。

实参的合法范围是 0 到 1,114,111(16 进制表示是 0x10FFFF)。如果 i 超过这个范围,会触发 ValueError 异常。”

即:

chr(),输入的参数是:Unicode的码位,也叫编码值,标准叫法是:unicode code point

(3)此处提到的:中国的”中”字的Unicode的码位

google 搜:

unicode 20013

unicode code point

可以找到在线的,查询Unicode的code point的地方:

Unicode codepoint lookup/search tool

搜索:中

可以找到:

可以看到是:

Character: 中          (U+4E2D)

即表示:

中 这个中文汉字的Unicode的code point=码位是(16进制的)4E2D =10进制的20013

引申:

如果想要查询其他汉字(或字符),也可以自己去查询

比如

Mac中搜狗输入法中也可以输入的微笑的表情图标:😊

如图:

然后复制粘贴过去,回车后,即可查询到:

对应Unicode的code point是16进制的:1F60A

 

【问题2】还有就是网上写的2进制、8进制、16进制互转,用内置函数的话都是用int()中转成10进制,再用bin()、oct()、hex()这几个内置函数转成相应的进制。但是我发现bin()可以直接输入8进制和16进制数,返回2进制数

回答:

不要轻信网上说的,或者你自己要真正看懂别人的代码,才能真正学会知识。

而如何真正看懂。此处举例说明:

自己找到Python的int()函数的定义,看看是怎么解释的。看懂后,再去使用。

google搜:

python int()

找到:

Built-in Functions — Python 3.8.1 documentation

class int(x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x defines __int__(), int(x) returns x.__int__(). If x defines __index__(), it returns x.__index__(). If x defines __trunc__(), it returns x.__trunc__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or – (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int(‘010’, 0) is not legal, while int(‘010’) is, as well as int(‘010’, 8).

The integer type is described in Numeric Types — int, float, complex.

Changed in version 3.4: If base is not an instance of int and the base object has abase.__index__ method, that method is called to obtain an integer for the base. Previous versions used base.__int__ instead of base.__index__.

Changed in version 3.6: Grouping digits with underscores as in code literals is allowed.

Changed in version 3.7: x is now a positional-only parameter.

Changed in version 3.8: Falls back to __index__() if __int__() is not defined.

如果不熟悉英文,那么可以切换成简体中文:

-》

内置函数 — Python 3.8.1 文档

https://docs.python.org/zh-cn/3/library/functions.html#int

“class int([x])

class int(x, base=10)

返回一个基于数字或字符串 x 构造的整数对象,或者在未给出参数时返回 0。 如果 x 定义了 __int__(),int(x) 将返回 x.__int__()。 如果 x 定义了 __index__(),它将返回 x.__index__()。 如果 x 定义了 __trunc__(),它将返回 x.__trunc__()。 对于浮点数,它将向零舍入。

如果 x 不是数字,或者有 base 参数,x 必须是字符串、bytes、表示进制为 base 的 整数字面值 的 bytearray 实例。该文字前可以有 + 或 – (中间不能有空格),前后可以有空格。一个进制为 n 的数字包含 0 到 n-1 的数,其中 a 到 z (或 A 到 Z )表示 10 到 35。默认的 base 为 10 ,允许的进制有 0、2-36。2、8、16 进制的数字可以在代码中用 0b/0B 、 0o/0O 、 0x/0X 前缀来表示。进制为 0 将安照代码的字面量来精确解释,最后的结果会是 2、8、10、16 进制中的一个。所以 int(‘010’, 0) 是非法的,但 int(‘010’) 和 int(‘010’, 8) 是合法的。

整数类型定义请参阅 数字类型 — int, float, complex 。

在 3.4 版更改: 如果 base 不是 int 的实例,但 base 对象有 base.__index__ 方法,则会调用该方法来获取进制数。以前的版本使用 base.__int__ 而不是 base.__index__。

在 3.6 版更改: 您可以使用下划线将代码文字中的数字进行分组。

在 3.7 版更改: x 现在只能作为位置参数。

在 3.8 版更改: 如果 __int__() 未定义则回退至 __index__()。”

你所说的:内置函数的话都是用int()中转成10进制

用代码写出来是:

# Function: demo for 
#     Python中非十进制数做参数会自动转换成十进制么?-CSDN论坛
#     https://bbs.csdn.net/topics/395822961
# Author: Crifan Li
# Update: 20200202

intBase10 = 20013
print("intBase10=%s" % intBase10)

intBase10Str = "20013"
convertedInt10FromBase10 = int(intBase10Str)
print("convertedInt10FromBase10=%s" % convertedInt10FromBase10)

intBase2Str = "0b100111000101101"
convertedInt10FromBase2 = int(intBase2Str, base=2)
print("convertedInt10FromBase2=%s" % convertedInt10FromBase2)

intBase8Str = "0o47055"
convertedInt10FromBase8 = int(intBase8Str, base=8)
print("convertedInt10FromBase8=%s" % convertedInt10FromBase8)

intBase16Str = "0x4e2d"
convertedInt10FromBase16 = int(intBase16Str, base=16)
print("convertedInt10FromBase16=%s" % convertedInt10FromBase16)

# intBase10=20013
# convertedInt10FromBase10=20013
# convertedInt10FromBase2=20013
# convertedInt10FromBase8=20013
# convertedInt10FromBase16=20013

这下你容易看懂了吧?

而对于另外的:

  • 2进制:bin()
    • 将一个整数转变为一个前缀为“0b”的二进制字符串。结果是一个合法的 Python 表达式。如果 x 不是 Python 的 int 对象,那它需要定义 __index__() 方法返回一个整数。一些例子:
    • >>> bin(3)
    • ‘0b11’
    • >>> bin(-10)
    • ‘-0b1010’
  • 8进制:oct()
    • oct(x)
    • 将一个整数转变为一个前缀为“0o”的八进制字符串。结果是一个合法的 Python 表达式。如果 x 不是 Python 的 int 对象,那它需要定义 __index__() 方法返回一个整数。一些例子:
    • >>> oct(8)
    • ‘0o10’
    • >>> oct(-56)
    • ‘-0o70’
  • 16进制:
    • hex()

很明显,对于:bin(),oct(),hex()

  • 输入:整数 Integer=int
    • 比如 3,8,255之类的
  • 输出:字符串 string=str
    • 对应的该种进制的写法,比如:’0b11’,’0o10’,’0xff’

对应代码写出来是:

binStr = bin(intBase10)
print("binStr=%s" % binStr)

octStr = oct(intBase10)
print("octStr=%s" % octStr)

hexStr = hex(intBase10)
print("hexStr=%s" % hexStr)

# binStr=0b100111000101101
# octStr=0o47055
# hexStr=0x4e2d

至此,算是回答完毕你的问题了。

总结:

其实,此处都是基本的概念和逻辑和函数,只是你不够熟悉罢了,所以需要以后补足相关知识。

再次整理如下:

  • Python中的基本的内置函数:int(), bin(), oct(), hex()
    • 官网文档
      • 英文:
    • 不同进制的数字的表达方式,写法
      • 比如:20013 = 0b100111000101101 = 0o47055 = 0x4e2d
    • Unicode的code point
      • 属于:字符编码
        • 不熟悉的可参考我的教程:
          • 详解:

如此都有所了解后,加上自己多动手写写代码,测试测试,自然就会化解心中疑惑。

转载请注明:在路上 » 【问题解答】Python中非十进制数做参数会自动转换成十进制么?

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.175 seconds, using 22.13MB memory