Python type 函数- Python零基础入门教程
目錄
- 一.type 函數(shù)簡介
- type 函數(shù)語法
- 二.type 函數(shù)實戰(zhàn)
- 三.猜你喜歡
零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門
一.type 函數(shù)簡介
Python 變量,也稱 Python 數(shù)據(jù)類型。Python 變量一共六種類型:整數(shù)/浮點數(shù)/字符串/BOOL/列表/元組/字典;
到之前為止,我們已經(jīng)學習完了兩個 Python 內(nèi)置函數(shù),分別是 print 函數(shù)和format 函數(shù),今天需要額外介紹另外一個 Python 內(nèi)置函數(shù) type,該函數(shù)主要用于解析判斷 Python 變量類型;
type 函數(shù)語法
''' 函數(shù)描述:type 函數(shù)用于獲取變量類型; 參數(shù):object : 實例對象; 返回值:直接或者間接類名、基本類型; ''' type(object)二.type 函數(shù)實戰(zhàn)
可能部分小伙伴很懵逼:就簡簡單單賦了一個值,我怎么知道是什么類型?其實類型的判斷,Python 已經(jīng)根據(jù)你賦的值內(nèi)部做了解析判斷,解析的結(jié)果直接通過內(nèi)置函數(shù) type 函數(shù)獲取即可。
什么是內(nèi)置函數(shù)?做一個簡單點的理解,就是 Python 自帶的,就好比人一出生就有兩個眼睛一個嘴巴,直接上代碼演示效果
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:type函數(shù).py @Time:2021/3/17 20:37 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""a = 10 b = 2.5 c = "python教程" d = False print("a的類型是:" ,type(a) ); print("b的類型是:", type(b)); print("c的類型是:", type(c)); print("d的類型是:", type(d));''' 輸出結(jié)果: a的類型是: <class 'int'> b的類型是: <class 'float'> c的類型是: <class 'str'> d的類型是: <class 'bool'> '''由此可見,當你分不清當前變量類型的時候,直接通過 type 函數(shù)便可判斷得出結(jié)果。
在 Python 內(nèi)置函數(shù)中,與 type 函數(shù)相似的還有另外一個內(nèi)置函數(shù) isinstance 函數(shù),兩者區(qū)別在于:
- ** isinstance 函數(shù)會認為子類是一種父類類型,考慮繼承關系。**
- ** type 函數(shù)不會認為子類是一種父類類型,不考慮繼承關系。**
type 函數(shù)和 isinstance 函數(shù)區(qū)別請具體參考:type 函數(shù)和 isinstance 函數(shù) 區(qū)別;
三.猜你喜歡
未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python type 函數(shù)
總結(jié)
以上是生活随笔為你收集整理的Python type 函数- Python零基础入门教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BugkuCTF-Crypto题一段Ba
- 下一篇: websocket python爬虫_p