内置函数

 

# 内置函数
# print([1, 2, 3].__len__())
# print(len([1, 2, 3]))

# 迭代相关函数
#迭代器.__next__()
#next(迭代器)

# 迭代器 = iter(可迭代对象)
# 迭代器 = 可迭代对象.__iter__

# range(10)

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

# help
# help(str)


# callable() 查看变量是否能被调用
# print(callable(print)) True
# print(callable(a)) False

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

# import time
# time = __import__('time')

#文件相关
# open()

#内存相关
# id()
# hash()

# 输入输出
# input()
# print('abc\n') 每次print自带一个换行符
# print('abc\n', end='') 默认end='\n' 指定输出的结束符
# print(1,2,3,4,5, sep='|') 默认sep=' '   指定输出多个值之间的分隔符
'''
'aaa'打印到文件中而非打印到屏幕中
f = open('file', 'w')
print('aaa', file=f)
f.close()
'''

import time
for i in range(0, 101, 2):
    time.sleep(0.03)
    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)
# flush 立即执行

#字符串类型代码的执行
exec('print(123)')
eval('print(123)')
print(eval('1+2+3+4'))  #eval有返回值
print(exec('1+2+3+4'))  #exec没有返回值,其他相同
#exec和eval都可以执行字符串类型的代码
#但eval有返回值,exec没有返回值
#eval只能用在明确知道执行的代码是什么,安全问题
# eval————适合处理有结果的简单计算
# exec————适合处理简单流程控制

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

code3 = 'name = input("input your name:")'
compile3 = compile(code3, '', 'single')
exec(compile3)
print(name)

# 参数1:code;
# 参数2:filename,代码文件名,当传入了scource参数是,filename传入空字符即可
# 参数3:使用什么方法执行,single是交互类
exec(compile1)
compile 可以实现一次编译一段字符串形式的语句为字节码,之后可以多次执行,提高效率
'''
code3 = 'name = input("input your name:")'
compile3 = compile(code3, '', 'single')
exec(compile3)
print(name)

#数据类型 数据类型转换用
# complex 复数    实数+虚数
# bool
# int
# float

#进制转换
#bin 二进制    0b
#oct 八进制    0o
#hex 十六进制   0x

# 数学运算
# abs 绝对值
# divmode 除余 divmod(7,2) 返回 3, 1       做分页用
# round(3.14159, 2) 返回3.14
#pow(2,3)   2的3次方
#pow(2,3,3) 2的3次方取余 8/3余2
#sum    接收可迭代的  sum([1,2,3], 10) 第一个参数是可迭代对象,第二个参数是起始值
#min    min(1,2,3,4)    min([1,2,3,4])  即可放*args,也可放iterable
        # min(1,2,3,-4,key=abs)    取绝对值判断最小值
#max

 

posted @ 2020-04-02 09:55  vivagoal  阅读(95)  评论(0)    收藏  举报