python 学习笔记之基础2 函数文件处理

文档处理
# 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())


# tail -f access.log


函数
'''
没有函数会有什么问题?

1.无组织无结构,代码冗余
2.可读性差
3.无法统一管理且维护成本高

'''


# 在python中函数分两类:内置函数,自定义函数

#内置函数
# sum
# max
# min
#
# a=len('hello')
# print(a)
#
# b=max([1,2,3])
# print(b)


#自定义函数

# # ******
# # ******
# # ******
# # hello world
# # ******
# # ******
# # ******
函数的参数
#从大的角度去看,函数的参数分两种:形参(变量名),实参(值)

#定义阶段
# 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:
# 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(name,age=10,*args,sex='male',height,**kwargs):
# def foo(name,age=10,*args,sex='male',height,**kwargs):
# print(name)
# print(age)
# print(args)
# print(sex)
# print(height)
# print(kwargs)
#
# foo('alex',1,2,3,4,5,sex='female',height='150',a=1,b=2,c=3)


# 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 foo(x,y,z):
# print('from foo',x,y,z)
#
# 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 bar():
# x
# if 1 >2:
# print('====>')
#
# bar()

函数的返回值
# 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)



#return 值1,值2,值3,... 返回结果:(值1,值2,值3,...)

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



#三:空函数

# def auth():
# """认证功能"""
# pass
# auth()
def insert():
"""插入功能"""
pass
def select():
"""查询功能"""
pass
def delete():
"""删除功能"""
pass
def update():
"""更新功能"""
pass

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29896444/viewspace-2139268/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29896444/viewspace-2139268/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值