外面雷声轰鸣,我在屋里暴躁学习。
=================================================分割线==========================================
第一个程序
文件关闭-fileclose
# encoding: utf-8
'''
@author: ccq
@file: fileclose.py
@time: 2019/6/6 8:19
'''
# 文件关闭
with open("yesterday", "r", encoding="utf-8")as f:
print(f.readline())
#昨天漏了一个小点,对文件操作结束后,应该注意要关闭文件。但是close方法是写在最后的很容易忘记,于是乎就有了with的这种写法。在一开始就设置好关闭,是一个不错的选择。
#with可以同时打开多个文件,写法如下:
with open("yesterday", "r", encoding="utf-8")as f1, open("yesterday2", "w", encoding="utf-8")as f2:
print(f1.readline())
f2.write("hello")
#突然想起来python官方说过一个规定,一行代码不要超过60个字节。那么应该用如下写法。
with open("yesterday", "r", encoding="utf-8")as f1, \
open("yesterday2", "w", encoding="utf-8")as f2:
print(f1.readline())
f2.write("hello")
=================================================分割线==========================================
第二个程序
函数-function
# encoding: utf-8
'''
@author: ccq
@file: function.py
@time: 2019/6/6 9:50
'''
# 函数
# 函数
def function1():
'''function1函数'''
print("this is function")
return 5
# 过程
def function2():
'''function2过程'''
print("this is process")
x = function1()
y = function2()
print("from function1 return is %s" % x)
print("from function2 return is %s" % y)
#过程是没有返回值的函数。python里没有明确的区别。
#返回值:x是5,y是none。
==============================================分割线=============================================
第三个程序
时间日志-timelog
# encoding: utf-8
'''
@author: ccq
@file: datelog.py
@time: 2019/6/6 10:26
'''
# 时间日志
import time
def logger():
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
with open('log', 'a+', encoding='utf-8')as f:
f.write('%s end function\n' % time_current)
def function1():
logger()
for i in range(5):
function1()
time.sleep(1)
#过程是一个很好用的东西。
==============================================分割线=============================================
第四个程序
参数-parameter
# encoding: utf-8
'''
@author: ccq
@file: parameter.py
@time: 2019/6/8 15:01
'''
# 参数
# 方法1
def test1(x):
print("test1", x)
test1(1)
# 方法2
def test2(x, y):
print("test2", x, y)
test2(2, 3)
# 方法3
def test3(x, y):
print("test3", x, y)
test3(4, y=5)
# 方法4
def test4(x, y=7):
print("test4", x, y)
test4(6)
# 方法5
def test5(x, y=9):
print("test5", x, y)
test5(8, 10)
# 元组
# 接受位置参数转换成元组
def test6(*args):
print("test6", args)
test6(1, 2, 3, 4, 4, 5)
test6(*[1, 2, 3, 4, 5])
# 字典
# 接受关键字参数转换成字典
def test7(**kwargs):
print("test7", kwargs)
test7(name="ccq", age="20", sex="male")
# 综合
def test8(name, age=20, *args, **kwargs):
print("test8")
print(name)
print(age)
print(args)
print(kwargs)
test8('ccq', 3, 4, 5, sex='male', hobby='python')
#函数式编程中最重要的就是函数。函数的返回值以及参数是核心的东西。
#方法4和5是默认值的对比,在不按位置赋值的时候,自动赋默认值,如果赋值,则更新为赋值内容。
#方法6:接受位置参数并转换成元组,当实参数量不固定,实参就可以用这样的方式定义。
#方法7:接受关键字参数并转换成字典。
#方法8:综合。注意:位置参数不能在关键字之后出现。这意味着:
test8('ccq', age=21,1, sex='male', hobby='python')
#上面这样的写法是错误的,age=21是关键字参数,后面的1是位置参数。因此pycharm会直接报错。
#这里可以看出,当输入多个形参之后,尤其是输入默认值后,后面还有位置参数时,就要注意,默认值不可以省略。就像下面这样。
test8('ccq', 20, 4, 5, sex='male', hobby='python')
#20必须要写出来,不然就会被位置参数覆盖。
==============================================分割线=============================================
今天结束。大家端午快乐!