摘要:函數(shù)也稱方法,是用于實(shí)現(xiàn)特定功能的一段代碼函數(shù)用于提高代碼的復(fù)用性函數(shù)必須要調(diào)用才會執(zhí)行函數(shù)里面定義的變量,為局部變量,局部變量只在函數(shù)里面可以使用,出了函數(shù)外面之后就不能使用一個函數(shù)只做一件事情形參入?yún)魅胍粋€文件名返回文件內(nèi)容轉(zhuǎn)成字典并
函數(shù)也稱方法,是用于實(shí)現(xiàn)特定功能的一段代碼
函數(shù)用于提高代碼的復(fù)用性
函數(shù)必須要調(diào)用才會執(zhí)行
函數(shù)里面定義的變量,為局部變量,局部變量只在函數(shù)里面可以使用,出了函數(shù)外面之后就不能使用
一個函數(shù)只做一件事情
import json def get_file_content(file_name): #形參 #入?yún)ⅲ簜魅胍粋€文件名 #返回:文件內(nèi)容轉(zhuǎn)成字典并返回 with open(file_name, encoding="utf-8") as f: res = json.load(f) return res abc = get_file_content("stus.json") #函數(shù)調(diào)用才執(zhí)行,此處傳入實(shí)參,函數(shù)有返回,需要用變量接收 print(abc)
def write_file(filename,content): with open(filename,"w", encoding="utf-8") as f: #f.write(json.dumps(content)) json.dump(content,f,ensure_ascii=False,indent=4) dict = {"name":"wrp","age":"18"} write_file("wrp.json",dict)
""" 作業(yè): 1、實(shí)現(xiàn)一個商品管理的程序。 #輸出1,添加商品 2、刪除商品 3、查看商品 添加商品: 商品的名稱:xxx 商品如果已經(jīng)存在的話,提示商品商品已經(jīng)存在 商品的價格:xxxx 數(shù)量只能為大于0的整數(shù) 商品的數(shù)量:xxx,數(shù)量只能為大于0的整數(shù) 2、刪除商品: 輸入商品名稱: iphone 如果輸入的商品名稱不存在,要提示不存在 3、查看商品信息: 輸入商品名稱: iphone: 價格:xxx 數(shù)量是:xxx all: print出所有的商品信息 """ FILENAME = "products.json" import json def get_file_content(): with open(FILENAME, encoding="utf-8") as f: content = f.read() if len(content) > 0: res = json.loads(content) else: res = {} return res def write_file_content(dict): with open(FILENAME,"w",encoding="utf-8") as fw: json.dump(dict, fw, indent=4, ensure_ascii=False) def check_digit(st:str): if st.isdigit(): st = int(st) if st > 0: return st else: return 0 else: return 0 def add_product(): product_name = input("請輸入商品名稱:").strip() count = input("請輸入商品數(shù)量:").strip() price = input("請輸入商品價格:").strip() all_products = get_file_content() if check_digit(count) == 0: print("數(shù)量輸入不合法") elif check_digit(price) == 0: print("價格輸入不合法") elif product_name in all_products: print("商品已經(jīng)存在") else: all_products[product_name] = {"count":int(count),"price":int(price)} write_file_content(all_products) print("添加成功") def del_product(): product_name = input("請輸入要刪除的商品名稱:").strip() all_products = get_file_content() if product_name in all_products: all_products.pop(product_name) print("刪除成功") write_file_content(all_products) else: print("商品不存在") def show_product(): product_name = input("請輸入要查詢的商品名稱:").strip() all_products = get_file_content() if product_name == "all": print(all_products) elif product_name not in all_products: print("商品不存在") else: print(all_products.get(product_name))拷貝
淺拷貝,內(nèi)存地址不變,把兩個對象指向同一份內(nèi)容,
深拷貝,會重新在內(nèi)存中生成相同內(nèi)容的變量
l = [1,1,1,2,3,4,5] l2 = l #淺拷貝 #l2 = l.copy() #淺拷貝 for i in l2: if i % 2 != 0: l.remove(i) print(l) #在此程序中,如果對同一個list進(jìn)行遍歷并刪除,會發(fā)現(xiàn)結(jié)果和預(yù)期不一致,結(jié)果為[1,2,4],使用深拷貝就沒問題
import copy l = [1,1,1,2,3,4,5] l2 = copy.deepcopy(l) # 深拷貝 for i in l2: if i % 2 != 0: l.remove(i) print(l)
非空即真,非零即真
name = input("請輸入名稱:").strip() name = int(name) #輸入0時為假 if name: print("輸入正確") else: print("name不能為空")默認(rèn)參數(shù)
import json def op_file_tojson(filename, dict=None): if dict: #如果dict傳參,將json寫入文件 with open(filename,"w",encoding="utf-8") as fw: json.dump(dict,fw) else: #如果沒傳參,將json從文件讀出 f = open(filename, encoding="utf-8") content = f.read() if content: res = json.loads(content) else: res ={} f.close() return res
校驗(yàn)小數(shù)類型
def check_float(s): s = str(s) if s.count(".") == 1: s_split = s.split(".") left, right = s_split if left.isdigit() and right.isdigit(): return True elif left.startswith("-") and left[1:].isdigit and right.isdigit(): return True else: return False else: return False print(check_float("1.1"))全局變量
def te(): global a a = 5 def te1(): c = a + 5 return c res = te1() print(res) #代碼會報錯,te()中定義的a在函數(shù)沒被調(diào)用前不能使用
money = 500 def t(consume): return money - consume def t1(money): return t(money) + money money = t1(money) print(money) #結(jié)果為500
name = "wangcai" def get_name(): global name name = "hailong" print("1,函數(shù)里面的name", name) def get_name2(): print("2,get_name2", name) get_name2() #wangcai get_name() #hailong print("3,函數(shù)外面的name", name) #hailong遞歸
遞歸的意思是函數(shù)自己調(diào)用自己
遞歸最多遞歸999次
def te1(): num = int(input("please enter a number:")) if num%2==0: #判斷輸入的數(shù)字是不是偶數(shù) return True #如果是偶數(shù)的話,程序就退出,返回True print("不是偶數(shù),請重新輸入") return te1()#如果不是的話繼續(xù)調(diào)用自己,輸入值 print(te1()) #調(diào)用test1參數(shù)
位置參數(shù)
def db_connect(ip, user, password, db, port): print(ip) print(user) print(password) print(db) print(port) db_connect(user="abc", port=3306, db=1, ip="192.168.1.1", password="abcde") db_connect("192.168.1.1","root", db=2, password="123456", port=123) #db_connect(password="123456", user="abc", 2, "192.168.1.1", 3306) #方式不對,位置參數(shù)的必須寫在前面,且一一對應(yīng),key=value方式必須寫在后面
可變參數(shù)
def my(name, sex="女"): #name,必填參數(shù),位置參數(shù) #sex,默認(rèn)參數(shù) #可變參數(shù) #關(guān)鍵字參數(shù) pass def send_sms(*args): #可變參數(shù),參數(shù)組 #1.不是必傳的 #2.把傳入的多個元素放到了元組中 #3.不限制參數(shù)個數(shù) #4.用在參數(shù)比較多的情況下 for p in args: print(p) send_sms() send_sms(123456) send_sms("123456","45678")
關(guān)鍵字參數(shù)
def send_sms2(**kwargs): #1.不是必傳 #2.不限制參數(shù)個數(shù) #3.key=value格式存儲 print(kwargs) send_sms2() send_sms2(name="xioahei",sex="nan") send_sms2(addr="北京",county="中國",c="abc",f="kkk")集合
集合,天生可以去重
集合無序,list有序
l = [1,1,2,2,3,3,3] res = set(l) #將list轉(zhuǎn)為set l = list(res) #set去重后轉(zhuǎn)為list print(res) print(l) set = {1,2,3} #set格式 jihe = set() #定義一個空的集合 xn=["tan","yang","liu","hei"] zdh=["tan","yang","liu","jun","long"] xn = set(xn) zdh = set(zdh) # res = xn.intersection(zdh) #取交集 # res = xn & zdh #取交集 res = xn.union(zdh) #取并集 res = xn | zdh #取并集 res = xn.difference(zdh) #取差集,在A中有,在B中無的 res = xn - zdh #取差集 res = xn.symmetric_difference(zdh) #對稱差集,取兩個中不重合的數(shù)據(jù) res = xn ^ zdh print(res)
import string l1 = set(string.ascii_lowercase) l2 = {"a","b","c"} print(l2.issubset(l1)) #l2是不是l2的子集 print(l2.issuperset(l1)) #l2是不是l2的父集 print(l2.isdisjoint(l1)) #是否有交集,有則Flase,無則True l2.add("s") #添加元素 print(l2) print(l2.pop()) #隨機(jī)刪除一個元素 l2.remove("a") #刪除指定元素 for l in l2: print(l)
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/43682.html
摘要:以下這些項(xiàng)目,你拿來學(xué)習(xí)學(xué)習(xí)練練手。當(dāng)你每個步驟都能做到很優(yōu)秀的時候,你應(yīng)該考慮如何組合這四個步驟,使你的爬蟲達(dá)到效率最高,也就是所謂的爬蟲策略問題,爬蟲策略學(xué)習(xí)不是一朝一夕的事情,建議多看看一些比較優(yōu)秀的爬蟲的設(shè)計方案,比如說。 (一)如何學(xué)習(xí)Python 學(xué)習(xí)Python大致可以分為以下幾個階段: 1.剛上手的時候肯定是先過一遍Python最基本的知識,比如說:變量、數(shù)據(jù)結(jié)構(gòu)、語法...
摘要:本文的分享主要圍繞以下幾個方面能做什么常見應(yīng)用場景介紹如何學(xué)習(xí)語法基礎(chǔ)實(shí)戰(zhàn)面向?qū)ο缶幊虒?shí)戰(zhàn)練熟基礎(chǔ)小游戲項(xiàng)目的實(shí)現(xiàn)與實(shí)戰(zhàn)一能做什么一種編程語言往往可以應(yīng)用于多方面,有些方面比較常用,有些方面極為常用。比如表示是一個空列表。 摘要:Python語言的教程雖然隨處可見,但是忙于日常業(yè)務(wù)/學(xué)習(xí)的你或許:一直想要找個時間學(xué)一點(diǎn),但是又不知道該從何下手?本文將從Python能做什么,如何學(xué)習(xí)Py...
摘要:通過函數(shù)名作為其的參數(shù)就能得到相應(yīng)地幫助信息。類是面向?qū)ο缶幊痰暮诵?,它扮演相關(guān)數(shù)據(jù)及邏輯的容器的角色。之后是可選的文檔字符串,靜態(tài)成員定義,及方法定義。 Python 源文件通常用.py 擴(kuò)展名。當(dāng)源文件被解釋器加載或顯式地進(jìn)行字節(jié)碼編譯的時候會被編譯成字節(jié)碼。由于調(diào)用解釋器的方式不同,源文件會被編譯成帶有.pyc或.pyo擴(kuò)展名的文件,你可以在第十二章模塊學(xué)到更多的關(guān)于擴(kuò)展名的知識...
摘要:函數(shù)的基本結(jié)構(gòu)中的函數(shù)基本結(jié)構(gòu)函數(shù)名參數(shù)列表語句幾點(diǎn)說明函數(shù)名的命名規(guī)則要符合中的命名要求。在中,將這種依賴關(guān)系,稱之為多態(tài)。不要期待在原處修改的函數(shù)會返回結(jié)果比如一定要之用括號調(diào)用函數(shù)不要在導(dǎo)入和重載中使用擴(kuò)展名或路徑。 在本教程的開始部分,就已經(jīng)引入了函數(shù)的概念:《永遠(yuǎn)強(qiáng)大的函數(shù)》,之所以那時候就提到函數(shù),是因?yàn)槲矣X得函數(shù)之重要,遠(yuǎn)遠(yuǎn)超過一般。這里,重回函數(shù),一是復(fù)習(xí),二是要在已經(jīng)...
馬上就要開始啦這次共組織15個組隊(duì)學(xué)習(xí) 涵蓋了AI領(lǐng)域從理論知識到動手實(shí)踐的內(nèi)容 按照下面給出的最完備學(xué)習(xí)路線分類 難度系數(shù)分為低、中、高三檔 可以按照需要參加 - 學(xué)習(xí)路線 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...
閱讀 1475·2021-11-24 09:39
閱讀 1789·2021-11-22 15:25
閱讀 3741·2021-11-19 09:40
閱讀 3300·2021-09-22 15:31
閱讀 1302·2021-07-29 13:49
閱讀 1211·2019-08-26 11:59
閱讀 1321·2019-08-26 11:39
閱讀 936·2019-08-26 11:00