三元運算 三元運算又叫三目運算,是對簡單的條件語句的縮寫,例如if判斷 # 標準if判斷語法 if 1 == 1: name = "yes" else: name = "no" # 如果 1==1 成立,name = "yes", 否則 name = "no" # 三元運算簡寫語法 name = " ...
三元運算
三元運算又叫三目運算,是對簡單的條件語句的縮寫,例如if判斷
# 標準if判斷語法 if 1 == 1: name = "yes" else: name = "no" # 如果 1==1 成立,name = "yes", 否則 name = "no" # 三元運算簡寫語法 name = "yes" if 1 == 1 else "no" # 如果條件成立,將yes賦值給name變數,否則將no賦值給name變數View Code
集合
set集合,是一個無序且不重覆的元素集合
# 創建集合 s1 = {11,22} s2 = set() s3 = set([11,22,33,4]) # 操作集合 s = set() s.add(123) # 把要傳入的元素做為一個整個添加到集合中 s.update(123) # 把要傳入的元素拆分,做為個體傳入到集合中 s.clear() # 刪除集合中所有元素 s1 = {11,22,33} s2 = {22,33,44} s3 = s1.difference(s2) # s1中存在,s2中不存在 s3 = s2.difference(s1) # s2中存在,s2中不存在 s3 = s1.symmetric_difference(s2) #s1 s2 中不同時存在的 s1.difference_update(s2) # 更新s1中存在,s2中不存在 s1.symmetric_difference_update(s2) # 更新s1 s2 中不同時存在的 s1 = {11,22,33} s1.discard(1111) # 如果1111是集合s1中的元素刪除,不存在不報錯 s1.remove(11111) # 如果1111是集合s1中的元素刪除,不存在報錯 ret = s1.pop() # 刪除集合中任意一個對象,返回它 s3 = s1.union(s2) # 返回一個新集合,集合元素是s1 s2的並集 s3 = s1.intersection(s2)# 返回一個新集合,集合元素是s1 s2的交集 s1.intersection_update(s2) # s1中成員是共同屬於s1和s2 # li = [11,22,33] # list __init__ # li() # list __call__ # li[0] # list __getitem__ # li[0] = 123 # list __setitem__ # def li[1] # list __delitem__ old_dict = { "#1": 8, "#2": 4, "#4": 2, } new_dict = { "#1": 4, "#2": 4, "#3": 2, } # old_kyes = old_dict.keys() # old_set = set(old_kyes) new_set = set(new_dict.keys()) old_set = set(old_dict.keys()) remove_set = old_set.difference(new_set) add_set = new_set.difference(old_set) update_set = old_set.intersection(new_set) import re re.match()View Code
函數
描述
在學習函數之前,一直遵循:面向過程編程,即:根據業務邏輯從上到下實現功能,其往往用一長段代碼來實現指定功能,開發過程中最常見的操作就是粘貼複製,也就是將之前實現的代碼塊複製到現需功能處,函數式編程最重要的是增強代碼的重用性和可讀性
def sendmail(): try: import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMEText('郵件內容', 'plain', 'utf-8') msg['From'] = formataddr(["武沛齊",'[email protected]']) msg['To'] = formataddr(["走人",'[email protected]']) msg['Subject'] = "主題" server = smtplib.SMTP("smtp.126.com", 25) server.login("[email protected]", "WW.3945.5") server.sendmail('[email protected]', ['[email protected]',], msg.as_string()) server.quit() except: # 發送失敗 return "失敗" else: # 發送成功 return "cc" ret = sendmail() print(ret) if ret == "cc": print('發送成功') else: print("發送失敗")發郵件函數
定義和使用
def 函數名(參數): ... 函數體 ... 返回值格式
函數的定義主要有如下要點:
- def:表示函數的關鍵字
- 函數名:函數的名稱,日後根據函數名調用函數
- 函數體:函數中進行一系列的邏輯計算,如:發送郵件、計算出 [11,22,38,888,2]中的最大數等...
- 參數:為函數體提供數據
- 返回值:當函數執行完畢後,可以給調用者返回數據。
返回值
# 在函數中,一旦執行return,函數執行過程立即終止 def f1(): print(123) return "111" print(456) r = f1() print(r) def f2(): print(123) r = f2() print(r)返回值
參數
函數中有不通的參數
- 普通參數(嚴格按照順序,將實際參數賦值給形式參數)
def sendmail(xxoo, content): # xxoo = alex try: import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMEText(content, 'plain', 'utf-8') msg['From'] = formataddr(["武沛齊",'[email protected]']) msg['To'] = formataddr(["走人",'[email protected]']) msg['Subject'] = "主題" server = smtplib.SMTP("smtp.126.com", 25) server.login("[email protected]", "WW.3945.59") server.sendmail('[email protected]', [xxoo,], msg.as_string()) server.quit() except: # 發送失敗 return "失敗" else: # 發送成功 return "cc" while True: em = input("請輸入郵箱地址:") result = sendmail(em, "SB") if result == "cc": print("發送成功") else: print("發送失敗")形式參數
- 預設參數(必須放置在參數列表的最後)
def send(xxoo, content, xx="OK"): print(xxoo, content, xx) print("發送郵件成功:", xxoo, content) return True while True: em = input("請輸入郵箱地址:") result = send(em, "SB", "ok") if result == True: print("發送成功") else: print("發送失敗")預設參數
- 指定參數(將實際參數賦值給制定的形式參數)
def send(xxoo, content): print(xxoo, content) # print("發送郵件成功:", xxoo, content) return True send(content="alex", xxoo="sb")指定參數
- 動態參數:
* 預設將傳入的參數,全部放置在元組中, f1(*[1`1,22,33,44])
** 預設將傳入的參數,全部放置在字典中 f1(**{"kl":"v1", "k2":"v2"})
f1(n1="alex", n2=18) dic = {'k1': "v1", "k2":"v2"} f1(kk=dic) dic = {'k1': "v1", "k2":"v2"} f1(**dic) f1(11) li = [11,22,"alex", "hhhh"] f1(li, '12') li = [11,22,"alex", "hhhh"] f1(li) f1(*li) li = "alex" f1(*li)動態參數
- 萬能參數, *args,**kwargs
def f1(*args, **kwargs): print(args) print(kwargs) f1(k1="v1") def f1(*args): # args = (11,) # args = ([11,22,"alex", "hhhh"],"12") print(args, type(args)) f1(11,22,33,44) li = [11,22,33,44] f1(*li) def f1(**args): # args = (11,) # args = ([11,22,"alex", "hhhh"],"12") print(args, type(args))萬能參數
全局變數
- 全局變數,所有作用域都可讀
- 對全局變數進行【重新賦值】,需要global
- 特殊:列表字典,可修改,不可重新賦值
def f1(): age = 18 global NAME # 表示,name是全局變數 # NAME = "123" print(age, NAME) def f2(): age = 19 print(age, NAME) f1() f2()全局變數
文件處理
f = open('db', 'r') # 只讀 f = open('db', 'w') # 只寫,先清空原文件 f = open('db', 'x') # 文件存在,報錯;不存在,創建並只寫 f = open('db', 'a') # 追加 f = open('db','r', encoding="utf-8") data = f.read() print(data, type(data)) f.close() f = open('db','r') data = f.read() print(data,type(data)) f = open('db','rb') data = f.read() print(data,type(data)) f = open("db", 'a') f.write("李傑") f.close() f = open("db", 'ab') f.write(bytes("李傑", encoding="utf-8")) f.close() f = open("db", 'r+', encoding="utf-8") # f.fileno() # 如果打開模式無 b,則read,按照字元讀取 data = f.read(1) # tell當前指針所在的位置(位元組) print(f.tell()) # 調整當前指著你的位置(位元組) f.seek(f.tell()) # 當前指針位置開始向覆蓋 f.write("888") f.close()打開文件
read() # 無參數,讀全部;有參數, b,按位元組 無b,按字元 tell() 獲取當前指針位置(位元組) seek(1) 指針跳轉到指定位置(位元組) write() 寫數據,b,位元組;無b,字元 close fileno flush 強刷 readline 僅讀取一行 truncate 截斷,指針為後的清空 for迴圈文件對象 f = open(xxx) for line in f: print(line) f = open("db", 'r+', encoding="utf-8") f.seek(3) f.truncate() f.close() f = open("db", 'r+', encoding="utf-8") for line in f: print(line)操作文件
f.close() with open('xb') as f: pass with open('xb') as f: pass with open('db1', 'r', encoding="utf-8") as f1, open("db2", 'w',encoding="utf-8") as f2: for line in f1: if line == "xx": f2.write() f2.write() # new_str = line.replace("alex", 'st') # f2.write(new_str)關閉文件