在python中函数分两类:内置函数,自定义函数
内置函数
sum
max
min
a=len('hello')
print(a)
b=max([1,2,3])
print(b)
自定义函数
def print_star():
print('#'*6)
从大的角度去看,函数的参数分两种:形参(变量名),实参(值)
定义阶段
def foo(x,y): #x=1,y=2
print(x)
print(y)
调用阶段
foo(1,2)
详细的区分函数的参数分为五种:
位置参数,关键字参数,默认参数,可变长参数(*args,**kwargs),命名关键字参数
位置参数
def foo(x,y,z):#位置形参:必须被传值的参数
print(x,y,z)
foo(1,2,3)
foo(1,2,3) #位置实参数:与形参一一对应
关键字参数:key=value
def foo(x,y,z):
print(x,y,z)
foo(z=3,x=1,y=2)
关键字参数需要注意的问题:
1:关键字实参必须在位置实参后面
2: 不能重复对一个形参数传值
foo(1,z=3,y=2) #正确
foo(x=1,2,z=3) #错误
foo(1,x=1,y=2,z=3)
默认参数
def register(name,age,sex='male'): #形参:默认参数
print(name,age,sex)
register('asb',age=40)
register('a1sb',39)
register('a2sb',30)
register('a3sb',29)
register('钢蛋',20,'female')
register('钢蛋',sex='female',age=19)
默认参数需要注意的问题:
一:默认参数必须跟在非默认参数后
def register(sex='male',name,age): #在定义阶段就会报错
print(name,age,sex)
(了解)二:默认参数在定义阶段就已经赋值了,而且只在定义阶段赋值一次
a=100000000
def foo(x,y=a):
print(x,y)
a=0
foo(1)
三:默认参数的值通常定义成不可变类型
可变长参数
def foo(x,y,*args): #*会把溢出的按位置定义的实参都接收,以元组的形式赋值给args
print(x,y)
print(args)
foo(1,2,3,4,5)
def add(*args)
res=0
for i in args:
res+=i
return res
print(add(1,2,3,4)
print(add(1,2))
def foo(x, y, **kwargs): # **会把溢出的按关键字定义的实参都接收,以字典的形式赋值给kwargs
print(x, y)
print(kwargs)
foo(1,2,a=1,name='egon',age=18)
def foo(name,age,**kwargs):
print(name,age)
if 'sex' in kwargs:
def foo():
print('from foo')
x=1
return 1,[2,3],(4,5),{}
def foo():
print(kwargs['sex']) if 'height' in kwargs:
print(kwargs['height'])
foo('egon',18,sex='male',height='185')
foo('egon',18,sex='male')
命名关键字参数(了解)
def foo(name,age,*,sex='male',height):
print(name,age)
print(sex)
print(height)
*后定义的参数为命名关键字参数,这类参数,必须被传值,而且必须以关键字实参的形式去传值
foo('egon',17,height='185')
def foo(*args):
print(args)
foo(1,2,3,4) # 1,2,3,4 <=====>*(1,2,3,4)
*['A','B','C','D'],=====>'A','B','C','D'
foo(*['A','B','C','D']) #foo('A','B','C','D')
foo(['A','B','C','D']) #
def foo(x,y,z):
print(x,y,z)
foo(*[1,2,3]) #foo(1,2,3)
foo(*[1,2]) #foo(1,2)
def foo(**kwargs):
print(kwargs)
x=1,y=2 <====>**{'y': 2, 'x': 1}
foo(x=1,y=2)
foo(**{'y': 2, 'x': 1,'a':1}) #foo(a=1,y=2,x=1)
def foo(x,y,z)
print(x,y,z)
foo(**{'z':3,'x':1,'y':2}) #foo(x=1,z=3,y=2)
foo(**{'z':3,'x':1}) #foo(x=1,z=3)
def wrapper(*args,**kwargs):
print(args)
print(kwargs)
wrapper(1,2,3,a=1,b=2)
def foo(x,y,z):
print('from foo',x,y,z)
def wrapper(*args,**kwargs):
print(args) #args=(1,2,3)
print(kwargs) #kwargs={'a':1,'b':2}
foo(*args,**kwargs) #foo(*(1,2,3),**{'a':1,'b':2}) #foo(1,2,3,b=2,a=1)
wrapper(1,2,3,a=1,b=2)
wrapper(1,z=2,y=3)
def foo(x,y,z)
print('from foo',x,y,z)
def wrapper(*args,**kwargs):
# print(args) #args=(1,)
# print(kwargs) #kwargs={'y':3,'z':2}
foo(*args,**kwargs) #foo(*(1,),**{'y':3,'z':2}) #foo(1,z=2,y=3)
wrapper(1,2,3,a=1,b=2)
wrapper(1,z=2,y=3)
函数的返回值
def foo():
print('from foo')
return None
res=foo()
print(res)
以三种情况返回值都为None:
没有return
return 什么都不写
return None
def foo():
print('from foo')
x=1
return x
res=foo()
print(res)
return 一个值 函数调用返回的结果就是这个值
def foo():
print('from foo')
x=1
return 1,[2,3],(4,5),{}
res=foo()
print(res) #打印结果:(1,[2,3],(4,5),{})
a,b,c,d=foo()
print(d)
t=(1,2,3)
a,_,_=t
print(a)
t=(1,2,3,4,5,6,7,8,9)
a,*_,c=t
print(a)
print(c)
def foo():
print('from foo')
def bar(name):
print('bar===>',name)
#按照有参和无参可以将函数调用分两种
foo() #定义时无参,调用时也无需传入参数
bar('egon') #定义时有参,调用时也必须有参数
#按照函数的调用形式和出现的位置,分三种
foo() #调用函数的语句形式
def my_max(x,y):
res=x if x >y else y
return res
# res=my_max(1,2)*10000000 #调用函数的表达式形式
# print(res)
res=my_max(my_max(10,20),30) #把函数调用当中另外一个函数的参数
print(res)
#为什么要定义函数?:先定义后使用,如果没有定义而直接使用,就相当于引用了一个不存在的变量名
# foo()
# def foo():
# print('from foo')
# print(foo)
#函数的使用包含两个阶段:定义阶段和使用阶段
# 语法
# def 函数名(参数1,参数2,...):
# """文档注释"""
# 函数体
# return 值
# x=len('hello')
# print(x)
#定义函数的三种形式
#一:无参数函数:如果函数的功能仅仅只是执行一些操作而已,就定义成无参函数,无参函数通常都有返回值
def print_star():
print('#'*6)
#二:定义有参函数:函数的功能的执行依赖于外部传入的参数,有参函数通常都有返回值
# def my_max(x,y):
# res=x if x >y else y
# return res
# 三元表达式
x=10
y=2
# if x > y:
# print(x)
# else:
# print(y)
#
# res=x if x > y else y
# print(res)
文件处理
f=open('a.txt','r+') #读写
f=open('a.txt','w+') #写读
f=open('a.txt','a+') #追加并且读
f=open('a.txt','w')
f.write('111111\n')
f.close()
上下文管理
with open('a.txt','r',encoding='utf-8') as f,open('b.txt') as b_f:
# print(f.read())
print('====>')
补充
for i in range(3):
print(i)
# continue
if i == 1:
break
else:
print('=============>') #当for循环不被break打断,就会执行else的代码
with open('a.txt','r',encoding='utf-8') as read_f,\
open('aa.txt','w',encoding='utf-8') as write_f:
for line in read_f:
write_f.write(line)
else:
print('write successfull')
i=0
while i< 5:
print(i)
i+=1
if i == 3:
break
else:
print('------>')
with open('a.txt','rb') as f:
print(f.read().decode('utf-8'))
with open('c.txt','wb') as f:
f.write('哈哈哈'.encode('utf-8'))
f=open('sb.jpg','r',encoding='utf-8') #文本的方式读不了二进制文件
print(f.read())
with open('sb.jpg','rb') as read_f,\
open('sb_alex.jpg','wb') as write_f:
data=read_f.read()
write_f.write(data)
不常用的,了解
with open('a.txt','r',encoding='utf-8') as f:
print(f.read(4)) #数字指的是读的是字符
with open('a.txt','rb') as f:
print(f.read(1)) #数字指的是读的是字符
with open('a.txt','r',encoding='utf-8') as f:
f.seek(3) #seek内指定的数字代表字节
print(f.tell()) #当前光标所在的位置
print(f.read())
with open('aa.txt','r+',encoding='utf-8') as f:
# f.seek(3) #seek内指定的数字代表字节
# print(f.read())
f.truncate(1)
with open('b.txt','rb') as f:
f.read()
f.seek(3) #默认情况,是以文件起始位置作为开始,往后移动3个bytes
f.read(1)
print(f.tell())
f.seek(2,1) #1 代表以当前光标所在的位置为开始,往后移动2个 bytes
print(f.tell())
f.seek(-1,2) #2表以当前光标所在的位置为开始,往后移动2个 bytes
print(f.tell())
f.seek(0,2)
with open('c.txt','r',encoding='utf-8') as f:
f.seek(0,2)
print('====>',f.read())