python 中 函数参数 及 类

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

一、函数参数

下面列举了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 首先尝试一下  类的创建

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值