一、函数参数
下面列举了4种不同情况下 python的函数参数使用方法,以及运行结果
# 函数的参数
# 定义必备参数,也就是说 没有默认值的,只能靠参数的传入
def fun1(string):
print("what you say is:",string)
return
# 定义了默认的参数
def fun2(string = 'hi'):
print("what you say is:",string)
return
# 定义关键字参数
def fun3(string2='hello',string1='world'):
print("what you say is:",string2+string1)
return
# 定义不定长参数
def fun4(arg1,*arg2):
print(arg1)
for i in arg2:
print(i)
return
fun1("hello,world")
fun2()
fun2("hello,hello")
fun3(string1='hihihi',string2='wwwww')
fun4(10,1,2,3,4,5,6,7)
运行结果如下:
what you say is: hello,world
what you say is: hi
what you say is: hello,hello
what you say is: wwwwwhihihi
10
1
2
3
4
5
6
7
二、类
2.1 首先尝试一下 类的创建

本文探讨了Python中函数参数的四种使用方法,并展示了实际运行结果。随后,文章深入介绍了类的创建,包括基本的类定义以及类的继承概念,通过示例展示了如何在Python中实现这些功能。
最低0.47元/天 解锁文章
1242

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



