Python入门篇-内建函数

              Python入门篇-内建函数

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.常见的内建函数案例 

1>.标识id

返回对象的唯一标识,CPython返回内存地址.

2>.哈希 hash()

返回一个对象的哈希值.

3>.类型 type()

返回对象的类型.

4>.类型转换

float(), int(), bin(), hex(), oct(), bool(), list(), tuple(), dict(), set(), complex(), bytes(), bytearray().

5>.打印print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

打印输出,默认使用空格分割、换行结尾,输出到控制台.

6>.对象长度len(s)

返回一个集合类型的元素个数.

7>.isinstance(obj, class_or_tuple)

判断对象obj是否属于某种类型或者元组中列出的某个类型

isinstance(True, int)
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


print(isinstance(False,int))

print(isinstance(False,(str,)))

print(isinstance(False,(int,)))

print(isinstance(False,(set,)))

print(isinstance(False,(bool,int,str)))



#以上代码执行结果如下:
True
False
True
False
True
isinstance使用案例

8>.issubclass(cls, class_or_tuple)

判断类型cls是否是某种类型的子类或元组中列出的某个类型的子类

issubclass(bool, int)
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

#判断bool是否是int的子类
print(issubclass(bool,int))


#判断str是否是int的子类
print(issubclass(str,int))



#以上代码执行结果如下:
True
False
issubclass使用案例

9>.绝对值abs(x) x为数值

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

#求10的绝对值
print(abs(10))

#求-20的绝对值
print(abs(-20))

#求-300的绝对值
print(abs(-300))



#以上代码执行结果如下:
10
20
300
abs(x)使用案例

10>.最大值max() 最小值min()

返回可迭代对象中最大或最小值

返回多个参数中最大或最小值
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


list_1 = [1,3,-5,7,9,100]

#求可迭代对象的最大值
print(max(list_1))

#求可迭代对象的最小值
print(min(list_1))

#以上代码执行结果如下:
100
-5
最大值max() 最小值min()使用案例

11>.round(x) 四舍六入五取偶,round(-0.5)

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


print(round(-0.5))
print(round(-1.5))
print(round(-1.3))
print(round(-1.6))


#以上代码执行结果如下:
0
-2
-1
-2
round使用案例

12>.pow(x , y) 等价于x**y

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

#求10的平方
print(pow(10,2))

#求5的三次方
print(pow(5,3))

#求2的8次方
print(pow(2,8))

#求2的32次方
print(pow(2,32))

#求2的64次方
print(pow(2,64))



#以上代码执行结果如下:
100
125
256
4294967296
18446744073709551616
pow使用案例

13>.range

range(stop) 从0开始到stop-1的可迭代对象;

range(start, stop[, step]) 从start开始到stop-1结束,步长为step的可迭代对象
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


for i in range(3):
    print("i = {}".format(i))

print("*" * 20 + "我是分割线" + "*" * 20)

for j in range(3,10):
    print("j = {}".format(j))

print("*" * 20 + "我是分割线" + "*" * 20)

for k in range(3,20,2):
    print("k = {}".format(k))




#以上代码执行结果如下:
i = 0
i = 1
i = 2
********************我是分割线********************
j = 3
j = 4
j = 5
j = 6
j = 7
j = 8
j = 9
********************我是分割线********************
k = 3
k = 5
k = 7
k = 9
k = 11
k = 13
k = 15
k = 17
k = 19
range使用案例

14>.divmod(x, y) 等价于tuple (x//y, x%y)

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


print(divmod(3,5))

print(3//5,3%5)


#以上代码执行结果如下:
(0, 3)
0 3
divmod使用案例

15>.sum(iterable[, start]) 对可迭代对象的所有数值元素求和

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

#
print(sum(range(1,100,2)))

#默认的起始数字为0
print(sum(range(1,100,2),0))

#指定起始数字位100
print(sum(range(1,100,2),100))



#以上代码执行结果如下:
2500
2500
2600
sum使用案例

16>.chr(i) 给一个一定范围的整数返回对应的字符

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

print(chr(97))
print(chr(20013))
print(chr(22269))



#以上代码执行结果如下:
a
中
国
chr使用案例

17>.ord(c) 返回字符对应的整数

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

print(ord('a'))
print(ord(''))
print(ord(''))



#以上代码执行结果如下:
97
20013
22269
ord使用案例

18>.sorted(iterable[, key][, reverse]) 排序

返回一个新的列表,默认升序

reverse是反转
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

print(sorted([1, 3, 5]))

print(sorted([1, 3, 5], reverse=True))

print(sorted({'c':1, 'b':2, 'a':1}))

#以上代码执行结果如下:
[1, 3, 5]
[5, 3, 1]
['a', 'b', 'c']
sorted使用案例

19>.翻转reversed(seq)

返回一个翻转元素的迭代器.
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


print(list(reversed("13579")))



print(next(reversed([1,3,5])))

for x in reversed(['c','b','a']):
    print(x)





#以上代码执行结果如下:
['9', '7', '5', '3', '1']
5
a
b
c
reversed使用案例

20>.枚举enumerate(seq, start=0)

迭代一个序列,返回索引数字和元素构成的二元组

start表示索引开始的数字,默认是0
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

for x in enumerate([2,4,6,8]):
    print(x)

for x in enumerate("abcde"):
    print(x,end=" ")


#以上代码执行结果如下:
(0, 2)
(1, 4)
(2, 6)
(3, 8)
(0, 'a') (1, 'b') (2, 'c') (3, 'd') (4, 'e') 
enumerate使用案例

21>.迭代器和取元素iter(iterable)、next(iterator[, default])

iter将一个可迭代对象封装成一个迭代器

next对一个迭代器取下一个元素。如果全部元素都取过了,再次next会抛StopIteration异常
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

it = iter(range(5))
print(next(it))

it = reversed([1,3,5])
print(next(it))


#以上代码执行结果如下:
0
5
iter使用案例

22>.拉链函数zip(*iterables)

像拉链一样,把多个可迭代对象合并在一起,返回一个迭代器

将每次从不同对象中取到的元素合并成一个元组
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


print(list(zip(range(10),range(10))))

print(list(zip(range(10),range(10),range(5),range(10))))

print(dict(zip(range(10),range(10))))

print({str(x):y for x,y in zip(range(10),range(10))})

list_1 = [1,3,5,7,9,11]
list_2 = [2,4,6,8]

list_3 = zip(list_1,list_2)
print("list_1 = {}".format(list_1))
print("list_2 = {}".format(list_2))
print("list_3 = {}".format(list_3))
for i in list_3:
    print(i)



#以上代码执行结果如下:
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
[(0, 0, 0, 0), (1, 1, 1, 1), (2, 2, 2, 2), (3, 3, 3, 3), (4, 4, 4, 4)]
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
list_1 = [1, 3, 5, 7, 9, 11]
list_2 = [2, 4, 6, 8]
list_3 = <zip object at 0x0000000002185F08>
(1, 2)
(3, 4)
(5, 6)
(7, 8)
zip使用案例

二.可迭代对象

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]

"""
可迭代对象特点:
    能够通过迭代一次次返回不同的元素的对象。所谓相同,不是指值是否相同,而是元素在容器中是否是同一个,例如列表中值可以重复的。
    可以迭代,但是未必有序,未必可索引
    可迭代对象有:list、tuple、string、bytes、bytearray、range、set、dict、生成器等
    可以使用成员操作符in、not in,in本质上就是在遍历对象
"""

print(3 in range(10))

print(3 in (x for x in range(10)))

print(3 in {x:y for x,y in zip(range(4),range(4,10))})


#以上代码执行结果如下:
True
True
True

三.迭代器

#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:[email protected]


"""
迭代器特点:
    特殊的对象,一定是可迭代对象,具备可迭代对象的特征
    通过iter方法把一个可迭代对象封装成迭代器
    通过next方法,迭代迭代器对象
    生成器对象,就是迭代器对象
"""

for x in iter(range(10)):
    print(x)

g = (x for x in range(10))
print(type(g))
print(next(g))
print(next(g))



#以上代码执行结果如下:
0
1
2
3
4
5
6
7
8
9
<class 'generator'>
0
1

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/10953102.html