My python_寒假特訓_5_數據類型(資料型態)_type_isinstance的使用



整數 (會直接省去小數點後的內容)





浮點



布林


e記法 (科學符號)




字串






獲得關於類型的資訊


之前 猜數字   輸入部分

要我們   輸入的就是 整數 資訊







python 有提供一個內置函數可以明確的告知我們

變數的輸入類型資訊

可做為檢查

就是  type()   函數






>>> a = '520'
>>> type(a)
<class 'str'>
>>> type(5.2)
<class 'float'>
>>> type(True)
<class 'bool'>
>>> type(3e15)
<class 'float'>
>>>
>>>
>>>
>>> help(type)
Help on class type in module builtins:

class type(object)
 |  type(object_or_name, bases, dict)
 |  type(object) -> the object's type
 |  type(name, bases, dict) -> a new type
 |
 |  Methods defined here:
 |
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |
 |  __dir__(...)
 |      __dir__() -> list
 |      specialized __dir__ implementation for types
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  __instancecheck__(...)
 |      __instancecheck__() -> bool
 |      check if an object is an instance
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  __prepare__(...)
 |      __prepare__() -> dict
 |      used to create the namespace for the class statement
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |
 |  __sizeof__(...)
 |      __sizeof__() -> int
 |      return memory consumption of the type object
 |
 |  __subclasscheck__(...)
 |      __subclasscheck__() -> bool
 |      check if a class is a subclass
 |
 |  __subclasses__(...)
 |      __subclasses__() -> list of immediate subclasses
 |
 |  mro(...)
 |      mro() -> list
 |      return a type's method resolution order
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __abstractmethods__
 |
 |  __dict__
 |
 |  __text_signature__
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  __base__ = <class 'object'>
 |      The most base type
 |
 |  __bases__ = (<class 'object'>,)
 |
 |  __basicsize__ = 432
 |
 |  __dictoffset__ = 132
 |
 |  __flags__ = -2146675712
 |
 |  __itemsize__ = 20
 |
 |  __mro__ = (<class 'type'>, <class 'object'>)
 |
 |  __weakrefoffset__ = 184

>>>


當然  還有一個更好用的

isinstance()

返回 一個 bool





當 參數一 跟 參數二 相同 返回 true

反之    false


留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題