推薦教學:《python影片教學》
#python自訂函數實例有哪些?
python自訂函數實例有:
1、什麼是函數?
函數是組織好的,可重複使用的,用來實現單一,或相關聯功能的程式碼段。函數能提高應用的模組性,和程式碼的重複使用率。
2、函數的定義方法:
def test(x): '函数定义方法' x+=1 return x
解釋:
##def:定義函數關鍵字
test:函數名稱
():可定義形參
:文件描述
:程式碼區塊或程式處理邏輯
:結束並傳回值函數為什麼要有回傳值?
透過傳回值接收函數的執行結果,後續的邏輯需要透過此結果執行其對應操作。
(1)、實例:給一個檔案寫入日誌訊息
import time # 定义函数 def test1(): '函数练习:添加日志记录' log_time = time.strftime('%Y-%m-%d %X') with open('file_a','a') as f: f.write(log_time+':log msg\n') # 调用函数 test1()
(2)、函數回傳值說明:
return 回傳值數量=0:回傳一個空白值(None)
回傳值數量=1:傳回一個物件(object)
傳回值數量>1:##tuples)
#; :#函数返回类型 def test_None(): print('返回一个空值') x=test_None() print(x) def test_object(): print('返回一个对象') return 0 y=test_object() print(y) def test_tuples(): print('返回一个元组') return 1,'hello world',['qwe','asd'],{'001':'simple'} z=test_tuples() print(z)(3)、形參:定義的參數叫形參(x,y) 實參:實際傳入的參數叫實參(1,2)不指定參數的情況下:實參與形參位置一一對應實例:#註:位置傳參與關鍵字傳參共用時,關鍵字參數不能寫在位置參數之前
def test_sum(x,y): '两数之和' z = x + y return z t_sum=test_sum(1,2) #实参与形参位置一一对应 print(t_sum) t_sum2=test_sum(x=1,y=2) #与形参位置无关 print(t_sum2) t_sum3=test_sum(1,y=2) # 错误方式:test_sum(x=1,2) 位置传参与关键字传参共用时,关键参数不能写在位置参数之前 print(t_sum3)(4)、預設值參數:
# 默认值参数 def test_default(x,y=2): '默认值参数' z = x + y return z print(test_default(2)) #print(tesst_default(2,2))(5)、參數組(不定長參數):
# 参数组,接受位置参数,将多个实参存入一个元组中 # 定义格式:*变量名(一般规范为 *args) def test_group(*args): '参数组' print(args) test_group(1,2,3,4,5,6) def test_group2(x,*args): '参数组与位置参数混用' print(x) print(args) test_group2(1,2,3,4,5,6) # 接受关键字参数组,转换成字典 def test_group3(**kwargs): '参数组键值对形式' print(kwargs['name']) test_group3(name='simple',age=25,sex='m') def test_group4(name,**kwargs): '参数、参数组键值对混用' print(name) print(kwargs) test_group4('simple',age=25,sex='m') #注:参数组必须放在位置参数、默认参数之后 def test_group5(name,age=25,**kwargs): '参数、默认参数、参数组键值对混用' print(name) print(age) print(kwargs) test_group5('simple',age=3,sex='m',game='lol') def test_group6(name,age=25,*args,**kwargs): '参数、默认参数、参数组、参数组键值对混用' print(name) print(age) print(*args) print(kwargs) test_group5('simple',age=3,sex='m',game='lol')推薦相關文章:《
python教學
》以上是python自訂函數實例有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具