Python笔记day15(基础)|内置函数

1,内容回顾

# 迭代器和生成器
# 迭代器
    # 可迭代协议 —— 含有iter方法的都是可迭代的
    # 迭代器协议 —— 含有next和iter的都是迭代器
    # 特点
        # 节省内存空间
        # 方便逐个取值,一个迭代器只能取一次。
# 生成器 —— 迭代器
    # 生成器函数
        # 含有yield关键字的函数都是生成器函数
        # 生成器函数的特点
            #调用之后函数内的代码不执行,返回生成器
            #每从生成器中取一个值就会执行一段代码,遇见yield就停止。
            #如何从生成器中取值:
                # for :如果没有break会一直取直到取完
                # next :每次只取一个
                # send :不能用在第一个,取下一个值的时候给上个位置传一个新的值
                # 数据类型强制转换 :会一次性把所有数据都读到内存里
    # 生成器表达式
        # (条件成立想放在生成器中的值 for i in 可迭代的 if 条件)

2,作业

处理文件,用户指定要查找的文件和内容,将文件中包含要查找内容的每一行都输出到屏幕

# def check_file(filename,aim):
#     with open(filename,encoding='utf-8') as f:   #句柄 : handler,文件操作符,文件句柄
#         for i in f:
#             if aim in i:
#                 yield i
#
# g = check_file('1.复习.py','生成器')
# for i in g:
#     print(i.strip())

写生成器,从文件中读取内容,在每一次读取到的内容之前加上‘*’之后再返回给用户。

# def check_file(filename):
#     with open(filename,encoding='utf-8') as f:   #句柄 : handler,文件操作符,文件句柄
#         for i in f:
#             yield '***'+i
#
# for i in check_file('1.复习.py'):
#     print(i.strip())

面试题

# def demo():
#     for i in range(4):
#         yield i
#
# g=demo()
#
# g1=(i for i in g)
# g2=(i for i in g1)
#
# print(list(g))
# print(list(g1))
# print(list(g2))

def add(n,i):
    return n+i

def test():
    for i in range(4):
        yield i

g=test()
# for n in [1,10,5]:
#     g=(add(n,i) for i in g)
n = 1
g=(add(n,i) for i in test())
n = 10
g=(add(n,i) for i in (add(n,i) for i in test()))#把每一层带进去n为最后的5
n = 5
g=(15,16,17,18)


print(list(g))

3,内置函数

# print()
# input()
# len()
# type()
# open()
# tuple()
# list()
# int()
# bool()
# set()
# dir()
# id()
# str()


# print(locals())  #返回本地作用域中的所有名字
# print(globals()) #返回全局作用域中的所有名字
# global 变量
# nonlocal 变量

#迭代器.__next__()
# next(迭代器)
# 迭代器 = iter(可迭代的)
# 迭代器 = 可迭代的.__iter__()

# range(10)
# range(1,11)
# print('__next__' in dir(range(1,11,2)))

# dir 查看一个变量拥有的方法
# print(dir([]))
# print(dir(1))

# help
# help(str)

# 变量
# print(callable(print))
# a = 1
# print(callable(a))
# print(callable(globals))
# def func():pass
# print(callable(func))

import time
# t = __import__('time')
# print(t.time())

某个方法属于某个数据类型的变量,就用.调用
如果某个方法不依赖于任何数据类型,就直接调用 —— 内置函数 和 自定义函数

# f = open('1.复习.py')
# print(f.writable())
# print(f.readable())

#id
#hash - 对于相同可hash数据的hash值在一次程序的执行过程中总是不变的
#     - 字典的寻址方式
# print(hash(12345))
# print(hash('hsgda不想你走,nklgkds'))
# print(hash(('1','aaa')))
# print(hash([]))

# ret = input('提示 : ')
# print(ret)

# print('我们的祖国是花园',end='')  #指定输出的结束符
# print('我们的祖国是花园',end='')
# print(1,2,3,4,5,sep='|') #指定输出多个值之间的分隔符
# f = open('file','w')
# print('aaaa',file=f)
# f.close()

进度条

# import time
# for i in range(0,101,2):
#      time.sleep(0.1)
#      char_num = i//2
#      per_str = '\r%s%% : %s\n' % (i, '*' * char_num) \
#          if i == 100 else '\r%s%% : %s' % (i,'*'*char_num)#三元运算符
#      print(per_str,end='', flush=True)
#progress Bar

exec和eval函数
exec和eval都可以执行 字符串类型的代码
eval有返回值 —— 有结果的简单计算
exec没有返回值 —— 简单流程控制
eval只能用在你明确知道你要执行的代码是什么

# exec('print(123)')
# eval('print(123)')
# print(eval('1+2+3+4'))   # 有返回值
# print(exec('1+2+3+4'))   #没有返回值
# exec和eval都可以执行 字符串类型的代码
# eval有返回值  —— 有结果的简单计算
# exec没有返回值   —— 简单流程控制
# eval只能用在你明确知道你要执行的代码是什么

# code = '''for i in range(10):
#     print(i*'*')
# '''
# exec(code)

# code1 = 'for i in range(0,10): print (i)'
# compile1 = compile(code1,'','exec')
# exec(compile1)

# code2 = '1 + 2 + 3 + 4'
# compile2 = compile(code2,'','eval')
# print(eval(compile2))

# code3 = 'name = input("please input your name:")'
# compile3 = compile(code3,'','single')
# exec(compile3) #执行时显示交互命令,提示输入
# print(name)
# name #执行后name变量有值
# "'pythoner'"

猜你喜欢

转载自blog.csdn.net/qq_34755081/article/details/81749390