重要内置函数、常见内置函数

Day15

今日内容概要

  • 重要内置函数

  • 常见内置函数

  • 可迭代对象

  • 迭代器对象

  • for循环内部原理

  • 异常处理

    今日内容详细

重要内置函数

1.zip() 拉链
zip() # 接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个 tuple,然后返回由这些 tuple 组成的 list。 若传入参数的长度不等,则返回 list 的长度和参数中长度最短的对象相同
l1 = [11, 22, 33, 44, 55, 66]
l2 = ['almira', 'judy', 'rudy', 'lina', 'linda', 'lily']
l3 = [1, 2, 3, 4, 5, 6]
res = zip(l1, l2, l3)
print(list(res))

image

2.filter 过滤
l1 = [11, 22, 33, 44, 55, 66, 77, 88]
res = filter(lambda x: x > 40, l1)
print(list(res))
3.sorted 排序
l1 = [21, 12, 53, 64, 76, 32, 11, 22]
res = sorted(l1)
print(res) # 默认是升序

image

常见内置函数

1.abs() 绝对值
#取绝对值(absolute绝对)
    print(abs(-111))
	print(abs(212))
2.all() 所有
#所有的数据值对应的布尔值为True结果才是True 否则返回False
print(all([0, 123, 4]))
print(all([213, 323, True]))

image

3.any
#所有的数据值对应的布尔值为有一个为True结果就是是True 否则返回False
print(any([0, None, '', 1]))
print(any([0, None, '']))
4.bin() oct() hex() int()
bin() # 二进制, binary 0 1
oct() # 八进制,Octal 0 1 2 3 4 5 6 7
hex() # 十六进制, HEXadecimal 0~9 a~f
int() # 整型, integer
5.bytes 转换成bytes类型
s1 = 'just do it!u can do it!'
print(s1.encode('utf8'))
print(bytes(s1, 'utf8'))
6.callable()
# 判断名字是否可以加括号调用
name = 'almira'
def index():
    print('from index')
print(callable(name)) # True
print(callable(index)) # False
7.chr() ord()
# 基于ASCII码表做数字与字母的转换
# A~Z 65~90  a~z 97~122
print(chr(65)) # A 数字转换为对应的字母
print(ord('A')) # 65 字母转换为对应的序号
8.dir()
# 返回括号内对象能够调用的名字
print(dir('hello'))
9.divmod()
# 元组第一个数据为商 第二个是余数(适用于网站分页)
res = divmod(100, 3)
print(res)

# page_num, more = divmod(9999, 20)
# print(divmod(99, 10))  # (9, 9)
# if more:
#     page_num += 1
# print('总页码为:', page_num)  # 总页码为: 500

image

10.enumerate() 枚举
d1 = {i:j for i in enumerate('beautiful')}
    print(d1)
11.eval() and exec()
# s1 = 'print("哈哈哈")'
# eval(s1)
# exec(s1)
# s2 = 'for i in range(100):print(i)'
# eval(s2)  
eval()能力弱一点 只能识别简单的python代码; 
exec()能力强一些 能识别具有逻辑性python代码 能够识别字符串中的python并执行
12.hash() 哈希加密
hash('almira')
res = hash('almira')
print(res) # 5081148859969051842
13.id() input() isinstance()
id() # 函数用于获取对象的内存地址
input()
usernmae = input('username>>>:')
isinstance() # 判断是否整型
14.map() max() min()
14.1 map() 映射  
    l1 = [1, 2, 3, 4, 5, 6, 5]
    def func(a):
        return a + 1
    res = map(lambda x:x+1, l1)
    print(list(res))
    
14.2 max()/min() 最大/最小值
    l1 = [1, 2, 3, 4, 5, 6, 5]
    res = max(l1)
    print(res)

    d1 = {
        'almira':20000,
        'lida':2122,
        'judy':1212,
    }
    res = max(d1, key=lambda k:d1.get(k))
    print(res)
15.open()
 with open(r'a.txt', 'r', encoding='utf8') as f:
        print(f.read())
16.pow()
# 幂指数
1.pow()函数 Python的内置函数,它计算并返回x的y次方的值。
2.pow(x, y, z) # 参数 x -- 数值表达式。 y -- 数值表达式。 z -- 数值表达式
17.range()
3.编写代码自动生成所有页网址(注意总共多少页)
	https://movie.douban.com/top250
      """"
https://movie.douban.com/top250
 https://movie.douban.com/top250?start=25&filter=
 https://movie.douban.com/top250?start=50&filter=
 https://movie.douban.com/top250?start=75&filter=
 https://movie.douban.com/top250?start=100&filter=
"""
# 先定义网址的模板
base_url = ' https://movie.douban.com/top250?start=%s&filter='
for i in range(1,251):
    print(base_url % i)
18.round()四舍五入
print(round(98.5)) # 98
print(round(98.2)) # 98
# 结果不够精确
19.sum求和
print(sum([123, 47, 82, 94, 54, 1, 874])) #列表里面的每一个数连续加
20.zip()
zip
l1 = [11, 22, 33, 44, 55, 66]
l2 = ['almira', 'judy', 'rudy', 'lina', 'linda', 'lily']
l3 = [1, 2, 3, 4, 5, 6]
res = zip(l1, l2, l3)
print(list(res))

image

可迭代对象

1.定义:数据对象内置有__iter__方法的都称为可迭代对象
"""
内置方法 通过点方法能够调用的方法
__iter__(双下iter)
"""
2.课迭代对象的范围
*不是可迭代对象
int float bool
*是可迭代对象
str list dict tuple set 文件对象
3.可迭代的含义
"""
迭代:更新换代(每次更新都必须依赖上一次的结果)
	eg:手机app更新
"""
#可迭代在python中可以理解为是否支持for循环

迭代器对象

1.迭代器对象
	是由可迭代对象判断的本质是看是否内置有__iter__和__next__

2.迭代器对象的作用
	提供了一种不依赖于索引取值的方式,正因为有迭代器的存在 字典 集合才能被for循环
3.迭代器对象的实操
	s1 = 'hello' # 可迭代对象
    res = s1.__iter__() # 迭代器对象
    print(res.__next__()) # 迭代取值for循环的本质
    
    *一旦__next__取不到值 就会直接报错*
    
4.迭代器对象注意事项
 	可迭代对象调用__next__会成为迭代器对象 迭代器对象如果还调用__next__不会有任何变化还是迭代器对象本身

for循环的本质

for 变量名 in 可迭代对象:
	循环体代码
"""
1.先将in后面的数据调用__iter__转变成迭代器对象
2.依次让迭代器对象调用__next__取值
3.一旦__next__取不到直接报错 for循环会自动捕获并处理
"""

异常捕获 / 处理

1.异常
异常就是代码运行报错 行业俗语bug 代码运行中一旦遇到异常会直接结束整个程序的运行 我们在编写代码的过程中尽量部门异常
2.异常类型
# 语法错误:
	不允许出现,一旦出现立刻改正 否则提桶跑路
# 逻辑错误:
	允许出现的,因为这种错误不容易被发现,代码运行之后会报错这时才可以知道具体什么错误
3.异常结构
异常结构(这个一定要学会看,出现报错时可以快速定位错误)
	# 错误位置
	# 错误类型
	# 错误详情

image

posted @ 2022-10-14 19:51  阿丽米热  阅读(27)  评论(0编辑  收藏  举报
Title