Python-05函数

20:32 2019/1/11/周五
//format用法:
li=["anzhekun",18]
print("我叫{},我今年{}岁".format(*li))//必须加个*,如果是用字典类型的用的是**
结果:我叫anzhekun,我今年18岁

//
li=["anzhekun",18]
print("我叫{},我今年{}岁".format(*li))
print("我叫{1},我今年{0}岁".format("anzhekun",18))//如果前面没有索引那么就是依次取。
结果:我叫anzhekun,我今年18岁
我叫18,我今年anzhekun岁


//函数

def test(x):
    y=x+1
    print(y)
test(2)

如果
def test(x):
    y=x+1
print(test(2))

输出结果:None

如果没有返回值的时候:返回none 
有一个则是返回当前值,如果有多个返回值的时候则返回一个元组

//
def test(x,y,z):
    print(x)
    print(y)

    print(z)

test(x=1,2,z=3)//报错
test(x=1,y=2,z=3)//正确
test(1,2,z=3)//正确//位置参数必须在值的左边。

//def test(x,y,z=2)://有默认的就可以在调用的时候不赋值,如果添加的话,则覆盖之前的默认的。
    print(x)
    print(y)
    print(z)
test(1,2)
结果:

//*y表示的传入的是元组
def test(x,*y):
    print(x)
    print(y)
    print(y[0])
test(1,2,3,4,5,6)
//def test(x,*y):
    print(x)
    print(y)
    print(y[0])
test(1,*["x","y","z"])//在列表前面加*表示遍历列表里面的数据传入给上面的*y

//
def test(x,*y):
    print(x)
    print(y)
    print(y[0][1])
test(1,["x","y","z"])
输出结果:
1
(['x', 'y', 'z'],)
y

猜你喜欢

转载自blog.csdn.net/qq_37431752/article/details/86345848