1. 程式人生 > >python基礎之元組、檔案操作、編碼、函式、變數 python基礎之元組、檔案操作、編碼、函式、變數

python基礎之元組、檔案操作、編碼、函式、變數 python基礎之元組、檔案操作、編碼、函式、變數

python基礎之元組、檔案操作、編碼、函式、變數

1、集合set

集合是無序的,不重複的,主要作用:

去重,把一個列表變成集合,就可以自動去重

關係測試,測試兩組資料的交集,差集,並集等關係

操作例子如下

複製程式碼
 1 list_1 = [1,4,5,7,3,6,7,9]
 2 list_1=set(list_1)
 3 
 4 list_2 = set([2,6,0,66,22,8,4])  5  6 print(list_1,list_2)  7  8 print(list_1,type(list_1))  9 執行結果如下: 10 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py 11 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} 12 {1, 3, 4, 5, 6, 7, 9} <class 'set'> 13 14 Process finished with exit code 0
複製程式碼

關於集合的功能及操作

複製程式碼
 1 關於就集合的交集intersection:
 2 print(list_1.intersection(list_2))
 3 print(list_1 & list_2)  4 執行結果如下:  5 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py  6 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}  7 {4, 6}  8 {4, 6}  9 #並集union 10 print(list_1.union(list_2)) 11 print(list_1 | list_2) 12 執行結果如下: 13 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py 14 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} 15 {0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22} 16 {0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22} 17 18 Process finished with exit code 0 19 #差集difference 20 print(list_1.difference(list_2)) 21 print(list_2.difference(list_1)) 22 print(list_1-list_2) 23 執行結果如下: 24 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py 25 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} 26 {0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22} 27 {0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22} 28 29 Process finished with exit code 0 30 #子集issubset 31 list_3=set([1,3,7]) 32 print(list_3.issubset(list_1)) 33 print(list_1.issubset(list_2)) 34 #父集issuperset 35 print(list_1.issuperset(list_2)) 36 執行結果如下: 37 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py 38 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} 39 True 40 False 41 False 42 43 Process finished with exit code 0 44 #對稱差集 45 print(list_1.symmetric_difference(list_2)) 46 print(list_1 ^ list_2) 47 執行結果如下: 48 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py 49 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} 50 {0, 1, 2, 66, 3, 5, 7, 8, 9, 22} 51 {0, 1, 2, 66, 3, 5, 7, 8, 9, 22} 52 53 Process finished with exit code 0 54 #判斷是否有交集,如果沒有返回True 55 list_4 = set([5,6,8]) 56 print(list_4.isdisjoint(list_3)) 57 執行結果如下: 58 D:\python35\python.exe D:/python培訓/s14/day3/set集合.py 59 {1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} 60 True 61 62 Process finished with exit code 0 63 #增加add 64 list_1.add(999) 65 print(list_1) 66 #更新 67 list_1.update([888,777,555]) 68 print(list_1) 69 #刪除 70 list_1.remove(4334) 71 print(list_1) 72 #隨機刪除 73 list_1.pop() 74 print(list_1) 75 #如果沒有存在不會報錯,如果是remove的時候會報錯 76 list_1.discard() 77 #集合的長度 78 len(list_1) 79 # x in s 判斷x是否是s的成員
複製程式碼

2、關於檔案操作

f = open("file.txt","r",encoding="utf-8") #檔案控制代碼,即檔案記憶體物件

寫操作w,這個會將檔案清空,即將檔案重新寫一遍,並且如果沒有這個檔案會建立

既讀又寫a----append  只能向檔案中追加內容,也是不能讀

讀寫r+ 

寫讀w+

追加寫a+

複製程式碼
 1 #迴圈讀這個檔案
 2 f=open("file.txt","r",encoding="utf-8")  3 for i in f.readlines():  4 print(i.strip())  5 f=open("file.txt","r",encoding="utf-8")  6 for index,line in enumerate(f.readlines()):  7 if index==2:  8 print("分割線".center(10,"-"))  9 continue 10 print(line.strip()) 11 但是上面的效率比較低 12 下面這種方式更好 13 f=open("file.txt","r",encoding="utf-8") 14 count =0 15 for line in f: 16 count +=1 17 if count == 3: 18 print("分割線".center(10, "-")) 19 print(line.strip()) 20 21 f.tell()列印當前的位置 22 f.seek()返回某個位置 23 f=open("file.txt","r",encoding="utf-8") 24 print(f.tell()) 25 print(f.readline()) 26 print(f.readline()) 27 print(f.readline()) 28 f.seek(0) 29 print(f.tell()) 30 print(f.readline()) 31 32 對於上面開啟檔案的時候用的方式,後面都需要加f.close(),有一種方式可以省卻這個步驟 33 with open(“file.txt”,‘r’) as f:
複製程式碼

3、 Unicode不管是中文和因為都是佔兩個字元,16位

ASCII 不存在中文,8位

UTF-8可變長字元編碼

在utf-8中所有的銀根字元用ascii存放,

所有的中文字元都是3個位元組

通過上圖解釋關於不同字元編碼之間的轉換

GBK轉換成UTF-8

需要先通過decode解碼轉換為Unicode編碼格式

再通過encode轉換為UTF-8編碼格式

4、 函式

函式是指將一組語句的集合通過一個名字封裝起來,要想執行這個函式,只需呼叫其函式名字即可

函式的特性:

減少重複程式碼

是程式易於擴充套件

使程式變得容易維護

 

程式語言中函式定義:函式是邏輯結構化和過程化的一種變成方法

一個函式的定義方法:

複製程式碼
 1 def test(x):
 2 "the function definitions"  3 x+=1  4 return x  5 print(test(2))  6 執行結果如下:  7 D:\python35\python.exe D:/python培訓/s14/day3/func.py  8 3  9 10 Process finished with exit code 0 11 其中: 12 def:定義函式的關鍵字 13 test:函式名 14 ():可以定義引數 15 “”:文件描述 16 return:定義返回值 17 18 一個函式的例子: 19 import time 20 21 def logger_test(): 22 time_format="%Y-%m-%d %X" 23 time_current = time.strftime(time_format) 24 with open("a.txt","a+") as f: 25 f.write('time %s end action\n' %time_current) 26 27 def test1(): 28 print("test1 starting action...") 29  logger_test() 30 31 def test2(): 32 print("test2 starting action...") 33  logger_test() 34 35 def test3(): 36 print("test3 starting action...") 37  logger_test() 38 39 test1() 40 test2() 41 test3() 42 執行結果如下: 43 D:\python35\python.exe D:/python培訓/s14/day3/def函式.py 44 test1 starting action... 45 test2 starting action... 46 test3 starting action... 47 並生成a.txt檔案內容如下: 48 time 2016-08-10 10:52:49 end action 49 time 2016-08-10 10:52:49 end action 50 time 2016-08-10 10:52:49 end action 51 從這裡也可以看出,通過定義函式,可以讓程式更易於擴充套件
複製程式碼

5、 函式和過程

過程定義:就是沒有返回值的函式,在一個函式中沒有使用return顯示定義返回值時,python直譯器會隱式的返回None,所以在python中即便是過程也算做函式

6、關於函式的返回值

複製程式碼
程式碼如下:
def test01():
    pass

def test02(): return 0 def test03(): return 3,2,"hello","zf",["zhaofan","name"],{"name":"dean","age":23} t1 = test01() t2=test02() t3=test03() print("from test01 return is %s:" %type(t1),t1) print("from test01 return is %s:" %type(t2),t2) print("from test01 return is %s:" %type(t3),t3) 執行結果如下: D:\python35\python.exe D:/python培訓/s14/day3/函式2.py from test01 return is <class 'NoneType'>: None from test01 return is <class 'int'>: 0 from test01 return is <class 'tuple'>: (3, 2, 'hello', 'zf', ['zhaofan', 'name'], {'name': 'dean', 'age': 23}) Process finished with exit code 0
複製程式碼

從上面可以看出:

返回值=0:返回None

返回值的個數為1返回object

返回值的個數大於1:返回tuple

7、 函式的呼叫:

呼叫函式的時候()裡可以有引數也可以沒有

引數:

形參和實參

形參:形式引數,不是實際存在的,是虛擬變數,在定義函式和函式體的時候使用形參,目的是在函式呼叫時接收實參

 

位置引數和關鍵字引數(標準呼叫:實參與形參的位置一一對應;關鍵字引數呼叫:位置無序固定)

預設引數

引數組

注意:關鍵引數不能再位置引數前面

關於引數的列子:

複製程式碼
#AUTHOR:FAN
#接收N個位置引數,轉換成元組的形式
def test1(x,*args): print(x) print(args) test1(1,2,3,4,5,6,7) #**kwargs:把N個關鍵字引數,轉換成字典的方式 def test2(**kwargs): print(kwargs) print(kwargs["name"]) print(kwargs["age"]) print(kwargs["sex"]) # # test2(name="zhaofan",age=22,sex="") test2(**{"name":"zhaofan","age":22,"sex":""}) def test3(name,**kwargs): print(name) print(kwargs) test3("alex",age=12,sex="mm") def test4(name,age=18,**kwargs): print(name) print(age) print(kwargs) test4("zhaofan",sex="zz",age=12,hobby="tsl") def test5(name,age=12,*args,**kwargs): print(name) print(age) print(args) print(kwargs) test5("zhaofan",age=34,sex="m",hobby="tsla")
複製程式碼

8、變數

區域性變數只在函式裡生效

字串、整數等在函式裡更改不會影響全域性變數

列表和字典,集合,類等可以在函式裡進行更改

例子演示:

複製程式碼
#AUTHOR:FAN
name = "zhaofan"
def change_name(name): print("before change:",name) name = "dean" print("after change:",name) change_name(name) print("-----",name) 程式執行結果: D:\python35\python.exe D:/python培訓/s14/day3/區域性變數2.py before change: zhaofan after change: dean ----- zhaofan Process finished with exit code 0
複製程式碼

9、  遞迴

複製程式碼
def calc(n):
    print(n)
    if int(n/2) >0: return calc(int(n/2)) print("----->",n) calc(10) 執行結果: D:\python35\python.exe D:/python培訓/s14/day3/遞迴.py 10 5 2 1 -----> 1 Process finished with exit code 0
複製程式碼

遞迴的特性:

必須有一個明確的結束條件

每次進入更深一層時,問題規模要比上次減少

遞迴效率不高

 

遞迴迴圈只能迴圈999層