08常见内置函数
1,exec 執(zhí)行字符串類型的代碼
s = "print ('你好啊,我是賽利亞')" exec (s)你好啊,我是賽利亞
exec ("a = 20") print (a)20
2,eval 執(zhí)行字符串類型的代碼,并返回最終結(jié)果
s = "1+1" eval (s)2
s = "[1,2,3,4]" ret = eval(s) # 可以把一個(gè)字符串代碼執(zhí)行,通常用來還原列表或者字典 print (ret) print (type(ret))[1, 2, 3, 4]
<class ‘list’>
3,input輸入
aa = input("請輸入你的內(nèi)容:") print (aa)請輸入你的內(nèi)容:好好學(xué)習(xí),天天向上
好好學(xué)習(xí),天天向上
4,print輸出
i love python
5,內(nèi)存id
2414673903408
2414674063664
6,hash值
135942013824091963
7,文件操作 open
r:只讀 read
w: 只寫 write
a : 追加寫 append
#相對路徑:相對于當(dāng)前程序所在的文件夾
#絕對路徑: c:/work/123.txt
mode :
r+ 讀寫操作
w+ 寫讀
a+ 追加寫讀
**8,模塊相關(guān) __ import __
from matplotlib import pyplot as plt #as 把pyplot修改為plt import requests9,幫助 help**
help (print)Help on built-in function print in module builtins:
print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
10, callable 調(diào)用相關(guān)
a = 10 def func():pass print (callable(func))#判斷能否調(diào)用 print (callable(a))True
False
[‘add’, ‘class’, ‘contains’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘getnewargs’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘mod’, ‘mul’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘rmod’, ‘rmul’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘capitalize’, ‘casefold’, ‘center’, ‘count’, ‘encode’, ‘endswith’, ‘expandtabs’, ‘find’, ‘format’, ‘format_map’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isascii’, ‘isdecimal’, ‘isdigit’, ‘isidentifier’, ‘islower’, ‘isnumeric’, ‘isprintable’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘maketrans’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’, ‘title’, ‘translate’, ‘upper’, ‘zfill’]
[‘add’, ‘class’, ‘contains’, ‘delattr’, ‘delitem’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘gt’, ‘hash’, ‘iadd’, ‘imul’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘mul’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘reversed’, ‘rmul’, ‘setattr’, ‘setitem’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘a(chǎn)ppend’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]
12,數(shù)據(jù)類型 bool值 ,int ,float
13,進(jìn)制轉(zhuǎn)換
0x64
0o144
0b1100100
100
14,數(shù)學(xué)運(yùn)算 abs , divmod , round , pow , sum , min , max
15,序列
列表和元組:list , tuple
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
s = slice (1,4)#切片 ss = "love" print (ss[s])ove
16,字符串 str , format , bytes , bytearry , memoryview , ord , chr , ascii , repr
33883
葛
驗(yàn)證碼
import random print (chr(random.randint(ord(“a”),old(“z”))))
17,數(shù)據(jù)集合 dict , set
18,相關(guān)內(nèi)置函數(shù) len , sortend , enumerate , all ,any ,zip , fiter , map
(0, ‘呵呵’)
(1, ‘哈哈’)
(2, ‘嚯嚯’)
[(‘a(chǎn)’, 1, ‘一’), (‘b’, 2, ‘二’), (‘c’, 3, ‘三’), (‘d’, 4, ‘四’)]
print (any([1,0,True,[],False])) #or print (all([1,0,True,[],False]))# andTrue
False
總結(jié)
- 上一篇: 06列表的常用基本操作
- 下一篇: 01变量常量注释