python中函数的定义实例_Python基础之函数的定义与使用实例
此文實(shí)例介紹了Python基礎(chǔ)之函數(shù)的定義與使用。推薦給大伙學(xué)習(xí)一下,內(nèi)容如下:
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ì)能有幫助。
本文來(lái)源:https://www.cnblogs.com/xiaofan130/p/8919781.html
總結(jié)
以上是生活随笔為你收集整理的python中函数的定义实例_Python基础之函数的定义与使用实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 遮掩java_css之图片下方定位遮掩层
- 下一篇: python切换消息窗_用Python切