函数式编程

本文详细介绍了Python中函数的定义方法及其使用技巧,包括函数与过程的区别、返回值的处理方式以及不同类型的参数如何传递等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python中函数定义方法:

def test(x):
"the function definitions"
x+=1
return x

def:定义函数的关键字
test:函数名
"":文档描述
x+=1: 泛指代码块或程序处理逻辑
return : 定义返回值

 

函数与过程的区别
函数有返回值,过程没有返回值。过程就是没有返回值的函数
#函数
def func1():
"""testing1"""
print('in the func1')
return 0
#过程
def func2():
'''testing2'''
print('in the func2')

x=func1()
y=func2()

print('from func1 return is %s' %x)
print('from func2 return is %s' %y)

结果
in the func1
in the func2
from func1 return is 0
from func2 return is None

 


函数返回值:

def test1():
print('in the test1')

def test2():
print('in the test2')
return 0
def test3():
print('in the test3')
#return 1,'hello',['alex','wupeiqi'],{'name':'alex'}
return test2

x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)

结果:
in the test1
in the test2
in the test3
None
0
<function test2 at 0x0048B738>

总结:
返回值数=0:返回None
返回值数=1:返回object
返回值数>1:返回tuple

 


参数详解
def test(x,y,z):
print(x)
print(y)
print(z)

# test(y=2,x=1) #与形参顺序无关
# test(1,2) #与形参一一对应
#test(x=2,3)
test(3,z=2,y=6) #关键参数一定在位置参数之后

结果
3
6
2

 

转载于:https://www.cnblogs.com/tengtianshan/p/9593753.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值