Python的常用内置函数介绍

时间:2023-03-08 15:38:53

                  Python的常用内置函数介绍

                                  作者:尹正杰

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

一.取绝对值(abs)

 #!/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:y1053419035@qq.com print(abs(-9)) 以上代码执行结果如下: print(abs(-9))

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

 #!/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:y1053419035@qq.com print(all([])) #传入的必须是一个列表
print(all([1,3,4]))
print(all([0,1,2]))
print(all([1,3,None]))
print(all([1,""]))
print(all(i for i in range(1,3))) #当然传入一个列表生成器也是可以的
print(all([i for i in range(1,3)])) #和上面一行是等效的,Python会自动帮助列表生成器补充“[]” #以上代码执行结果如下:
True
True
False
False
False
True
True

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

 #!/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:y1053419035@qq.com print(any(""))
print(any([0,"",()]))
print(any([0,1])) #以上代码执行结果如下:
False
False
True

四.二进制转换(bin)

 #!/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:y1053419035@qq.com print(bin(3))
print(bin(7))
print(bin(20)) 以上代码执行结果如下:
0b11
0b111
0b10100

五.八进制转换(oct)

 #!/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:y1053419035@qq.com print(oct(9))
print(oct(21)) #以上代码执行结果如下:
0o11
0o25

六.十六进制转换(hex)

 #!/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:y1053419035@qq.com print(hex(7))
print(hex(14))
print(hex(12))
print(hex(21)) #以上代码执行结果如下:
0x7
0xe
0xc
0x15

七.布尔运算(bool)

 #!/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:y1053419035@qq.com print(bool(0))
print(bool(None))
print(bool(""))
print(bool("yinzhengjie")) #以上代码执行结果如下:
False
False
False
True

八.字符串转换(bytes)

 #!/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:y1053419035@qq.com Name = "yinzhengjie"
print(Name)
print(bytes(Name,encoding="utf-8")) #将字符串"yinzhengjie"转换成“utf-8”编码的字节
print(Name.encode("utf-8")) #这种方式和上面的执行小伙一样 #以上代码执行结果如下:
yinzhengjie
b'yinzhengjie'
b'yinzhengjie'

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

 #!/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:y1053419035@qq.com def PersonalIntroduction(Name):
print("Hello! My name is %s"% Name) print(callable(PersonalIntroduction)) #判断“PersonalIntroduction”这个函数是否可以被调用 #以上代码执行结果如下:
True

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

 #!/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:y1053419035@qq.com print(chr(81)) #将ASCII编码表中的数字编号对应的字母打印出来
print(chr(66)) #以上代码执行结果如下:
Q
B

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

 #!/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:y1053419035@qq.com print(ord("A")) #与chr内置函数相反,是将字母对应在ASCII编码表中的数字找出来。
print(ord("a")) #以上代码执行结果如下:
65
97

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

 #!/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:y1053419035@qq.com X = 1 + 2j #等效于"X = complex(1 + 2j)" Y = 3 - 2j
print(X.real) #实数
print(X.imag) #虚数 print(Y.real)
print(Y.imag) #以上代码值解析结果如下:
1.0
2.0
3.0
-2.0

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

 #!/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:y1053419035@qq.com import os print(dir(os)) #查看一个对象有哪些方法 #以上代码执行结果如下:
['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)

 #!/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:y1053419035@qq.com def yzj():
"Add by yinzhengjie"
pass print(help(yzj)) #查看函数的帮助信息 #以上代码执行结果如下:
Help on function yzj in module __main__: yzj()
Add by yinzhengjie None

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

 #!/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:y1053419035@qq.com TotalCount = 10
PerCount = 3
res = divmod(TotalCount,PerCount) #可以用于分页的案例操作
if res[1] > 0:
page = res[0] + 1 print(page) #以上代码执行结果如下:
4

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

 #!/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:y1053419035@qq.com array = ["yinzhengjie",""]
for i in enumerate(array):
print(i) #以上代码执行结果如下:
(0, 'yinzhengjie')
(1, '')

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

 #!/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:y1053419035@qq.com s = frozenset({1,2,3}) #设置不可变集合
print(s) s2 = set([100,200,300])
s2.add(400)
s2.pop()
print(s2) #以上代码执行结果如下:
frozenset({1, 2, 3})
{100, 400, 300}

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

 #!/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:y1053419035@qq.com print(globals()) #查看全局变量;
print(locals()) #查看当前作用域的局部变量;
print(globals() is locals()) #由于改行代码在全局作用域写的,全局作用域的变量就是本地变量; #以上代码执行结果如下:
{'__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}
{'__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}
True

十九.计算hash值

 #!/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:y1053419035@qq.com s1 = "yinzhengjie"
s2 = "yinzhengjie" print(hash(s1))
print(hash(s2)) #以上代码执行结果如下:
-578215773
-578215773

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

 #!/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:y1053419035@qq.com Name = "尹正杰"
print(type(Name))
print(isinstance(Name,str)) #以上代码执行结果如下:
<class 'str'>
True

二十一.取最大值(max)

 #!/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:y1053419035@qq.com print(max([100,200,300])) #取最大值
print(max((3,5,7)))
print(
max(
i for i in range(10)
)
) #以上代码执行结果如下:
300
7
9

二十二.算数运算(pow)

 #!/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:y1053419035@qq.com print(pow(3,2)) #计算3的2次方的值
print(pow(3,2,2)) #计算3的2次方在于相除取余数 #以上代码执行结果如下:
9
1

二十三.range用法

 #!/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:y1053419035@qq.com for i in range(0,5):
print(i) for i in range(0,5,2):
print(i) for j in range(-5,0):
print(j) for j in range(5,0,-1):
print(j) #以上代码执行结果如下:
0
1
2
3
4
0
2
4
-5
-4
-3
-2
-1
5
4
3
2
1

二十四.列表反转(reversed)

 #!/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:y1053419035@qq.com list_1 = ["a1","a2","a3","a4","a5","a6","a7","a8","a9"]
print(list_1[2:5:2])
print(list_1[:])
print(list_1[::2])
print(list_1[::-1]) print(list(reversed(list_1))) #将列表进行反转,和“print(list_1[::-1])”功能一样 #以上代码执行结果如下:
['a3', 'a5']
['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']
['a1', 'a3', 'a5', 'a7', 'a9']
['a9', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1']
['a9', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1']

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

 #!/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:y1053419035@qq.com print(round(6.5568321,3)) #表示对“6.5568321”进行四舍五入保留三位小数点 #以上代码执行结果如下:
6.557

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

 #!/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:y1053419035@qq.com l = ["a1","a2","a3","a4","a5","a6","a7","a8","a9"]
print(l[2:5:2]) x = slice(2,5,2)
print(l[x]) #以上代码执行结果如下:
['a3', 'a5']
['a3', 'a5']

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

 #!/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:y1053419035@qq.com s1 = (i for i in range(101))
s2 = [100,-200,300]
print(sum(s1)) #只能计算int类型的数字之和。
print(sum(s2)) #以上代码执行结果如下:
5050
200

二十八.拉链函数(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:y1053419035@qq.com s1 = "yinzhengjie"
s2 = "" for i in zip(s1,s2):
print(i) #以上代码执行结果如下:
('y', '')
('i', '')
('n', '')
('z', '')
('h', '')
('e', '')
('n', '')
('g', '')
('j', '')