1. anonymous function
Functions are divided into: well-known functions and anonymous functions.
Famous: said to have a name. Anonymous: - "there is no name -" There is no way to call - "functions and some methods used in conjunction.
def f1()
pass
Named function: Before we given function are well-known function, which is based on the use of the function name
def func()
print("oldboy")
func()
func()
print(func)
#oldboy
#oldboy
#<function func at 0x000001677168A318>
Anonymous function: anonymous function, he has no name binding, ie, once recovered, can either run in brackets
#匿名函数的语法:lambda x,y = x+y
res = (lambda x,y:x+y)(1,2)
print(res)
如果要用,就得变成有名函数
f = lambda x,y = x*y
res = f(1,2)
print(res)
Anonymous functions normally and max / min / filter / map / sorted MS.
max: maximum value of return
res = max(1,2,3,4)
print(res) #4
res = max([1,2,3,4,5])
print(res) #5
dic = {
'Owen':120000
"liang":6000
"li":3000
"zhang":4000
}
def func(name)
return dic[name]
res = max(dic,key = func)
print(res)# Owen 默认key的首字母
key = func 默认做的事情
1.循环遍历dic,会取到所有的值
2.然后把所有的key值依次入func中,返回薪资
3.通过返回值的薪资排序。
直接调用
res = max(max(dic,key=lambda name:dic[name])) # Owen
print(res) #默认key的首字母
Usage :( takes a minimum value min)
dic = {
'Owen':120000,
"liang":6000,
"li":3000,
"zhang":4000,
}
res = min(dic,key=lambda name:dic[name])
print(res) #"li"(取最小的值)
fileter ---> Screening:
# def function(item): # 1/2/3/4
# if item < 5:
# return True
# else:
# return False
# res = filter(lambda item: item > 2, [1, 2, 3, 4])
# print(res) # 迭代器
# print(list(res)) # 3,4
map -> Mapping -> y = x + 1
# def function1(item):
# return item + 2
#
# res = map(function1, [1, 2, 3, ])
# print(res)
# print(list(res)) # 3,4,5
soeted ---> Sort
def function2(item):
return salary_dict[item]
salary_dict = {
'nick': 3000,
'jason': 100000,
'tank': 5000,
'sean': 2000,
'z': 1000
}
res = sorted(salary_dict, key=function2, reverse=True)
print(list(res)) #['jason', 'tank', 'nick', 'sean', 'z']
2. built-in method
python interpreter method
- betes
- chr / word
- divmod
- enumerate
- eval
- hash
betes
res = bytes('中国', encoding='utf8')
print(res) # 把中文翻译为二进制“b
chr / word:
CHR () refer to a digital ASCII code table converted into a corresponding character; the ord () converts characters into a corresponding digital
print(chr(97))#a
print(ord("a"))#97
divmod: Rounding / modulo
Columns ,
print(divmod(10, 3))
#(3, 1)
enumerate
Indexed iteration
lt = [1, 2, 3]
for i in range(len(lt)):
print(i, lt[i])
for ind, val in enumerate(lt):
print(ind, val)
0 1
1 2
2 3
0 1
1 2
2 3
eval
Translating the strings to the data type.
s = '"abc"'
print(type(eval(s)), eval(s))#<class 'str'> abc
hash
Whether the hash.
print(hash(1))
1
To understanding:
- abs
- all
- any
- bin/oct/hex
- to you
- frozenset
- gloabals/locals
- pow
- round
- slice
- sum
- import
1.abs()
Seek absolute values.
print(abs(-13)) # 求绝对值
# 13
2.all()
Inner iterations may object elements are all true, true is returned.
print(all([1, 2, 3, 0]))
print(all([]))
# False
# True
3.any()
Iteration object may have an element is true, True.
print(any([1, 2, 3, 0]))
print(any([]))
#True
#False
4.bin()/oct()/hex()
Binary, octal, hexadecimal conversion.
print(bin(17))
print(oct(17))
print(hex(17))
#0b10001
#0o21
#0x11
5.dir ()
Enumerate all time functions.
import time
print(dir(time))
# ['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname', 'tzset']
6.frozenset()
Immutable collections.
s = frozenset({1, 2, 3})
print(s)
# frozenset({1, 2, 3})
7.globals()/loacals()
View global name; see local name.
# gloabals/locals
# print(globals()) # 列出所有全局变量
# print('locals():', locals())
def func():
s = 's1'
print(globals()) # 列出所有全局变量
print('locals():', locals()) # 列出当前位置所有变量
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]) # print(lis[1:4:1])
# ['b', 'c']
11.sum()
print(sum(range(100)))
# 4950
12.__import__()
Import module string.
m = __import__('time')
print(m.time())
# 1556607502.334777
3. Exception Handling
Exception Handling: error, for processing.
# print(1)
# num = input('请输入数字:')
#
# dic = {'a': 1}
#
# try:
#
# print(dic['b']) # KeyError
# 1 / int(num) # 报错之后,不运行下面的代码
#
# except ZeroDivisionError:
# print('傻逼,不能输入0')
# except KeyError:
# print('傻逼,不知道什么错误')
# print(2)
# print(1)
# num = input('请输入数字:')
#
# dic = {'a': 1}
#
# try:
#
# print(dic['b']) # KeyError
# 1 / int(num) # 报错之后,不运行下面的代码
#
# except Exception as e: # 万能异常,只要有错误,就捕捉
# print(e) # 错误的描述信息
# print('傻逼,不知道什么错误')
#
# print(2)
Abnormal capture can only capture logic error
# fr = open('test.py')
# try:
# # 文件中途报错
# 1 / 0
# fr.close()
# except Exception as e:
# print(e)
# finally: # 无论你包不报错,都执行这一行
# print('finally')