python type函数
阿里云大學(xué)人工智能學(xué)前小測驗-Python測試
4.代碼 print(type([1,2])) 輸出結(jié)果為
A. <class 'list'>
B. <class 'tuple'>
C. <class 'int'>
D. <class 'set'>
?
如何知道一個變量的數(shù)據(jù)類型
基本上入門級最開始使用的就是type 函數(shù),如:
?
1 num1 = 1232 str1 = 'hello'3 noneobj = None4 print(type(num1))5 print(type(str1))6 print(type(noneobj))7 8 <class 'int'>9 <class 'str'> 10 <class 'NoneType'>直接對數(shù)據(jù)對象本身進行type ,如type(123),type('hello'),type(None),結(jié)果是一樣的。
對于一些內(nèi)置變量,比如abs 函數(shù),max 函數(shù), len 函數(shù),也都可以使用type 去獲取他們的對象類型。
1 print(type(abs)) 2 print(type(max)) 3 print(type(len)) 4 5 <class 'builtin_function_or_method'> # 這一類說明是Python 內(nèi)置的函數(shù) 6 <class 'builtin_function_or_method'> 7 <class 'builtin_function_or_method'>補充:python數(shù)據(jù)類型
? ? ? ? ? ? ?字符串 str string “”或 ‘’
????????????? 列表 list []
????????????? 元祖 tuple () 類似于列表,但不能修改元素
????????????? 字典 dict dictionary {} 鍵值對
????????????? 集合 set {} 非鍵值對
? ? ? ? ? ? ? 集合(set)是一個無序的不重復(fù)元素序列。可以使用大括號 { } 或者 set() 函數(shù)創(chuàng)建集合,注意:創(chuàng)建一個空集合必須用 set() 而不是 { },因為 { } 是用來創(chuàng)建一個空字典。
總結(jié)
以上是生活随笔為你收集整理的python type函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python数据类型转换方法列表
- 下一篇: python 重复输出字符串