day 15 内置函数

内置函数 不用def定义能直接用的函数,带括号的

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

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

range(10)
range(1,11)
range(1,11,2)

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

变量
print(callable(print))
a = 1
print(callable(a))
def func():
    pass
print(callable(func))
callable() 检测是否是函数

help
help(str) 当前数据类型的可用的方法
比dir更详细的解释

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

list.append()
list.__len__()
dir(list)

某个方法属于某个数据类型的变量,就用调用
如果某个方法不依赖于任何数据类型,就直接调用 ---- 内置函数 和 自定义函数
l = [1,2,3,4]   # l 列表句柄
l.append()

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

id() # 一个变量所在的内存地址
hash - 对于相同可以hash数据的hash值在一次程序的执行过程中,总是不变的
     - 字典的寻址方式
print(hash(12345)) # 能不能哈希就是看能不能执行hash函数
print(hash('asdfghjkl'))
print(hash('asdfghjkl'))
print(hash('asdfghjkl'))
print(hash('asdfghjkl'))
print(hash(('1', 'aaa')))
print(hash([]))

对于相同可以hash数据的hash值在一次程序的执行过程中,总是不变的

字典的key可hash,将hash后的key值对应一个内存地址,存放value

ret = input('input here:>>>')
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)
    # 第一个'*'是星号,第二个 * 是乘号
    # /r : 作用: 回到行首,重新运行
    print(per_str, end='', flush=True)
progress Bar,进度条函数

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

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

compile 编字符代码为机械码 :exec流程类,eval计算类,single交互类
code1 = 'for i in range(10): print(i)'
compile1 = compile(code1, '', 'exec')
exec(compile1)

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

复数 ------ complex 复合的数 5 + 12j [j是虚数]

进制转换 bin oct hex

绝对值
print(abs(-5))

divmod 除数(求商),余数
print(divmod(7,3))

round 精确,保留小数位数
re = round(3.14159, 2)
print(re)

pow 幂运算
print(pow(2, 3))   # 幂运算,2的3次方 == 2**3
print(pow(3, 2, 2))   # 幂运算后取余,3的2次方后对2取余

sum(iterable,start)
ret = sum([1,2,3,4,5])
print(ret)

min ; max 可以指定一个判断条件
min(1,2,3,-5) # return -5
min(1,2,3,-5,key = abs) # return 1;因为此时的判断依据设成了 abs 绝对值
 

 

posted @ 2019-03-19 23:42  云川望雨  阅读(190)  评论(0编辑  收藏  举报