• python类型检查和类型转换


    1. 类型检查

       type()用来检查值的类型 (整型、浮点型、布尔值、字符串、空值、列表、元组、集合、字典)
       该函数会将检查的结果作为返回值返回,可以通过变量来接收函数的返回值

      print(type(1))         # <class 'int'>
      print(type(1.5))       # <class 'float'>
      print(type(True))      # <class 'bool'>
      print(type('hello'))   # <class 'str'>
      print(type(None))      # <class 'NoneType'>
      print(type([1,2,3]))   # <class 'list'>
      print(type( (1,2,3) )) # <class 'tuple'>
      print(type( {1,2} )) # <class 'set'>
      print(type({'name':'cat','age':20}) ) # <class 'dict'>
      a
      =type(123) print('123数字类型检查结果返回值:',a) # 123数字类型检查结果返回值: <class 'int'>
    2. 类型转换
       类型转换四个函数 int() float() str() bool()

      a = '123'
      a = int(a)

      int() 可以用来将其他的对象转换为整型
      规则:
        布尔值:True -> 1 False -> 0
             浮点数:直接取整,省略小数点后的内容
             字符串:合法的整数字符串,直接转换为对应的数字
                            如果不是一个合法的整数字符串,
                            则报错 ValueError: invalid literal for int() with base 10: '11.5'  
             对于其他不可转换为整型的对象,直接抛出异常 ValueError

      float() 和 int()基本一致,不同的是它会将对象转换为浮点数

      str() 可以将对象转换为字符串
             True -> 'True'
             False -> 'False'
             123 -> '123'


      bool() 可以将对象转换为布尔值,任何对象都可以转换为布尔值
             规则:对于所有表示空性的对象都会转换为False,其余的转换为True
                        哪些表示的空性:0 、 None 、 ''

  • 相关阅读:
    Numpy
    Homer SIP capture and VoIP Monitoring Install Guide
    How to install FreeSWITCH in Centos 7
    Media Samples
    lavfi
    Jitsi Sip Communicator Source Code Build Guide for Windows
    How to install Asterisk 13 with WebRTC support in CentOS
    How to build PJSIP with WebRTC
    WebRTC & SIP: The Demo
    WebRTC tutorial using SIPML5
  • 原文地址:https://www.cnblogs.com/FlyingLiao/p/11142225.html
Copyright © 2020-2023  润新知