Python的常用内置函数介绍

2018/02/28 23:33
阅读数 21

                  Python的常用内置函数介绍

                                  作者:尹正杰

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

 

 

 

一.取绝对值(abs)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(abs(-9))
 8 
 9 
10 以上代码执行结果如下:
11 
12 print(abs(-9))

二.布尔运算and运算(all)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(all([]))                          #传入的必须是一个列表
 8 print(all([1,3,4]))
 9 print(all([0,1,2]))
10 print(all([1,3,None]))
11 print(all([1,""]))
12 print(all(i for i in range(1,3)))       #当然传入一个列表生成器也是可以的
13 print(all([i for i in range(1,3)]))     #和上面一行是等效的,Python会自动帮助列表生成器补充“[]”
14 
15 
16 #以上代码执行结果如下:
17 True
18 True
19 False
20 False
21 False
22 True
23 True

三.布尔运算or运算(any)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(any(""))
 8 print(any([0,"",()]))
 9 print(any([0,1]))
10 
11 #以上代码执行结果如下:
12 False
13 False
14 True

四.二进制转换(bin)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(bin(3))
 8 print(bin(7))
 9 print(bin(20))
10 
11 
12 以上代码执行结果如下:
13 0b11
14 0b111
15 0b10100

五.八进制转换(oct)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(oct(9))
 8 print(oct(21))
 9 
10 
11 
12 #以上代码执行结果如下:
13 0o11
14 0o25

六.十六进制转换(hex)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(hex(7))
 8 print(hex(14))
 9 print(hex(12))
10 print(hex(21))
11 
12 
13 
14 #以上代码执行结果如下:
15 0x7
16 0xe
17 0xc
18 0x15

七.布尔运算(bool)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(bool(0))
 8 print(bool(None))
 9 print(bool(""))
10 print(bool("yinzhengjie"))
11 
12 
13 
14 #以上代码执行结果如下:
15 False
16 False
17 False
18 True

八.字符串转换(bytes)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 Name = "yinzhengjie"
 9 print(Name)
10 print(bytes(Name,encoding="utf-8"))  #将字符串"yinzhengjie"转换成“utf-8”编码的字节
11 print(Name.encode("utf-8"))         #这种方式和上面的执行小伙一样
12 
13 
14 
15 #以上代码执行结果如下:
16 yinzhengjie
17 b'yinzhengjie'
18 b'yinzhengjie'

九.判断函数是否可以被调用(callable)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 def PersonalIntroduction(Name):
 9     print("Hello! My name is %s"% Name)
10 
11 print(callable(PersonalIntroduction))  #判断“PersonalIntroduction”这个函数是否可以被调用
12 
13 
14 
15 #以上代码执行结果如下:
16 True

十.将ASCII编码表正解(chr)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 print(chr(81))      #将ASCII编码表中的数字编号对应的字母打印出来
 9 print(chr(66))
10 
11 
12 #以上代码执行结果如下:
13 Q
14 B

十一.将ASCII编码表反解(ord)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 print(ord("A"))     #与chr内置函数相反,是将字母对应在ASCII编码表中的数字找出来。
 9 print(ord("a"))
10 
11 
12 #以上代码执行结果如下:
13 65
14 97

十二.实数与虚数的判断(complex)

 

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 X = 1 + 2j  #等效于"X = complex(1 + 2j)"
 9 
10 Y = 3 - 2j
11 print(X.real)   #实数
12 print(X.imag)   #虚数
13 
14 print(Y.real)
15 print(Y.imag)
16 
17 
18 #以上代码值解析结果如下:
19 1.0
20 2.0
21 3.0
22 -2.0

十三.查看一个对象拥有哪些可以调用的方法(dir)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 import os
 9 
10 print(dir(os))      #查看一个对象有哪些方法
11 
12 
13 
14 
15 #以上代码执行结果如下:
16 ['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

十四.查看对象的帮助信息(help)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 def yzj():
 9     "Add by yinzhengjie"
10     pass
11 
12 print(help(yzj))     #查看函数的帮助信息
13 
14 
15 
16 
17 #以上代码执行结果如下:
18 Help on function yzj in module __main__:
19 
20 yzj()
21     Add by yinzhengjie
22 
23 None

十五.取商和余数(divmod(10,3))

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 TotalCount = 10
 9 PerCount = 3
10 res = divmod(TotalCount,PerCount)   #可以用于分页的案例操作
11 if res[1] > 0:
12     page = res[0] + 1
13 
14 print(page)
15 
16 
17 #以上代码执行结果如下:
18 4

十六.给可迭代对象添加序号(enumerate)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 array = ["yinzhengjie","18"]
 8 for i in enumerate(array):
 9     print(i)
10 
11 
12 
13 #以上代码执行结果如下:
14 (0, 'yinzhengjie')
15 (1, '18')

十七.设置不可变集合(frozenset

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 s = frozenset({1,2,3})      #设置不可变集合
 8 print(s)
 9 
10 
11 s2 = set([100,200,300])
12 s2.add(400)
13 s2.pop()
14 print(s2)
15 
16 
17 #以上代码执行结果如下:
18 frozenset({1, 2, 3})
19 {100, 400, 300}

十八.全局变量(globals)与局部变量(locals)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(globals())                    #查看全局变量;
 8 print(locals())                     #查看当前作用域的局部变量;
 9 print(globals() is locals())        #由于改行代码在全局作用域写的,全局作用域的变量就是本地变量;
10 
11 
12 
13 #以上代码执行结果如下:
14 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x006E65B0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/Code/pycharm/文件存放处/python学习笔记/DAY8/1.内置函数.py', '__cached__': None}
15 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x006E65B0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/Code/pycharm/文件存放处/python学习笔记/DAY8/1.内置函数.py', '__cached__': None}
16 True

十九.计算hash值

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 s1 = "yinzhengjie"
 8 s2 = "yinzhengjie"
 9 
10 print(hash(s1))
11 print(hash(s2))
12 
13 
14 
15 #以上代码执行结果如下:
16 -578215773
17 -578215773

二十.判断数据类型(isinstance)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 Name = "尹正杰"
 8 print(type(Name))
 9 print(isinstance(Name,str))
10 
11 
12 
13 #以上代码执行结果如下:
14 <class 'str'>
15 True

二十一.取最大值(max)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(max([100,200,300]))   #取最大值
 8 print(max((3,5,7)))
 9 print(
10     max(
11         i for i in range(10)
12     )
13 )
14 
15 
16 #以上代码执行结果如下:
17 300
18 7
19 9

二十二.算数运算(pow)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(pow(3,2))     #计算3的2次方的值
 8 print(pow(3,2,2))   #计算3的2次方在于相除取余数
 9 
10 
11 
12 
13 #以上代码执行结果如下:
14 9
15 1

二十三.range用法

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 for i in range(0,5):
 8     print(i)
 9     
10 for i in range(0,5,2):
11     print(i)
12 
13 for j in range(-5,0):
14     print(j)
15 
16 for j in range(5,0,-1):
17     print(j)
18 
19 
20 
21 #以上代码执行结果如下:
22 0
23 1
24 2
25 3
26 4
27 0
28 2
29 4
30 -5
31 -4
32 -3
33 -2
34 -1
35 5
36 4
37 3
38 2
39 1

二十四.列表反转(reversed)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 list_1 = ["a1","a2","a3","a4","a5","a6","a7","a8","a9"]
 8 print(list_1[2:5:2])
 9 print(list_1[:])
10 print(list_1[::2])
11 print(list_1[::-1])
12 
13 print(list(reversed(list_1)))   #将列表进行反转,和“print(list_1[::-1])”功能一样
14 
15 
16 
17 #以上代码执行结果如下:
18 ['a3', 'a5']
19 ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']
20 ['a1', 'a3', 'a5', 'a7', 'a9']
21 ['a9', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1']
22 ['a9', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1']

二十五.四舍五入运算(round)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 print(round(6.5568321,3))   #表示对“6.5568321”进行四舍五入保留三位小数点
 8 
 9 
10 
11 
12 #以上代码执行结果如下:
13 6.557

二十六.取切片操作(slice)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 l = ["a1","a2","a3","a4","a5","a6","a7","a8","a9"]
 8 print(l[2:5:2])
 9 
10 x = slice(2,5,2)
11 print(l[x])
12 
13 
14 
15 #以上代码执行结果如下:
16 ['a3', 'a5']
17 ['a3', 'a5']

二十七.计算int类型的之和(sum)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 s1 = (i for i in  range(101))
 8 s2 = [100,-200,300]
 9 print(sum(s1))      #只能计算int类型的数字之和。
10 print(sum(s2))
11 
12 
13 
14 
15 #以上代码执行结果如下:
16 5050
17 200

二十八.拉链函数(zip)

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 s1 = "yinzhengjie"
 8 s2 = "123456789"
 9 
10 for i in zip(s1,s2):
11     print(i)
12 
13 
14 
15 #以上代码执行结果如下:
16 ('y', '1')
17 ('i', '2')
18 ('n', '3')
19 ('z', '4')
20 ('h', '5')
21 ('e', '6')
22 ('n', '7')
23 ('g', '8')
24 ('j', '9')

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部