1、函数的定义
- 定义函数:def 函数名();
调用函数:函数名()
解释说明:python 中使用 def 命令创建一个函数,也就是 "定义"(define)的意思,调用函数其实就是执行函数中的代码
def sum(x,y):
print('x=%d'% x)
print('y=%d'% y)
return x+y
a=10
b=5
print('c=%d'% sum(a,b))
2、函数的参数
调用函数:函数名()
解释说明:python 中使用 def 命令创建一个函数,也就是 "定义"(define)的意思,调用函数其实就是执行函数中的代码
def sum(x,y):
print('x=%d'% x)
print('y=%d'% y)
return x+y
a=10
b=5
print('c=%d'% sum(a,b))def funcA(a,b=0): print(a) print(b) funcA(1) funcA(10,20) #2.参数为tuple print('########参数tuple########') def funcD(a,b,*c): print(a) print(b) print("length of c is: %d" % len(c)) print(c) funcD(1,2,3,4,5,6) # main(a,*args) print("##############参数字典#############") #3.参数为dict def funcF(a,**b): print(a) for x in b: print( x + ":" + str(b[x])) funcF(100,x="hello",y="word")
输出结果: 0 20 ########参数tuple######## 2 length of c is: 4 (3, 4, 5, 6) ##############参数字典############# x:hello y:word Process finished with exit code 0
本文详细介绍了Python中函数的基本定义方法及不同类型的参数使用方式,包括默认参数、元组参数和字典参数等,通过具体示例展示了如何定义及调用这些函数。
1838

被折叠的 条评论
为什么被折叠?



