day14 总结

1. 匿名函数

函数分为:有名函数和匿名函数。

有名:则称有名字。 匿名:--》则没有名字--》没有调用方式--》职能和某些方法连用。

def f1()
    pass

有名函数:我们之前定的函数都是有名函数,它是基于函数名使用

def func()
    print("oldboy")
func()
func()
print(func)

#oldboy
#oldboy
#<function func at 0x000001677168A318>

匿名函数:匿名函数,他没有绑定名字,使用一次即被收回,加括号既可以运行

#匿名函数的语法: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)

匿名函数通常和 max / min /filter /map / sorted联用。

max :返回最大值

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的首字母

min的用法:(取最小值)

dic = {
    'Owen':120000,
    "liang":6000,
    "li":3000,
    "zhang":4000,
}
res = min(dic,key=lambda name:dic[name])
print(res)  #"li"(取最小的值)

fileter --->筛选:

# 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 --> 映射 --> y = x+1

# def function1(item):
#     return item + 2
#
# res = map(function1, [1, 2, 3, ])
# print(res)
# print(list(res))  # 3,4,5

soeted ---> 排序

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.内置方法

python解释器的方法

  1. betes
  2. chr/ord
  3. divmod
  4. enumerate
  5. eval
  6. hash

betes

res = bytes('中国', encoding='utf8')
print(res)  # 把中文翻译为二进制“b

chr / ord:

chr()参考ASCII码表将数字转成对应字符;ord()将字符转换成对应的数字

print(chr(97))#a
print(ord("a"))#97

divmod:取整/取余

分栏 ,

print(divmod(10, 3))
#(3, 1)

enumerate

带索引的迭代

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

把字符串翻译成数据类型。

s = '"abc"'
print(type(eval(s)), eval(s))#<class 'str'> abc

hash

是否可哈希。

print(hash(1))
1

了解:

  1. abs
  2. all
  3. any
  4. bin/oct/hex
  5. dir
  6. frozenset
  7. gloabals/locals
  8. pow
  9. round
  10. slice
  11. sum
  12. import

1.abs()

求绝对值。

print(abs(-13))  # 求绝对值
# 13

2.all()

可迭代对象内元素全为真,则返回真。

print(all([1, 2, 3, 0]))
print(all([]))
# False
# True

3.any()

可迭代对象中有一元素为真,则为真。

print(any([1, 2, 3, 0]))
print(any([]))
#True
#False

4.bin()/oct()/hex()

二进制、八进制、十六进制转换。

print(bin(17))
print(oct(17))
print(hex(17))
#0b10001
#0o21
#0x11

5.dir()

列举出所有time的功能。

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()

不可变集合。

s = frozenset({1, 2, 3})
print(s)
# frozenset({1, 2, 3})

7.globals()/loacals()

查看全局名字;查看局部名字。

# 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__()

通过字符串导入模块。

m = __import__('time')
print(m.time())
# 1556607502.334777

3.异常处理

异常处理:报错,进行处理。

# 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)

异常捕捉只能捕捉逻辑错误

# fr = open('test.py')
# try:
#     # 文件中途报错
#     1 / 0
#     fr.close()
# except Exception as e:
#     print(e)
# finally:  # 无论你包不报错,都执行这一行
#     print('finally')

猜你喜欢

转载自www.cnblogs.com/WQ577098649/p/11588047.html