日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

day3 python 函数

發(fā)布時(shí)間:2025/5/22 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 day3 python 函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

常犯的錯(cuò)誤:

IndentationError:expected an indented block說明此處需要縮進(jìn),你只要在出現(xiàn)錯(cuò)誤的那一行,按空格或Tab(但不能混用)鍵縮進(jìn)就行...

?

函數(shù)是指一組語句的集合通過一個(gè)名字(函數(shù)名)封裝起來,執(zhí)行這個(gè)函數(shù),調(diào)用這個(gè)函數(shù)名即可。

特性:

減少代碼重復(fù)

使程序變得可擴(kuò)展性

使程序易維護(hù)

#定義函數(shù)
def sayhi(): #函數(shù)名
??? print ('hello world')
sayhi()#調(diào)用函數(shù)

?

f=open('yesterdate.txt','a') f.truncate(44) #截取長度
#文件的修改 f=open('../dang.txt','r') #源文件 p=open('yesterdat.txt','w') #沒有文件創(chuàng)建文件,修改的內(nèi)容寫入新文件里 #replace修改文件內(nèi)容 for line in f: if "i like code but really you"? in line: line=line.replace("i like code but really you","i like code but fulimei you") p.write(line) f.close() p.close()

?

#這個(gè)用sed就可以了:
#sed -i 's/version=.*/version=0/' config.ini
#如果有多個(gè)ini文件:
#sed -i 's/version=.*/version=0/' *.ini

#位置參數(shù) def calc(x,y): print(x) print(y) calc(1,2) #關(guān)鍵字參數(shù) def test(x, y): print(x) print(y) test(y=3,x=4) def ff(x,y): print(x) print(y) a=9 b=8 ff(y=a, x=b) ?

位置參數(shù)與形參一一對應(yīng)

關(guān)鍵字參數(shù)與形參順序無關(guān)????????

關(guān)鍵字參數(shù)不能寫在位置參數(shù)前面

*args 元組 接受位置參數(shù) ,不能接收關(guān)鍵字參數(shù)

**kwargs? 字典

def test1() : print('in the test1') def test2() : print ('in the test2') return 0 def test3() : print ('in the test3') return 0 ,'hello',['zhang','xin'],{'tt':'aaa'} x=test1() y=test2() z=test3() print (x) print (y) print (z)

----------------------------

注釋很多行#,CTRL+A ?然后再ctrl +/

TRL+A ?然后 ctrl+d 復(fù)制下所有內(nèi)容

?

name=['zba','dex','ggg'] def calc(name): name[0]="女王" print(name) calc(name) print(name)

預(yù)期結(jié)果

['女王', 'dex', 'ggg']

['女王', 'dex', 'ggg']

?

#遞歸

def cu(n): print(n) if int(n/2) >0: return cu(n/2) print(n) cu(10) # python 或 批處理 替換文件中的內(nèi)容 # 有一個(gè)配置文件 config.ini 其中有一段內(nèi)容 "version=x" 此處x為0、1、2、3、4等數(shù)字,但不確定是什么數(shù)字 # 如何將這段內(nèi)容替換為“version=0” 要是用批處理實(shí)現(xiàn)是最好的,應(yīng)該會用到通配符, # 用批處理實(shí)現(xiàn)起來有難度。 import re f1 = r'd:\config.ini' f2 = r'd:\config.ini.1' with open(f2, 'w') as ff2: with open(f1, 'r') as ff1: for x in ff1: if 'version=' in x: x = re.sub(re.search('version=(\d+)', x).group(1), '0', x) ff2.write(x) ?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/xuehuahongmei/p/5763686.html

總結(jié)

以上是生活随笔為你收集整理的day3 python 函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。