1. 函数递归
递归:函数A调用函数A
函数的嵌套调用是:函数嵌套函数。函数的递归调用:它是一种特殊的嵌套调用,但是它在调用一个函数的过程中,又直接或间接地调用了它自身。如果递归函数不断地调用函数自身,那么这个递归函数将会进入一个死循环,因此我们应该给递归函数一个明确的结束条件。
count = 1 # 2 # 3
def f1():
global count # 下面的count是全局的count
count += 1 # 2 # 3
print(count) # 2 # 3
f1()
f1()
递归的核心:递进的时候能够达到一个结果,问题规模越来越小(不一定要真正的达到); 设置一个条件,能够让最后一次函数调用结束;
递归代码(递归更多的是一种思想,用来解决某种问题)
count = 1 # 2 # 3
def f1():
global count # 下面的count是全局的count
if count > 100:
return
count += 1 # 2 # 3
print(count)
f1()
f1()
求年龄
16/18/20/22/24/26
知道第一个人的年龄16,我要求出5个人后的年龄-->26
age = 16
def age_func(x): # x的规模在减小 # x = 5 # x =4 # x =3 # x =2 # x = 1 # x = 0
global age
if x == 0:
return age # 终止函数,age=26,这是age_func(0)
# age = 18 # age = 20 # age 22 # age = 24 # age = 26
return age_func(x - 1) + 2 # age_func(4) # age_func(3) # age_func(2) # age_func(1) # age_func(0)
res = age_func(5)
print(res) # 26
'''
age_func(5) --> age = 18 --> return age_func(4) # return 26
age_func(4) --> age = 20 --> return age_func(3)
age_func(3) --> age = 22 --> return age_func(2)
age_func(2) --> age = 24 --> return age_func(1) == return age_func(0) == return age == return 26
age_func(1) --> age = 26 --> return age_func(0) == return age == return 26
return age # age = 26
'''
'''
age_func(5) --> return age_func(4)+2 == 24+2 =26
age_func(4) --> return age_func(3)+2 == 22+2 = 24
age_func(3) --> return age_func(2)+2 == 20+2 = 22
age_func(2) --> return age_func(1)+2 == 18+2 = 20
age_func(1) --> return age_func(0)+2 == 16+2= 18
'''
2. 内置方法
# 掌握
# 1 bytes()
# 解码字符
res = '你好'.encode('utf8')
print(res) # b'xe4xbdxa0xe5xa5xbd'
res = bytes('你好',encoding='utf8')
print(res) # b'xe4xbdxa0xe5xa5xbd'
# 2 chr()/ord()
# chr()参考ASCII码表将数字转成对应字符;ord()将字符转换成对应的数字
print(chr(65)) # A
print(ord('A')) # 65
# 3 divmod()
# divmod(a,b) (a//b, a%b)
print(divmod(10,3)) # (3, 1)
# 4 enumerate()
# 带有索引的迭代
l = ['a','b','c']
for i in enumerate(l):
print(i)
'''
(0, 'a')
(1, 'b')
(2, 'c')
'''
# 5 eval()
# 把字符串翻译成数据类型
lis = '[1,2,3]'
lis_eval = eval(lis)
print(lis_eval) # [1, 2, 3]
# 6 hash()
# 是否可哈希
print(hash(1)) # 1
# 了解
# 1 abs()
# 求绝对值
print(abs(-13)) # 13
# 2 all()
# 可迭代对象内元素全为真,则返回真
print(all([1,2,3,0])) # False
print(all([])) # True
# 3 any()
# 可迭代对象中有一元素为真,则为真
print(any([1,2,3,0])) # True
print(any([])) # False
# 4 bin()/oct()/hex()
# 二进制、八进制、十六进制转换
print(bin(17)) # 0b10001
print(oct(17)) # 0o21
print(hex(17)) # 0x11
# 5 dir()
# 列举出所有time的功能
import time
print(dir(time))
# 6 frozenset()
# 不可变集合
s = frozenset({1,2,3})
print(s) # frozenset({1, 2, 3})
# 7 globals()/locals()
# 查看全局名字;查看局部名字
# print(globals())
def func():
a = 1
# print(globals())
print(locals()) # {'a': 1}
func()
# 8 pow()
print(pow(3,2,3)) # (3**2)%3 # 0
# 9 round()
# 四舍五入取整
print(round(3.5)) # 4
# 10 slice()
lis = ['a','b','c']
s = slice(1,4,1)
print(lis[s]) # ['b', 'c']
# 11 sum()
# 求和
print(sum(range(100))) # 4950
# 12 __import__()
# 通过字符串导入模块
m = __import__('time')
print(m.time()) # 1565768986.9250665
3. 面向过程编程
面向 过程 编程 == 按照 流程(流水线的思想) 码代码
eg:造瓶子
输入原材料(一堆塑料) --> 融化塑料 --> 造瓶身和瓶盖 --> 输出瓶子
一堆变量/参数 --> 函数1(过程1) --> 函数2(过程2) --> 结果
上一个过程的输出必定是下一个过程的输入
优点:思路清晰
缺点:1.上一个过程完蛋了,下一个过程也完蛋
2.功能与功能之间不独立
3.牵一发而动全身,不方便修改/扩展功能,可扩展性差
完成某个功能,你可能要写十个函数
模块化思想,9个函数分成3大类模块,模块1/模块2/模块3, 以前9个函数互相关联,顶多3个之间互相关联.功能和功能之间解耦合了(把重复的代码再一次抽离),复杂的问题流程化,更加清晰.
def input_username_pwd():
username = input('username:')
pwd = input('pwd:')
return username, pwd
def read_file(filename):
with open(filename, 'r', encoding='utf8') as fr:
data = fr.read()
return data
def write_file(filename, data):
with open(filename, 'w', encoding='utf8') as fw:
fw.write(data)
def register():
username, pwd = input_username_pwd()
with open('user_info.txt', 'a', encoding='utf8') as fa:
fa.write(f'{username}:{pwd}|')
def login():
username, pwd = input_username_pwd()
user_info = f'{username}:{pwd}'
data = read_file('user_info.txt')
user_info_list = data.split('|')
if user_info in user_info_list:
print('登录成功')
else:
print('登录失败')
register()
login()