日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

day3 python 函数

發布時間:2025/5/22 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

#位置參數 def calc(x,y): print(x) print(y) calc(1,2) #關鍵字參數 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) ?

位置參數與形參一一對應

關鍵字參數與形參順序無關????????

關鍵字參數不能寫在位置參數前面

*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 函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。