day3 python 函数
常犯的錯誤:
IndentationError:expected an indented block說明此處需要縮進,你只要在出現錯誤的那一行,按空格或Tab(但不能混用)鍵縮進就行...
?
函數是指一組語句的集合通過一個名字(函數名)封裝起來,執行這個函數,調用這個函數名即可。
特性:
減少代碼重復
使程序變得可擴展性
使程序易維護
#定義函數
def sayhi(): #函數名
??? print ('hello world')
sayhi()#調用函數
?
f=open('yesterdate.txt','a') f.truncate(44) #截取長度#文件的修改 f=open('../dang.txt','r') #源文件 p=open('yesterdat.txt','w') #沒有文件創建文件,修改的內容寫入新文件里 #replace修改文件內容 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()
?
#這個用sed就可以了:
#sed -i 's/version=.*/version=0/' config.ini
#如果有多個ini文件:
#sed -i 's/version=.*/version=0/' *.ini
位置參數與形參一一對應
關鍵字參數與形參順序無關????????
關鍵字參數不能寫在位置參數前面
*args 元組 接受位置參數 ,不能接收關鍵字參數
**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 復制下所有內容
?
name=['zba','dex','ggg'] def calc(name): name[0]="女王" print(name) calc(name) print(name)預期結果
['女王', 'dex', 'ggg']
['女王', 'dex', 'ggg']
?
#遞歸
def cu(n): print(n) if int(n/2) >0: return cu(n/2) print(n) cu(10) # python 或 批處理 替換文件中的內容 # 有一個配置文件 config.ini 其中有一段內容 "version=x" 此處x為0、1、2、3、4等數字,但不確定是什么數字 # 如何將這段內容替換為“version=0” 要是用批處理實現是最好的,應該會用到通配符, # 用批處理實現起來有難度。 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) ??
??
轉載于:https://www.cnblogs.com/xuehuahongmei/p/5763686.html
總結
以上是生活随笔為你收集整理的day3 python 函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS开发多线程篇—多线程简单介绍
- 下一篇: 夺命雷公狗---node.js---3c