Python进阶-VII 内置函数

一、内置函数引入

我们已经了解的有;
print()  input() range() next()  dir()
str() int()  list()  set()  tuple()  dict()
help()
print(help(''))

#print('_2'.isidentifier()) # 是否是标识符,变量名的命名规则适用如此

何为内置函数,python自带的函数,直接可调用!

内置函数总共有68个.

二、内置函数的分类及部分内置函数(55个)功能

1、文件操作(1个)

open()
f = open('03_exam')
f.writable()
f.readable()
f.close()
f = open('file','w')
print('aaaa',file=f)
f.close()
2、数据类型(4个)
bool()
int()
float()
complex()
补充知识:复数 —— complex
 实数 : 有理数
无理数
虚数 :虚无缥缈的数
5 + 12j === 复合的数 === 复数
6 + 15j 浮点数(有限循环小数,无限循环小数) != 小数 :有限循环小数,无限循环小数,无限不循环小数
浮点数
354.123 = 3.54123*10**2 = 35.4123 * 10
f = 1.781326913750135970
print(f)

3、进制转换(3个)
print(bin(10))  #二进制  0b
print(oct(10)) #八进制 0o
print(hex(10)) #十六进制 0x
4、数学运算(7个)
 print(sum([1,2,3]))  #sum(iterable,start)只接收可迭代的数据类型

 print(max([1,2,3])) #max(iterable,key,default) 或者 max(*args,key,default)  key是函数名,如abs
print(min([1,2,3])) #min(iterable,key,default) 或者 min(*args,key,default) print(abs(-3)) #取绝对值 print(pow(2,3)) #pow幂运算 == 2**3
print(pow(2,3,3))#2的3次幂,然后取余 print(divmod(7,2)) # div出发 mod取余
print(divmod(9,5)) # 除余
print(round(3.14159,3)) #小数位精确

5、序列(13)

 1)列表和元组
  list()
tuple() 
2)相关内置函数
  reversed(object) #参数:序列,返回值:反序迭代器
slice() 
 3)字符串
 str()
bytes() #bytes(s,enconding='utf-8')
repr() #用于%r格式化输出
format()#python3中的格式化输出
bytearray()#bytearray(s,enconding='utf-8')
memoryview()#memoryview('ILOVEU ',bytes(s,enconding='utf-8'))
ord() #字符按照unicode转数字
chr() #数字按unicode转字符
ascii()#只要是ascii码中的内容,就打印出来,否则就转换成\u

3、其他(12个)

1)、字符串类型代码的行为:eval、exec、compile
 eval('print(123)')
exec('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) #compile是编译字符串类型的代码,适合一次编译多次执行的场景,节约时间!
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'"
2)、输入输出
 print()
print('我们的祖国是花园',end='') #指定输出的结束符
print('我们的祖国是花园',end='')
print(1,2,3,4,5,sep='|') #指定输出多个值之间的分隔符
#print综合应用:打印进度条
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 input()
# ret = input('提示 : ')
# print(ret)
3)、内存相关
 id()   #取变量的内存地址
hash() #对于相同可hash数据的hash值在一次程序的执行过程中总是不变的,只对可hash的变量进行取值,即不可变数据类型的
# 字典的寻址方式就是用hash,因为key是唯一的,它对应一个内存地址,所以寻找速度快!
4)、帮助
help('')
5)、调用相关
allable() #是否能被调用,返回True或False
# 某个方法属于某个数据类型的变量,就用.调用
# 如果某个方法不依赖于任何数据类型,就直接调用 —— 内置函数 和 自定义函数
6)、模块相关
 #import time  导入或者叫引入模块
t = __import__('time')
print(t.time())
7)查看内置属性
  dir() #查看一个变量拥有的方法 

4、作用域

globals() #返回全局作用域中的所有名字

locals() #返回本地作用域中的所有名字

5、迭代器或生成器
 range() # 是可迭代的对象,可以进行切片,切片后转换为list后可以看到值

next()#在生成器或迭代器中取下一值的方法,它其实调用的是__next__方法

iter()#把可迭代的对象,变成迭代器,实际上盗用的是__iter__方法

上一篇:Python的常用内置函数介绍


下一篇:企业IT管理员IE11升级指南【2】—— Internet Explorer 11 对Adobe Flash的支持