Python的常用內建函式介紹

weixin_33981932發表於2018-02-28

                  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')

 

相關文章