python学习日志--day3

时间:2021-07-28 03:34:44

一、字符编码

python学习日志--day3python学习日志--day3


二、函数

1、语法:

def functionName(parameters):
2、函数作用域

3、变量名解析

4、函数参数

位置参数;

关键字参数;

默认参数;

可变参数;

python学习日志--day3


5、匿名函数lambda

6、

python学习日志--day3

输出结果

python学习日志--day3


7、

python学习日志--day3

输出

python学习日志--day3


三、文件修改

import sys
f = open("yesterday","r",encoding="utf-8")
f_new = open("yes.txt","w",encoding="utf-8")

for line in f:
if "快乐" in line:
line = line.replace("快乐","开心")
f_new.write(line)
f.close()
f_new.close()

输出结果为:生成一个新文件yes.txt,文件内容是yesterday文件内容中的“快乐”替换成“开心”之后的文档。


四、进度条

import sys,time

for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)

五、递归

def calc(n):
print(n)
if int(n/2) >0:
return calc( int(n/2) )
print("->",n)


calc(10)

输出结果:

python学习日志--day3