day3 python 函数
常犯的錯(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ù)與形參一一對應(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS开发多线程篇—多线程简单介绍
- 下一篇: python学习笔记系列----(五)输