Python基礎(chǔ)之函數(shù)的定義與使用示例
本文實(shí)例講述了Python基礎(chǔ)之函數(shù)的定義與使用。分享給大家供大家參考,具體如下:
Python 定義函數(shù)使用 def 關(guān)鍵字,一般格式如下:
def 函數(shù)名(參數(shù)列表):
函數(shù)體
讓我們使用函數(shù)來(lái)輸出"Hello World!":
>>> def hello() :
print("Hello World!")
>>> hello()
Hello World!
>>>
更復(fù)雜點(diǎn)的應(yīng)用,函數(shù)中帶上參數(shù)變量:
def area(width, height):
return width * height
def print_welcome(name):
print("Welcome", name)
print_welcome("Fred")
w = 4
h = 5
print("width =", w, " height =", h, " area =", area(w, h))
以上實(shí)例輸出結(jié)果:
Welcome Fred
width = 4 height = 5 area = 20
函數(shù)變量作用域
定義在函數(shù)內(nèi)部的變量擁有一個(gè)局部作用域,定義在函數(shù)外的擁有全局作用域。通過(guò)以下實(shí)例,你可以清楚了解Python函數(shù)變量的作用域:
#!/usr/bin/env python3
a = 4 # 全局變量
def print_func1():
a = 17 # 局部變量
print("in print_func a = ", a)
def print_func2():
print("in print_func a = ", a)
print_func1()
print_func2()
print("a = ", a)
以上實(shí)例運(yùn)行結(jié)果如下:
in print_func a = 17
in print_func a = 4
a = 4
關(guān)鍵字參數(shù)
函數(shù)也可以使用 kwarg=value 的關(guān)鍵字參數(shù)形式被調(diào)用.例如,以下函數(shù):
def parrot(voltage, state='a stiff', action='voom',
type='Norwegian Blue'):
print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.")
print("-- Lovely plumage, the", type)
print("-- It's", state, "!")
可以以下幾種方式被調(diào)用:
parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
以下為錯(cuò)誤調(diào)用方法:
parrot() # required argument missing parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument parrot(110, voltage=220) # duplicate value for the same argument parrot(actor='John Cleese') # unknown keyword argument
返回值
Python的函數(shù)的返回值使用return語(yǔ)句,可以將函數(shù)作為一個(gè)值賦值給指定變量:
def return_sum(x,y): c = x + y return c res = return_sum(4,5) print(res)
你也可以讓函數(shù)返回空值:
def empty_return(x,y): c = x + y return res = empty_return(4,5) print(res)
可變參數(shù)列表
最后,一個(gè)最不常用的選擇是可以讓函數(shù)調(diào)用可變個(gè)數(shù)的參數(shù).這些參數(shù)被包裝進(jìn)一個(gè)元組(查看元組和序列).在這些可變個(gè)數(shù)的參數(shù)之前,可以有零到多個(gè)普通的參數(shù):
def arithmetic_mean(*args): sum = 0 for x in args: sum += x return sum print(arithmetic_mean(45,32,89,78)) print(arithmetic_mean(8989.8,78787.78,3453,78778.73)) print(arithmetic_mean(45,32)) print(arithmetic_mean(45)) print(arithmetic_mean())
以上實(shí)例輸出結(jié)果為:
244
170009.31
77
45
0
關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python中用format函數(shù)格式化字符串的用法
- Python自定義函數(shù)的創(chuàng)建、調(diào)用和函數(shù)的參數(shù)詳解
- Python入門篇之函數(shù)
- Python def函數(shù)的定義、使用及參數(shù)傳遞實(shí)現(xiàn)代碼
- python中的一些類型轉(zhuǎn)換函數(shù)小結(jié)
- python函數(shù)參數(shù)*args**kwargs用法實(shí)例
- Python函數(shù)中定義參數(shù)的四種方式
- python回調(diào)函數(shù)的使用方法
- Python常用內(nèi)置函數(shù)總結(jié)
- 常用python數(shù)據(jù)類型轉(zhuǎn)換函數(shù)總結(jié)
- Python的函數(shù)嵌套的使用方法
相關(guān)文章
Python用HBuilder創(chuàng)建交流社區(qū)APP
這篇文章主要講解Python使用HBuilder創(chuàng)建交流社區(qū)APP,使用HBuilder做一個(gè)簡(jiǎn)單的社區(qū)瀏覽界面,下面文章附有詳細(xì)的代碼,需要的朋友可以參考一下2021-11-11
Python中使用socket發(fā)送HTTP請(qǐng)求數(shù)據(jù)接收不完整問(wèn)題解決方法
這篇文章主要介紹了Python中使用socket發(fā)送HTTP請(qǐng)求數(shù)據(jù)接收不完整問(wèn)題解決方法,本文使用一個(gè)循環(huán)解決了數(shù)據(jù)不完整問(wèn)題,需要的朋友可以參考下2015-02-02
Python雙精度浮點(diǎn)數(shù)運(yùn)算并分行顯示操作示例
這篇文章主要介紹了Python雙精度浮點(diǎn)數(shù)運(yùn)算并分行顯示操作,涉及Python數(shù)學(xué)運(yùn)算及顯示相關(guān)操作技巧,注釋備有詳盡的說(shuō)明,需要的朋友可以參考下2017-07-07
基于python實(shí)現(xiàn)matlab filter函數(shù)過(guò)程詳解
這篇文章主要介紹了基于python實(shí)現(xiàn)matlab filter函數(shù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
解決PIP安裝第三方庫(kù)報(bào)錯(cuò)SSL: CERTIFICATE_VERIFY_FAILED問(wèn)題
這篇文章主要介紹了解決PIP安裝第三方庫(kù)報(bào)錯(cuò)SSL: CERTIFICATE_VERIFY_FAILED問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
使用實(shí)現(xiàn)python連接hive數(shù)倉(cāng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了使用實(shí)現(xiàn)python連接hive數(shù)倉(cāng)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
tensorflow指定CPU與GPU運(yùn)算的方法實(shí)現(xiàn)
這篇文章主要介紹了tensorflow指定CPU與GPU運(yùn)算的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
使用numpy實(shí)現(xiàn)topk函數(shù)操作(并排序)
這篇文章主要介紹了使用numpy實(shí)現(xiàn)topk函數(shù)操作(并排序),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

