table of Contents
Anonymous function
Famous -> with the name
Anonymous -> no name -> no way to call -> and only in combination with certain methods
Anonymous function syntax
lambda Parameters: Return Value
If you really use, it can also be used, but will become a well-known function
f = lambda x, y: x * y
res = f(1, 2)
print(res)
max / min / filter / map / sorted MS
max / min returns the maximum
res = max([1, 2, 3, 4, 5])
print(res)
res = max(1, 2, 3, 4, 5, 6, )
print(res)
salary_dict = {
'nick': 3000,
'jason': 100000,
'tank': 5000,
'sean': 2000,
'z': 1000
}
def func(name): # nick/jason/tank/sean/z
return salary_dict[name] # 3000/100000/5000/2000/1000
res = max(salary_dict, key=func) # 默认key的首字母
res = max(salary_dict, key=lambda name: salary_dict[name]) # 默认key的首字母
print(res)
key = func default thing to do
- Loop through salary_dict, will take all the key value
- Then all the key values in turn thrown into func in return pay
- By ordering pay returns to give maximum
res = min(salary_dict, key=lambda name: salary_dict[name])
print(res)
fileter ---> Screening
def function(item): # 1/2/3/4
if item < 5:
return True
else:
return False
res = filter(function, [1, 2, 3, 4])
res = filter(lambda item: item > 2, [1, 2, 3, 4])
print(res) # 迭代器
print(list(res))
salary_dict = {
'nick': 3000,
'jason': 100000,
'tank': 5000,
'sean': 2000,
'z': 1000
}
res = filter(lambda item: salary_dict[item] > 3000, salary_dict)
print(list(res))
map -> Mapping -> y = x + 1
def function1(item):
return item + 2
res = map(function1, [1, 2, 3, ])
print(res)
print(list(res))
sorted -> Sort
def function2(item):
return salary_dict[item]
salary_dict = {
'nick': 3000,
'jason': 100000,
'tank': 5000,
'sean': 2000,
'z': 1000
}
res = sorted([2,3,4,1,0,5],key=function2,reverse=True)
res = sorted(salary_dict, key=function2, reverse=True)
print(list(res))
Built-in functions
python解释器内置方法:
# bytes
res = bytes('中国', encoding='utf8')
print(res)
# chr/ord
print(chr(97))
print(ord('a'))
# divmod
print(divmod(10, 4)) # 取整/取余
# enumerate(********)
lt = [1, 2, 3]
for i in range(len(lt)):
print(i, lt[i])
for ind, val in enumerate(lt):
print(ind, val)
# eval(***) --> 把字符串的引号去掉,留下来的是什么就是什么
s = '"abc"'
print(type(eval(s)), eval(s))
# hash,可变不可哈希
print(hash(123123))
# 了解
# abs
print(abs(-1))
# all# 可迭代对象内的元素全部为True则为True
print(all([1, 2, 3, 3]))
# any
print(any([0, 0, ]))
# bin/oct/hex
print(bin(123))
print(oct(123))
print(hex(123))
# dir: 列出模块的所有方法
# import time
#
# print(dir(time))
# frozenset: 不可变化的集合,类似于元组
s = frozenset({1, 2, 3})
print(s)
# gloabals/locals
# print(globals()) # 列出所有全局变量
# print('locals():', locals())
def func():
s = 's1'
print(globals()) # 列出所有全局变量
print('locals():', locals()) # 列出当前位置所有变量
func()
# pow
print(pow(2, 2))
# round
print(round(10.333))
# slice
s = slice(1, 5, 2) # 1start,5stop,2step
lt = [1, 2, 3, 4, 5, 6, 7]
print(lt[s])
print(lt[1:5:2])
# # sum
print(sum([1, 2, 3, 4, 5]))
# __import__ # 通过字符串导入模块
# import 'time'
time = __import__('time')
print(time.time())
Exception Handling
Exception Handling: report a mistake, for processing
dic = {'a': 1}
#
# try:
#
# print(dic['b']) # KeyError
# 1 / int(num) # 报错之后,不运行下面的代码
#
# except Exception as e: # 万能异常,只要有错误,就捕捉
# print(e) # 错误的描述信息
# print('傻逼,不知道什么错误')
#
# print(2)
# fr = open('test.py')
# try:
# # 文件中途报错
# 1 / 0
# fr.close()
# except Exception as e:
# print(e)
# finally: # 无论你包不报错,都执行这一行
# print('finally')
assert assert condition + -> eliminated by history
Process-oriented programming
Noodles version - "process-oriented programming -" thought - "Object-Oriented Programming
There is no good or bad idea
For (Pronoun - "Use) Process (Process -" step) programming (write code, write file)
Made of bronze
I input input (defined variables) P Process Process (control variables changing) O output (new variable) == oriented programming process programmed the output
Materials - "Liquid baking -" poured into a mold - "Cooling -" Beat - "Cooling -" outputs a Bronze
A step that is a function
Process-oriented programming: similar lines, step by step down
Pros: Very clear logic
Cons: Back wrong, wrong with the next step to follow
Why error
Logic - "control variables changing wrong direction
Check the error - "change the status of print variable -"
x = 10
y = 20
z = x + y
print(1, z) # 30
z = z + 10
print(2, z) # 40
z = z - 20
print(z)
debug