Python进阶-VII 内置函数

一、内置函数引入

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

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

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

内置函数总共有68个.

二、内置函数的分类及其对应功能

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个)
 1 print(sum([1,2,3]))  #sum(iterable,start)只接收可迭代的数据类型
 2 
 3 print(max([1,2,3])) #max(iterable,key,default) 或者 max(*args,key,default)  key是函数名,如abs
 4 print(min([1,2,3])) #min(iterable,key,default) 或者 min(*args,key,default)
 5 
 6 print(abs(-3)) #取绝对值
 7 
 8 print(pow(2,3))   #pow幂运算  == 2**3
 9 print(pow(2,3,3))#2的3次幂,然后取余
10 
11 print(divmod(7,2))   # div出发 mod取余
12 print(divmod(9,5))   # 除余
13 print(round(3.14159,3)) #小数位精确

5、序列(13)

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

3、其他(12个)

1)、字符串类型代码的行为:eval、exec、compile
 1 eval('print(123)')
 2 exec('print(123)')
 3 print(eval('1+2+3+4'))   # 有返回值
 4 print(exec('1+2+3+4'))   #没有返回值
 5 # exec和eval都可以执行 字符串类型的代码
 6 # eval有返回值  —— 有结果的简单计算
 7 # exec没有返回值   —— 简单流程控制
 8 # eval只能用在你明确知道你要执行的代码是什么
 9 
10 code = '''for i in range(10):
11     print(i*'*')
12 '''
13 exec(code)
14 
15 #compile是编译字符串类型的代码,适合一次编译多次执行的场景,节约时间!
16 code1 = 'for i in range(0,10): print (i)'
17 compile1 = compile(code1,'','exec')
18 exec(compile1)
19 
20 code2 = '1 + 2 + 3 + 4'
21 compile2 = compile(code2,'','eval')
22 print(eval(compile2))
23 
24 code3 = 'name = input("please input your name:")'
25 compile3 = compile(code3,'','single')
26 exec(compile3) #执行时显示交互命令,提示输入
27 print(name)
28 name #执行后name变量有值
29 "'pythoner'"
2)、输入输出
 1 print()
 2 print('我们的祖国是花园',end='')  #指定输出的结束符
 3 print('我们的祖国是花园',end='')
 4 print(1,2,3,4,5,sep='|') #指定输出多个值之间的分隔符
 5 #print综合应用:打印进度条
 6 import time
 7 for i in range(0,101,2):
 8      time.sleep(0.1)
 9      char_num = i//2
10      per_str = '\r%s%% : %s\n' % (i, '*' * char_num) \
11          if i == 100 else '\r%s%% : %s' % (i,'*'*char_num)
12      print(per_str,end='', flush=True)
13 #progress Bar
14 
15 input()
16 # ret = input('提示 : ')
17 # print(ret)
3)、内存相关
1 id()   #取变量的内存地址
2 hash() #对于相同可hash数据的hash值在一次程序的执行过程中总是不变的,只对可hash的变量进行取值,即不可变数据类型的
3 #   字典的寻址方式就是用hash,因为key是唯一的,它对应一个内存地址,所以寻找速度快!
4)、帮助
help('')
5)、调用相关
allable() #是否能被调用,返回True或False
# 某个方法属于某个数据类型的变量,就用.调用
# 如果某个方法不依赖于任何数据类型,就直接调用 —— 内置函数 和 自定义函数

6)、模块相关
1 #import time  导入或者叫引入模块
2 t = __import__('time')
3 print(t.time())
7)查看内置属性
 1 dir() #查看一个变量拥有的方法 

4、作用域

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

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

扫描二维码关注公众号,回复: 8026798 查看本文章

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

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

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

猜你喜欢

转载自www.cnblogs.com/funyou/p/11962179.html
今日推荐