python的六種資料型別
python中資料型別分為不可變資料型別和可變資料型別
可變資料型別
可變資料型別包括:List(串列)、Dictionary(字典)、Set(集合)
不可變資料型別
不可編資料型別包括:Number(數字)、String(字串)、Tuple(元組)
不可變資料型別與可變資料型別的區別
- 不可變資料型別
- 不可變資料型別賦值后會創建一個新的記憶體地址空間
- 并讓賦值資料指向這一新創建的地址,故兩個變數的記憶體地址并不相同
- 重新賦值的資料,系結到了新的記憶體地址,而不是修改之前的記憶體地址
#不可變資料型別
str1 = "ascdfg"
str2 = str1
print(str2)
print("----------------------------")
str1 = "ooooooopo"
print(id(str1))
print(id(str2))
--------------------------------------------
ascdfg
----------------------------
2278845527984
2278844137392
> 運行結果顯示:記憶體地址不同
-
可變資料型別
-
可變資料型別賦值后不會創建新的記憶體地址空間
-
賦值資料與被賦值資料還是同時指向同一塊地址
-
#可變資料型別
list1 = [1,2,3]
list2 = list1 #將list1的值賦給list2
print("改變前")
print(list2) #輸出list2
print("----------------------------")
list1[0] = 2 #將list1的第一個元素賦值為2
print("改變后")
print("list1:\t",id(list1))
print("list2:\t",id(list2))
運行結果顯示:記憶體地址相同
Nmuber(數字)---不可變資料型別
- 整型(int)-通常被稱為是整型或整數,是正或負整數,不帶小數點,在python3中不限制大小,可以當作Long型別使用,布爾(bool)是整型的子型別,
- 浮點型(float)-浮點型由整數部分與小數部分組成,浮點型也可以用科學計數法表示,
- 復數(conplex)-復數由實數部分和虛數部分構成,可以用a+bi,或者complex(a,b)表示,復數的實部a和虛部b都是浮點型,
String(字串)---不可變資料型別
- 字串用單引號或雙引號括起來,+號是字串的連接符,星號*表示復制當前字串,與之結合的數字為復制的次數,
- python中的字串有兩種索引方式,從左往右以0開始,從右往左以-1開始
- python中的字串不能該改變
List(串列)---可變資料型別
- 串列中元素型別可以不相同,支持數字,字串甚至可以包含串列(所謂嵌套)
- 和字串一樣,串列同樣可以被索引和截取,串列被截取后回傳一個包含所需元素的新串列
#串列截取
t = [1,5,6,7,2,9]
a = t[2:5]
b = t[2:]
print(a)
print(b)
---------------------------------------------------
[6, 7, 2]
[6, 7, 2, 9]
[2:5]是左閉右開的一個區間,故截取的是[6,2,7],而[2:]譯為截取從編號2開始到最后一個
串列函式及方法
append()方法添加串列項,并對資料項進行修改或更新
#append()方法添加串列項
list1 = ['aaaa','qwe','zxc']
list2 = list1
print("第三個元素為",list1[2])
list1.append('cccccc')
print(list2)
---------------------------------------------------
第三個元素為 zxc
['aaaa', 'qwe', 'zxc', 'cccccc']
del陳述句洗掉串列元素
#使用del陳述句進行洗掉串列項
list1 = ['asd','zxc','qwe']
del list1[0]
print(list1)
---------------------------------------------------
['zxc', 'qwe']
len(list)函式查詢串列元素個數
list1 = [1,2,5,6,8,2,4,2]
print(len(list1))
---------------------------------------------------
8
max(list)函式回傳串列元素的最大值
list1 = [1,2,7,8,9,2,4,2]
print(max(list1))
---------------------------------------------------
9
min(list)函式回傳串列元素的最小值
list1 = [1,2,7,8,9,2,4,2]
print(min(list1))
---------------------------------------------------
1
list(seq)函式將元組轉換為串列
tup = ('asx','apple',1,5,5)
print("tup的資料型別為",type(tup))
list1 = list(tup) #將tup元組轉換為串列并賦值給list1
print(list1)
print("list1的資料型別為",type(list1))
---------------------------------------------------
tup的資料型別為 <class 'tuple'>
['asx', 'apple', 1, 5, 5]
list1的資料型別為 <class 'list'>
list.count(obj)函式統計某個元素在串列中出現的次數
#count()方法統計某個元素在串列中出現的次數
list1 = [1,2,5,6,8,2,4,2]
list2 = list1.count(2)
print(list2)
---------------------------------------------------
3
list.extend(seq)函式在串列末尾一次性追加另一個序列中的多個值(用新串列擴展原來的串列)
#extend()方法進行擴展串列
list1 = ['apple','qaz','wsx']
list2 = list(range(0,5)) #range()創建0-4的串列
list1.extend(list2)
print(list1)
---------------------------------------------------
['apple', 'qaz', 'wsx', 0, 1, 2, 3, 4]
list.index(obj)從串列中找出某個值第一個匹配項的索引位置
#index()從串列找出某一個值第一個匹配項的索引位置
list1 = [1,5,3,6,9,8]
print(list1.index(3))
---------------------------------------------------
2
list.insert(index,obj)將物件插入串列
#insert()將物件插入串列
list1 = ['apple','qaz','wsx']
list1.insert(1,'qwe') #insert(索引位置,要插入的物件)
print(list1)
---------------------------------------------------
['apple', 'qwe', 'qaz', 'wsx']
list.pop([index=-1])移除串列中的一個元素(默認最后一個元素),并且回傳該元素的值
#pop()移除串列的一個元素(默認最后一個元素),并回傳該元素的值
list1 = ['apple','qaz','wsx']
list_pop = list1.pop(1)
print("洗掉項為",list_pop)
print("串列現在為",list1)
---------------------------------------------------
洗掉項為 qaz
串列現在為 ['apple', 'wsx']
list.remove(obj)移除串列中某個值的第一個匹配項
#remove()移除串列中某個值的第一個匹配項
list1 = ['qsd','apple','wsx','apple']
list_remove = list1.remove('apple')
print(list1)
---------------------------------------------------
['qsd', 'wsx', 'apple']
list.reverse()反向串列中的元素
#reverse()反向串列中的元素
list1 = ['apple','qaz','wsx']
list_re = list1.reverse()
print(list1)
---------------------------------------------------
['wsx', 'qaz', 'apple']
list.sort(key=None,reverse=False)對串列進行排序
#sort()對原串列進行排序
list1 = [1,5,3,6,9,8]
list0 = [1,6,4,2,3,8,1,9]
list2 = list0.sort(reverse=True) #reverse=true 降序
list3 = list1.sort(reverse=False) #reverse=falses 升序
print("降序",list0)
print("升序",list1)
---------------------------------------------------
降序 [9, 8, 6, 4, 3, 2, 1, 1]
升序 [1, 3, 5, 6, 8, 9]
list.clear()清空串列
#clear()清空串列
list1 = [1,5,3,6,9,8]
list1.clear()
print(list1)
---------------------------------------------------
[]
list.copy()復制串列
#copy()復制串列
list1 = [1,5,3,6,9,8]
list2 = list1.copy()
print(list2)
---------------------------------------------------
[1, 5, 3, 6, 9, 8]
Tuple(元組)---不可變資料型別
- 元組的元素不能更改,指的是元組所指向的記憶體地址不可變
- 元組使用小括號(),串列使用[]
- 元組中元素使用逗號分隔
- 元組中只包含一個元素時,需要在元組后面加逗號,否則括號會被當做運算子使用
tup1 = ("asd","asdasd",[1,2,5,9,8])
print(type(tup1))
print(tup1[-1])
---------------------------------------------------
<class 'tuple'>
[1, 2, 5, 9, 8]
Dictionary(字典)---可變資料型別
- 字典是可變容器模型,可以存盤任意型別物件
- 字典的每個鍵值{key:value}對用冒號進行分割,每個對之間用逗號分隔,整個字典包括在大括號中{}
- dict為python的關鍵字和內置函式,不能命名為變數
- 鍵必須是唯一的,且鍵是不可變的,如字串、數字,但值不必,值可以是任何資料型別
- 字典值可以是任何python物件,可以是標準的,也可以是定義的,但鍵不行:(1)不允許同一個鍵出現兩次,創建是如果同一個鍵被賦值兩次,后一個值會被記住(2)鍵必須不可變,所以可以用數字,字串或元組充當,而不能使用串列,
訪問字典中的值(通過鍵去訪問對應的值)
d = {"age":13,"sex":'男',"name":"lihua"}
print(d["sex"])
---------------------------------------------------
男
創建空字典的兩種方法
#創建空字典法一
emptydict = {}
print(emptydict)
print(len(emptydict))
print(type(emptydict))
---------------------------------------------------
{}
0
<class 'dict'>
#創建空字典法二
emptydict1 = dict()
print(emptydict1)
print(len(emptydict1))
print(type(emptydict1))
---------------------------------------------------
{}
0
<class 'dict'>
修改字典
#修改字典
d = {"age":13,"sex":'男',"name":"lihua"}
d["age"] = 10 #更新age
d["school"] = "北京大學" #增添資訊
print(d["age"])
print(d["school"])
---------------------------------------------------
10
北京大學
洗掉、添加字典元素
#洗掉字典元素
d = {"age":13,"sex":'男',"name":"lihua"}
d1 = dict() #創建空字典
d1["name"] = "張偉" #添加資訊
print(d1["name"])
del d1["name"] #洗掉字典資訊
d1.clear() #清除字典
print(d1)
---------------------------------------------------
張偉
{}
Set(集合)---可變資料型別
- 集合是一個無序的不重復的元素序列
- 可以使用大括號或set()函式創建集合(注:創建空集合必須用set()來創建,不可使用{}進行創建)
ax = {'apple','qwe','apple','lllsd','qwe'}
print(ax)
#創建集合
ax1 = set("adsa")
print(ax1)
#判斷元素是否在集合中
z = 'apple' in ax
print(z)
---------------------------------------------------
{'qwe', 'lllsd', 'apple'}
{'a', 'd', 's'}
True
Python3中的資料轉換
資料轉換可分為兩種:隱式型別轉換--自動完成、顯示型別轉換--需要使用型別函式進行轉換
隱式轉換
- 隱式轉換中python會自動將一種資料型別轉換成為另一種資料型別,不需要我們進行干預
將較低資料型別(整數)轉換為較高資料型別(浮點數)以避免資料丟失
num_int = 123
num_float = 1.23
num_new = num_int + num_float
print("num_int的資料型別",type(num_int))
print("num_float的資料型別",type(num_float))
print("num_new的值",num_new)
print("num_new的資料型別",type(num_new))
---------------------------------------------------
num_int的資料型別 <class 'int'>
num_float的資料型別 <class 'float'>
num_new的值 124.23
num_new的資料型別 <class 'float'>
顯示轉換
由于整型和字串型別相加運算結果會報錯,輸出TypeError,因此在這種情況下無法進行隱式轉換,故python提供了一種解決方案稱為顯示轉換,
- 在顯示型別轉換中,用戶將物件的資料型別轉換為所需的資料型別,
- 我們使用int()、float()、str()等預定義函式來執行顯示型別轉換
#顯示型別轉換
num_int = 123
num_str = "456"
print("num_int的資料型別",type(num_int))
print("型別轉換前,num_str的資料型別",type(num_str))
num_str = int(num_str) #強制轉換為整型
print("型別轉換后,num_str的資料型別",type(num_str))
num_sum = num_int + num_str
print("num_int和num_str相加的結果為",num_sum)
print("sum的資料型別為",type(num_sum))
---------------------------------------------------
num_int的資料型別 <class 'int'>
型別轉換前,num_str的資料型別 <class 'str'>
型別轉換后,num_str的資料型別 <class 'int'>
num_int和num_str相加的結果為 579
sum的資料型別為 <class 'int'>
Python推導式
python推導式是一種獨特的資料處理方式,可以從一個資料序列構建另一個新的資料序列的結構題,python支持各種資料結構的推導式,
串列(list)推導式
- out_exp_res:串列生成元素運算式,可以是有回傳值的函式
- for out exp in input list:迭代input list將out exp傳入到out exp res運算式中
- if condition:條件陳述句,可以過濾串列中不符合條件的值
[運算式 for 變數 in 串列]
[out_exp_res for out_exp in input_list]
或
[運算式 for 變數 in 串列 if 條件]
[out_exp_res for out_exp in input_list if condition]
#串列推導式
news = ['Tom','Bob','Smith','Lorry','James','Curry']
new_names = [name.upper() for name in news if len(name)>3]
print(new_names)
---------------------------------------------------
['SMITH', 'LORRY', 'JAMES', 'CURRY']
#計算從0到30可以整除3的整數
match_30 = [i for i in range(31) if i % 3 == 0]
print(match_30)
---------------------------------------------------
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492243.html
標籤:Python
