python————学习函数的使用

是用函数编程,具有一致性,可控制性,减少编程量的特性

import  time
def logger():
    time_format = '%Y-%m-%d %X'
    time_current = time.strftime(time_format)  ##显示时间
    with open('a.txt','a+') as f:    ##给a.txt文件增加内容
        f.write('%s end action\n' %time_current)

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

    logger()    ##调用logger函数
def test2():
    print('in the test2')

    logger()   ##调用logger函数
def test3():
    print('in the test3')
    logger()   ##调用logger函数

test1()
test2()
test3()

  

 

函数的实参也是可以调用其他函数的,是高级函数

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)

  

 

 

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)  ##实参

  实参的值有一一对应关系,

#test(y=2,3) 这样的赋值是会出错的赋值给y后,3这个值是对应也给到y但是这次没有给x赋值就出错了,像test(x=2,3)是没错的
test(2,y=2,3)这样是是不可以的,关键参数y=2是不能写在位置参数前面的,也会出错。

形参组——满足多个形参的使用 接受位置参数
def test(*args):
  print(args)
#第二种方式 test(1,2,3,4,5)
#第二种方式
test(*[1,2,3,4,5]) # args=tuple([1,2,3,4,5])

  

 



posted @ 2018-03-22 13:59  小公子ww  阅读(116)  评论(0编辑  收藏  举报