引子
def max2(x,y):
if x > y:
return x
else:
return y
x=11
y=22
res = ‘ok‘ if x > y else ‘no‘
print(res)
# 案例1
l = []
for i in range(10):
l.append(i)
print(l)
l = [i for i in range(10)]
print(l)
# 案例2
l = []
for i in range(10):
if i > 5:
l.append(i)
print(l)
l = [i for i in range(10) if i > 5]
print(l)
# 案例3:
names = ["lxx",‘hxx‘,"wxx",‘lili‘]
l = [name + "_sb" for name in names]
print(l)
# 案例4:
names = ["egon","lxx_sb","hxx_sb","wxx_sb"]
res = [name for name in names if name.endswith("sb")]
print(res)
字典生成式
res = {i: i ** 2 for i in range(5)}
items = [(‘k1‘,111),(‘k2‘,222),(‘k3‘,333)]
print(dict(items))
print({k:v for k,v in items})
res = {i for i in range(5)}
print(res)
res = (i for i in range(3))
print(res)
print(next(res))
print(next(res))
print(next(res))
print(next(res))
with open(‘a.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f:
# res = f.read()
# print(len(res))
# res = 0
# for line in f:
# res += len(line)
# res = sum((len(line) for line in f))
res = sum(len(line) for line in f)
print(res)
在调用一个函数的内部又调用自己,所以递归调用的本质就是一个循环的过程
大前提: 递归调用一定要在某一层结束
def func():
print("func")
func()
# func()
import sys
sys.setrecursionlimit(2000)
print(sys.getrecursionlimit())
?
? 1、回溯:向下一层一层挖井
? 2、递推:向上一层一层返回
age(5) = age(4) + 10
age(4) = age(3) + 10
age(3) = age(2) + 10
age(2) = age(1) + 10
age(1) = 18
def age(n):
if n == 1:
return 18
return age(n-1) + 10
res = age(5)
print(res)
nums = [1,[2,[3,[4,[5,[6,[7,]]]]]]]
def get(l):
for num in l:
if type(num) is list:
get(num)
else:
print(num)
get(nums)
nums = [-3,1,3,7,13,23,37,43,57,63,77,91,103]
find_num = 64
def find(nums,find_num):
print(nums)
if len(nums) == 0:
print("not exists")
return
mid_index = len(nums) // 2
if find_num > nums[mid_index]:
# in the right
find(nums[mid_index+1:],find_num)
elif find_num < nums[mid_index]:
# in the left
find(nums[:mid_index], find_num)
else:
print(‘you got it‘)
find(nums,find_num)
def func():
print(‘func‘)
func()
func()
func()
f = lambda x,y:x+y
print(f)
res = f(1,2)
print(res)
res = (lambda x,y:x+y)(1,2)
print(res)
salaries = {
"egon":3000,
"tom":1000000,
"zxx":1000
}
print(max([11,22,33]))
print(max(salaries.values()))
print(max(salaries))
def func(k):
return salaries[k]
print(max(salaries,key=func))
print(max(salaries,key=lambda k:salaries[k]))
print(min(salaries,key=lambda k:salaries[k]))
print(sorted(salaries,key=lambda k:salaries[k]))
print(sorted(salaries,key=lambda k:salaries[k],reverse=True))
map
names = ["lxx",‘hxx‘,"wxx",‘lili‘]
规则
"lxx_sb","hxx_sb"
l = (name + "_sb" for name in names)
res = map(lambda name:name + "_sb",names)
print(list(res))
filter
names = ["lxx_sb",‘egon‘,"wxx_sb",‘lili_sb‘]
print([name for name in names if name.endswith(‘sb‘)])
res = filter(lambda name:name.endswith(‘sb‘),names)
print(list(res))
reduce
from functools import reduce
res = reduce(lambda x,y:x+y,[1,2,3])
res = reduce(lambda x,y:x+y,["aa","bb","cc"])
print(res)
res = pow(10,2,3) # 10 ** 2 % 3
print(res)
面向过程编程绝对不是用函数编程这么简单,面向过程是一种编程思路、思想,而编程思路是不依赖于具体的
语言或语法的。 言外之意是即使我们不依赖于函数,也可以基于面向过程的思想编写程序
核心是“过程”二字,过程就是解决问题的步骤,即先干啥、再干啥、后干啥
所以基于该思想编写程序就好比设计一条一条的流水线
优点:复杂的问题流程化、进而就简单化
缺点:牵一发而动全身、扩展性差
扩展性要求不高的场景,典型案例如linux内核,git,httpd
原文:https://www.cnblogs.com/gfeng/p/14222571.html