一、python已知的數(shù)據(jù)類型
1、標(biāo)準(zhǔn)數(shù)據(jù)類型
(1)Python3 中有六個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)類型:
- Number(數(shù)字),判斷標(biāo)志: int、float、bool、complex
- String(字符串),判斷標(biāo)志: str
- List(列表),判斷標(biāo)志: list
- Tuple(元組),判斷標(biāo)志:?tuple
- Set(集合),判斷標(biāo)志: set
- Dictionary(字典),判斷標(biāo)志: dict
(2)在Python3 的這六個(gè)標(biāo)準(zhǔn)數(shù)據(jù)類型中:
- 不可變數(shù)據(jù)(3 個(gè)):Number(數(shù)字)、String(字符串)、Tuple(元組);
- 可變數(shù)據(jù)(3 個(gè)):List(列表)、Dictionary(字典)、Set(集合)。
2、其他數(shù)據(jù)類型
- range,判斷標(biāo)志: range
- frozenset,判斷標(biāo)志:?frozenset
- bytes ,判斷標(biāo)志:?bytes
- bytearray ,判斷標(biāo)志:?bytearray
- memoryvie , 判斷標(biāo)志:?memoryview
二、判斷標(biāo)志的使用
1、直接判斷數(shù)據(jù)類型
(1)運(yùn)行代碼:
isinstance(5, int)
isinstance(5.5, float)
isinstance(False, bool)
isinstance(1+2j, complex)
isinstance("str", str)
isinstance([1,2,3,4,5], list)
isinstance((1,2,3,4,5), tuple)
isinstance({1,2,3,4,5}, set)
isinstance({"1":1}, dict)
isinstance(range(5), range)
isinstance(frozenset({1,2,3,4,5}), frozenset)
isinstance(b"bit", bytes)
isinstance(bytearray(5), bytearray)
isinstance(memoryview(bytes(5)), memoryview)
(2)運(yùn)行結(jié)果:
isinstance(5, int)
True
isinstance(5.5, float)
True
isinstance(False, bool)
True
isinstance(1+2j, complex)
True
isinstance("str", str)
True
isinstance([1,2,3,4,5], list)
True
isinstance((1,2,3,4,5), tuple)
True
isinstance({1,2,3,4,5}, set)
True
isinstance({"1":1}, dict)
True
isinstance(range(5), range)
True
isinstance(frozenset({1,2,3,4,5}), frozenset)
True
isinstance(b"bit", bytes)
True
isinstance(bytearray(5), bytearray)
True
isinstance(memoryview(bytes(5)), memoryview)
True
2、傳參時(shí)的使用
(1)樣式一:形參不賦值
def demo(name_int: int,
name_float: float,
name_bool: bool,
name_complex: complex,
name_str: str,
name_list: list,
name_tuple: tuple,
name_set: set,
name_dict: dict,
name_range: range,
name_frozenset: frozenset,
name_bytes: bytes,
name_bytearray: bytearray,
name_memoryview: memoryview):
pass
demo(name_int=1,
name_float=2,
name_bool=3,
name_complex=4,
name_str=5,
name_list=6,
name_tuple=7,
name_set=8,
name_dict=9,
name_range=10,
name_frozenset=11,
name_bytes=12,
name_bytearray=13,
name_memoryview=14)
(2)樣式二:傳參賦值
備注:引號(hào)可加可不加,如果隨便寫其他字符串,數(shù)據(jù)類型將是Any,如果后面賦值,將以值的類型為準(zhǔn)。
def demo(name_int: "int"=5,
name_float: "float"=5,
name_bool: "bool"=False,
name_complex: "complex"=1+2j,
name_str: "str"="str",
name_list: "list"=[1,2,3,4,5],
name_tuple: "tuple"=(1,2,3,4,5),
name_set: "set"={1,2,3,4,5},
name_dict: "dict"={"1":1},
name_range: "range"=range(5),
name_frozenset: "frozenset"=frozenset({1,2,3,4,5}),
name_bytes: "bytes"=b"bit",
name_bytearray: "bytearray"=bytearray(5),
name_memoryview: "memoryview"=memoryview(bytes(5))):
print(name_memoryview)
print(name_bytearray)
print(name_bytes)
print(name_frozenset)
print(name_range)
print(name_dict)
print(name_set)
print(name_tuple)
print(name_list)
print(name_str)
print(name_complex)
print(name_bool)
print(name_float)
print(name_int)
demo()
(3)樣式三:函數(shù)返回類型
def nice(num:int) -> int:
print(num)
return num
print(nice(100))
如上述函數(shù),num:int提示傳入整形,而-> int 提示返回整形。
?三、數(shù)據(jù)類型的區(qū)別
1、frozenset 與 set 的區(qū)別
都是字典,不過,set無序排序且不重復(fù),是可變的,有add()、remove()等方法,而frozenset是不可變的。
set({1,2,3,4,5}) # {1,2,3,4,5}
frozenset({1, 2, 3, 4, 5}) # frozenset({1, 2, 3, 4, 5})
2、bytes 與?bytearray 的區(qū)別
都是字節(jié),不過,bytes是不可變序列,bytearray是可變序列,可以原處修改字節(jié)。
?bytearray(5)? #??bytearray(b'x00x00x00x00x00')
bytes(5)? #?b'x00x00x00x00x00'
3、list 與 range 的區(qū)別
list是列表,可包含各種數(shù)據(jù)類型,而range對(duì)象返回的是一系列連續(xù)增加的整數(shù),是特殊的列表。
4、memoryview的介紹
memoryview()?函數(shù)返回給定參數(shù)的內(nèi)存查看對(duì)象(memory view)。所謂內(nèi)存查看對(duì)象,是指對(duì)支持緩沖區(qū)協(xié)議的數(shù)據(jù)進(jìn)行包裝,在不需要復(fù)制對(duì)象基礎(chǔ)上允許Python代碼訪問。
>>>v = memoryview(bytearray("abcefg", 'utf-8'))
>>> print(v[1])
98
>>> print(v[-1])
103
>>> print(v[1:4])
<memory at 0x10f543a08>
>>> print(v[1:4].tobytes())
b'bce'
>>>