day 14 推导式 内置函数

我们之前已经学习了一部分内置函数了,现在我们进行回忆:

len() count() print() max() min() dir() list() eval() open() str() isinstance() format() type() iter() input() int() set() frozenset()...

现在我们学习68种内置函数...

其他相关:

***1、eval 执行字符串类型的代码,并返回最终结果

 

1 print(eval('3+4'))
2 ret = eval('{"name":"老男孩"}')
3 print(ret, type(ret))
View Code

 

***2、exec:执行字符串类型的代码,流程语句。

1 ret = '''
2 li = [1, 2, 3]
3 for i in li:
4     print(i)
5 '''
6 print(exec('3+4'))
7 print(exec(ret))
View Code

3、compile:将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。

1 code1 = 'for i in range(0,10) :print(i)'
2 compile1 = compile(code1,'','exec')
3 exec (compile1)
View Code

***4、print 

1 print(self, *args, sep=' ',end='\n', file = None)
2 print(333,end = '')
3 print(666)
4 print(111,222,333,sep='|')
5 with open ('log',encoding = 'utf-8',mode = 'a') as f1:
6 print('5555',file=f1)
View Code

*5、hash 哈希

1 print(hash(12322))   #数字不变
2 print(hash('123'))
3 print(hash('arg'))
4 print(hash('alex'))
5 print(hash(True))
6 print(hash(Flase))
7 print(hash((1,2,3)))
View Code

*6、help 函数用于查看函数或模块用途的详细说明。

1 print(help(list))
View Code

***7、callable 函数用于检查一个对象是否可调用的。

  如果返回True,object仍然可能调用失败,但如果返回False,调用object绝对不会成功。

1 def func1():
2     print(555)
3 a=3
4 f= func1
5 print(callable(f))
6 print(callable(a))
View Code

***8、dir

1 print(dir(list))
View Code

*9、next

 1 #首先获得Iterator对象:
 2 it = iter([1, 2, 3, 4, 5])
 3 #循环:
 4 while True:
 5     try:
 6         #获得下一个值:
 7         x = next(it)
 8         print(x)
 9     except StopIteration:
10         #遇到StopIteration就退出循环
11         break
View Code

*10、iter()

*11、int

1 print(int()) # 0
2 print(int('12')) #12
3 print(int(3.4)) #3
4 print(int('0100',base = 2)) #将2进制的0100转化成十进制,结果为4
5 print(type(3.14)) #float
6 print(float(3))
View Code

12、complex: 函数用于创建一个值为real + imag*j的复数或者转化一个字符串或数为复数。

  如果第一个参数为字符串,则不需要指定第二个参数

*13~15、bin 将十进制转换为二进制并返回。

    oct 将十进制转换成八进制字符串并返回

    hex 将十进制转换成十六进制字符串并返回

1 print(bin(5))   #0b101
2 print(oct(7))   #0o7
3 print(oct(0))   #0o11
4 print(hex(10))#0xa
5 print(hex(15))#0xf
6 print(hex(19))#0x13
View Code

*16、abs 函数返回数字的绝对值

1 print(abs(-20))
View Code

*17、divmod 计算除数与被除数的结果,返回一个包含商和余数的元组(a//b,a%b)。

1 print(divmod(11,3))   #(3,1)
View Code

*18、round 保留浮点数的小数位数,默认保留整数

1 print(round(3.1415))  #3
2 print(round(3.1415,3)) #3.141
View Code

*19、pow 求x**y次幂。(三个参数为x**y的结果对z取余)

1 print(pow(2,3))   #8
2 print(pow(2,3,5))# 3
View Code

***20、sum 对可迭代对象进行求和计算,可设置初始值

1 print(sum([1,2,3]))
2 print(sum([1,2,3],100))
View Code
***21、max 返回可迭代对象的最大值(可加key,key为函数名,通过函数的规则,返回最大值)
 
1 print(max([1,2,3]))
2 ret = max([1,2,-5],key = abs)
3 print(ret)
View Code

***22、min 返回可迭代对象的最小值  (可加key,key为函数名,通过函数的规则,返回最小值)

1 ret = min ([1,2,-5],key = abs)
2 print(ret)
View Code

***23、reversed:将一个序列翻转,并返回此翻转序列的迭代器。

1 ite = reversed(['a',2,3,'c',4,2])
2 print(ite)
3 for i in ite:
4     print(i)
View Code

23、构造一个切片对象,用于列表的切片。

1 li = ['a','b','c','d','e','f','g']
2 l2 = ['a','b','c',1,2,3,4,54]
3 sli_obj = slice(3)
4 print(li[sli_obj])
5 print(l2[sli_obj])
View Code

***24、format 用于格式化输出,很重要,左对齐,右对齐,居中

1 s1 = format('test', '<30')
2 print(format('test','<30')) #左对齐
3 print(format('test','>20')) #右对齐
4 print(format('test','^20')) #居中
5 print(len(s1))    #总长度30,左对齐
format
25、bytes: 只能编码,将unicode---->非unicode bytes(s1,encoding = 'utf-8")

 

1 s1 = '老男孩'
2 # s2 = s1.encode('utf-8')
3 # s22 = bytes(s1,encoding='utf-8')
4 # print(s2)
5 # print(s2.decode('utf-8'))
View Code

26、bytearry: 返回一个新新字节数组。这个数组里的元素是可变的,并且每个元素的值的范围:0<= x<256.

1 ret = bytearray('alex',encoding='utf-8')  # [65,108,101,120]
2 print(id(ret))
3 print(ret)
4 print(ret[0])  # 97
5 ret[0] = 65
6 print(ret)
View Code

27、memoryview

1 ret = memoryview(bytes('你好',encoding='utf-8'))
2 print(len(ret))  # 6 utf-8的bytes类型,放在一个list中 [\xe4,\xbd,\xa0,\xe5,\xa5,\xbd]
3 print(ret)
4 print(bytes(ret[:3]).decode('utf-8'))
5 print(bytes(ret[3:]).decode('utf-8'))
6 print('你好'.encode('utf-8'))
View Code

*28-30

ord:输入字符找该字符编码的位置 unicode
chr:输入位置数字找出其对应的字符 unicode
ascii:是ascii码中的返回该值,不是就返回
1 print(ord('a'))
2 print(ord(''))
3 print(chr(98))
4 print(chr(20013))
5 
6 print(ascii('a'))
7 print(ascii(''))  # '\u4e2d'
View Code
%r  原封不动的写出来
name = 'taibai'
print('我叫%r' % name)
*** 31、repr 原形毕露
1 print(repr('{"name":"alex"}'))
2 print('{"name":"alex"}')
View Code
***32、 sorted  排序,可加key
1 li = [1,-2,-7,8,5,-4,3]
2 print(sorted(li,reverse=True,key=abs))
3 
4 L = [('a', 1), ('c', 3), ('d', 4),('b', 2), ]
5 sorted(L, key=lambda x:x[1])               # 利用key
View Code

*** 33、enumerate:枚举,返回一个枚举对象。 (0, seq[0]), (1, seq[1]), (2, seq[2]),

1 li = ['老男孩', 'alex', 'wusir', '嫂子', 'taibai']
2 print(enumerate(li))
3 print('__iter__' in dir(enumerate(li)))
4 print('__next__' in dir(enumerate(li)))
5 
6 for k,v in enumerate(li):
7     print(k,v)
8 for k,v in enumerate(li,1):
9     print(k,v)
View Code

*33-34、all 可迭代对象中,全都是True才是True       any 可迭代对象中,有一个True 就是True

1 print(all([1,2,True,0]))
2 print(any([1,'',0]))
View Code

***35、zip 拉链方法 形成元组的个数与最短的可迭代对象的长度一样,然后......

1 l1 = [1, 2, 3, 4]
2 l2 = ['a', 'b', 'c', 5]
3 l3 = ('*', '**', (1,2,3), 777)
4 l4= ('0', '99', (1,2,3), 777)
5 print('__iter__' in dir(zip(l1,l2,l3,l4)))
6 print('__next__' in dir(zip(l1,l2,l3,l4)))
7 for i in zip(l1,l2,l3,l4):
8     print(i)
View Code

36、filter 过滤 通过你的函数,过滤一个可迭代对象,返回的是True ,类似于[i for i in range(10) if i > 3]

1 ef func(x):
2     return x % 2 == 0
3 ret = filter(func,[1,2,3,4,5,6,7])
4 print(ret)
5 for i in ret:
6     print(i)
View Code

37、square  计算平方数

1 def square(x) :            # 计算平方数
2     return x ** 2
3 print(map(square,[1,2,3,4,5]))
4 for i in map(square,[1,2,3,4,5]):
5     print(i)
View Code

 

 

 







posted @ 2018-04-07 21:20  大白1#  阅读(46)  评论(0)    收藏  举报