内置函数_类型转换与类型判断

类型转换与类型判断

  • 内置函数bin()、oct()、hex()用来将整数转换为二进制、八进制和十六进制

    >>> bin(555)
    '0b1000101011'
    >>> oct(555)
    '0o1053'
    >>> hex(555)
    '0x22b'
  • 内置函数int()用来将其他形式的数字转换为整数,参数可以为整数、实数、分数或合法的数字字符串,当参数为数字字符串时,还允许指定第二个参数base用来说明数字字符串的进制。其中base取值应为0或2~36之间的整数,其中0表示按数字字符串隐含的进制进行转换

    >>> bin(555)
    '0b1000101011'
    >>> oct(555)
    '0o1053'
    >>> hex(555)
    '0x22b'
    >>> int(-3.2)
    -3
    >>> from fractions import Fraction,Decimal
    >>> x = Fraction(7,3)
    >>> int(x)
    2
    >>> x = Decimal(10/3)
    >>> x
    Decimal('3.333333333333333481363069950020872056484222412109375')
    >>> int(x)
    3
    >>> int('0x153',16)
    339
    >>> int('0x153',0)
    339
    >>> int('153',16)
    339
    >>> int('153')
    153
  • 内置函数float()用来将其它类型转换为实数,complex()可以用来生成复数

    >>> float(3)
    3.0
    >>> float('3.5')
    3.5
    >>> float('inf')  # 无穷大,inf不区分大小写
    inf
    >>> complex(3)
    (3+0j)
    >>> complex(3,5)
    (3+5j)
    >>> complex('inf')
    (inf+0j)
    >>> complex('nan')  # 非数字
    (nan+0j)
    >>> float('nan')
    nan
    >>> nan = float('nan')
    >>> nan == float('nan') # 非数字无法比较大小
    False
    >>> nan >= float('nan')
    False
    >>> nan <= float('nan')
    False
  • ord()和chr()时一对功能相反的函数,ord()用来返回单个字符的Unicode码,而chr()则用来返回Unicode编码对应的字符,str()则直接将其任意类型参数转化为字符串

    >>> ord('a')
    97
    >>> chr(65)
    'A'
    >>> chr(ord('A') + 1)
    'B'
    >>> chr(ord('国') + 1)
    '图'
    >>> ord('董')
    33891
    >>> ord('付')
    20184
    >>> ord('国')
    22269
    >>> ''.join(map(chr,(33891,20184,22269)))
    '董付国'
    >>> str(1234)
    '1234'
    >>> str([1,2,3])
    '[1, 2, 3]'
    >>> str((1,2,3))
    '(1, 2, 3)'
    >>> str({1,2,3})
    '{1, 2, 3}'
  • 内置函数ascii()可以把对象转换为ASCII码表示形式,必要的时候使用转义字符来表示特定的字符

    >>> ascii('a')
    "'a'"
    >>> ascii('董付国')
    "'\\u8463\\u4ed8\\u56fd'"
    >>> eval(_)
    '董付国'
    >>> eval("'\\u8463\\u4ed8\\u56fd'")
    '董付国'
  • 内置函数bytes()用来生成字节串,或者把指定对象转换为特定编码的字节串

    >>> bytes()
    b''
    >>> bytes(3)
    b'\x00\x00\x00'
    >>> bytes(5)
    b'\x00\x00\x00\x00\x00'
    >>> bytes(8)
    b'\x00\x00\x00\x00\x00\x00\x00\x00'
    >>> bytes('董付国','utf-8')
    b'\xe8\x91\xa3\xe4\xbb\x98\xe5\x9b\xbd'
    >>> bytes('董付国','gbk')
    b'\xb6\xad\xb8\xb6\xb9\xfa'
    >>> str(_,'gbk')
    '董付国'
    >>> str(b'\xb6\xad\xb8\xb6\xb9\xfa','gbk')
    '董付国'
    >>> '董付国'.encode('gbk')
    b'\xb6\xad\xb8\xb6\xb9\xfa'
    >>> _.decode('gbk')
    '董付国'
    >>> x = '董付国'.encode('gbk')
    >>> list(x)
    [182, 173, 184, 182, 185, 250]
    >>> bytes(_)
    b'\xb6\xad\xb8\xb6\xb9\xfa'
    >>> _.decode('gbk')
    '董付国'
  • list()、tuple()、dict()、set()、frozenset()用来把其他类型的数据转换为列表、元组、字典、可变集合和不可变集合或者创建空列表、空元组、空字典和空集合

    >>> list(range(5))  # 把range对象转换为列表
    [0, 1, 2, 3, 4]
    >>> range(5)
    range(0, 5)
    >>> tuple(_)
    (0, 1, 2, 3, 4)
    >>> dict(zip('1234','abcde')) # 创建字典
    {'1': 'a', '2': 'b', '3': 'c', '4': 'd'}
    >>> dict(zip('1234','abc'))
    {'1': 'a', '2': 'b', '3': 'c'}
    >>> set('1111222334486')       # 创建可变集合,自动去除重复
    {'8', '4', '2', '6', '1', '3'}
    >>> _.add(5)
    >>> _
    {'8', '4', '2', 5, '6', '1', '3'}
    >>> frozenset('1111222334486')  # 创建不可变集合,自动去除重复
    frozenset({'8', '4', '2', '6', '1', '3'})
    >>> _.add(5)
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    AttributeError: 'frozenset' object has no attribute 'add'
  • 内置函数type()和isinstance()可以用来判断数据类型,常用来对函数参数进行检查

    >>> type(3)
    <class 'int'>
    >>> type([3])
    <class 'list'>
    >>> type({3}) in (list,tuple,dict,set)
    True
    >>> type({3}) in (list,tuple,dict)
    False
    >>> isinstance(3,int)
    True
    >>> isinstance(3j,int)
    False
    >>> isinstance(3j,(int,float,complex))
    True

猜你喜欢

转载自www.cnblogs.com/zxbdboke/p/10468349.html