python函数第一天(day 20)

 f.writelines(['111111\n','1111111\n'])

以列表的形式循环写,每次一行

为什么要用函数:解决代码的冗余(同一个功能,多处用到,要写多处),扩展性差(修改一个功能,要修改所有用到的代码)

函数分为:

#内置函数sum max len
# print(print)

#自定义函数

 

函数的定义 :

def 函数名(arg1,arg2,arg3):
  '描述信息'
  函数体
  return 1

 

#函数的返回值可以是任意类型
#没有return--->None
#return 1--->1
#return 1,2,3--->(1,2,3)

return返回的是一个元组,

def bar(x,y):
  # return (1,2,3,4,5,6,[1,2],{'a':2},{1,2,3})
  return 1,2,3

# res2=bar(1,2)
# print(res2)

# a,b,c=bar(1,2)
# print(a)
# print(b)
# print(c)

 注意:函数的所传的实参,必须是不可变类型,不要跟外部扯上关系

############################

形参:定义函数时的def 后面跟的其实就是个变量名,内存中没有值

实参:函数调用时如foo(1,2)变量值

#函数的参数介绍
#
# def foo(x,y):
  # print(x)
  # print(y)

 

 


#在实参的角度

#第一种:按照位置传值
# foo(1,2)
# foo(2,1)


#第二种:按照关键字传值
# foo(x=1,y=2)
# foo(y=2,x=1)

#第三种:混着用
# foo(1,y=2)
#问题一:按位置传值必须在按关键字传值的前面
# foo(y=2,1) #报错
#问题一:对于一个形参只能赋值一次
# foo(1,y=2)


#从形参的角度来分析

#位置参数:必须传值的参数
# def foo(x,y):
  # print(x)
  # print(y)
#
# foo(1,2,3)
# # foo(y=2,x=1)


#默认参数
def foo(x,y=1):
  print(x)
  print(y)

# # foo(1)
# foo(1,2)
# foo(y=2,x=2)


# def open(file,mode='r'):
  # pass
#
# open('a.txt')

 

# def register(name,sex='male'):
  # print(name)
  # print(sex)
#
# register('qinzhen')


#默认参数必须注意的问题是:默认参数必须放到位置参数的后面
# def register(sex='male'): #报错
  # print(name)
  # print(sex)
# register()
# # register('qinzhen')

# x='male'
# def register(sex=x): # register-------------[sex='male' ...]
  # print(sex)
# #------------------------------------------------
# x=None
# register()


#不推荐下述方式
# x=[]
# def register(name,name_list=x): #
  # name_list.append(name)
# #------------------------------------------------
# register('ASB')
# register('YSB')
# register('WSB')
# print(x)
#

 


#*args
# def foo(x,*args): #args=(2, 3, 4, 5, 6, 6, 'a', 'b')
# print(x)
# print(args)
#
# foo(1,2,3,4,5,6,6,'a','b')


# #*args与位置参数和默认参数混用:*args要放到位置参数后面
#*args会把多余的所有参数,接收成一个元组,会把形参按位置传入的多入的参数,保存成元组
# def foo(x,*args,y=1):
  # print(x)
  # print(y)
  # print(args)
#
# foo(1,2,3,4,5,6,7,8,9,10,y=10000000)


#从形参的角度
def foo(*args): #foo(x,y,z)
  print(args)

# foo(1,2,3)

#*args=*(1,2,3)
#从实参的角度
def bar(x,y,z):
  print(x)
  print(y)
  print(z)
# bar(*(1,2,3,4)) #bar(1,2,3,4)

 

 

# def my_sum(*nums): #nums=(1,2,3,4,5,6,7)
  # res=0
  # for i in nums:
    # res+=i
  # return res
#
# # print(my_sum((1,2,3,4,5)))
# print(my_sum(1,2,3,4,5,6,7))

 

 


#**kwargs **会把你实参按关键字传的参数多入的保存成一个字典
# def foo(x,**kwargs):
  # print(x)
  # print(kwargs)
#
# foo(1,y=1,z=2)

#混着用的位置问题
# def foo(x,*args,**kwargs):
  # print(x)
  # print(args)
  # print(kwargs)
#
# foo(1,y=1,z=2)


# def foo(*args,**kwargs):
  # print(args)
  # print(kwargs)
#
# foo(1,1,1,1,1,1,a=1,b=2)


#从形参的角度
def foo(**kwargs): #x=1,y=2
  print(kwargs)

#
# foo(x=1,y=2,z=3)

#从实参的角度
# def foo(x,y,z=1):
  # print(x)
  # print(y)
  # print(z)


# foo(**{'x':1,'y':2,'z':3}) #foo(x=1,y=2,z=2)
# foo(**{'x':1,'y':2}) #foo(x=1,y=2)
# foo(**{'a':1,'y':2}) #foo(a=1,y=2)


def auth(name,password,sex='male'):
  print(name)
  print(password)
  print(sex)

def foo(*args,**kwargs): #args=('egon','123') kwargs={}
  print('from foo')
  auth(*args,**kwargs)
  # auth(*'egon','123'),**{})--->auth('egon','123')

# foo('egon','123')
# foo('ASB','123',sex='female')
foo(name='ASB',password='123',sex='female')

 

转载于:https://www.cnblogs.com/wanchenxi/p/7349036.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值