Python基礎day-11[內置函數(未完),遞歸,匿名函數]
內置函數:
abs() : 返回數字的絕對值。參數可以是整數或浮點數,如果參數是復數,則返回復數的模。
print(abs(0.2)) print(abs(1)) print(abs(-4)) print(abs(-0.2)) print(abs(3+4j)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 0.2 1 4 0.2 5.0 Process finished with exit code 0
all():如果 iterable 的所有元素都為真(或者如果可叠代為空),則返回 True
。
l = [1,2,3,4,5,6,7]print(all(l)) l = [] print(all(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py True True Process finished with exit code 0
any():如果iterable的任何元素為真,則返回True。如果iterable為空,則返回False。
l = [1,2,3,4,5,6,7] print(any(l)) l = [] print(any(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py True False Process finished with exit code 0
ascii():返回一個可打印的對象字符串方式表示,如果是非ascii字符就會輸出\x,\u等字符來表示。
print(ascii(‘中文‘)) print(ascii(10), ascii(9000000), ascii(‘b\10‘), ascii(‘0x\001‘)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py ‘\u4e2d\u6587‘ 10 9000000 ‘b\x08‘ ‘0x\x01‘ Process finished with exit code 0
bin():將整數轉換為二進制字符串。
print(bin(10)) print(bin(77))print(bin(100)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 0b1010 0b1001101 0b1100100 Process finished with exit code 0
bool():返回布爾值,即True或False之一。
print(bool(0)) print(bool(None)) print(bool()) print(bool(1)) print(bool(‘abc‘)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py False False False True True Process finished with exit code 0
chr():輸入unicode代碼,返回對應的字符。
print(chr(25991)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 文 Process finished with exit code 0
ord():輸入一個字符,返回其unicode代碼。
print(ord(‘中‘)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 20013 Process finished with exit code 0
dict():定義一個新的字典。
d = dict({‘name‘:‘abc‘,‘age‘:123}) print(d) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py {‘name‘: ‘abc‘, ‘age‘: 123} Process finished with exit code 0
divmod():以兩個(非復數)數字作為參數,並在使用整數除法時返回由商和余數組成的一對數字。
print(divmod(73,23)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py (3, 4) Process finished with exit code 0
enumerate():給列表,元組等可索引類型加上序號。
l=[‘a‘,‘b‘,‘c‘] for i in enumerate(l): print(i) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py (0, ‘a‘) (1, ‘b‘) (2, ‘c‘) Process finished with exit code 0
eval():從文件中讀取字符串轉換成命令執行。
with open(r‘E:\Python\Exercise\user.txt‘,‘r‘,encoding=‘utf-8‘) as f: for i in f: d = eval(i) print(d,type(d)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py {‘abc‘: ‘123‘, ‘laochai‘: ‘SB‘, ‘xiaohe‘: ‘bigSB‘} <class ‘dict‘> Process finished with exit code 0
float():將一個整數或者字符串類型的數字,變成浮點數返回。
x = float(1) print(x,type(x)) i = float(‘123‘) print(i,type(i)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 1.0 <class ‘float‘> 123.0 <class ‘float‘> Process finished with exit code 0
format():格式化字符串
x = ‘Name:{x},age:{y},sex:{z}‘ x = x.format(y=18,x=‘abc‘,z=‘male‘) print(x) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py Name:abc,age:18,sex:male Process finished with exit code 0
frozenset():不可變的集合,沒有add,remove方法
s = frozenset({1,2,3,4,5,6}) print(s) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py frozenset({1, 2, 3, 4, 5, 6}) Process finished with exit code 0
globals():返回當前全局作用域。
print(globals()) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py {‘__name__‘: ‘__main__‘, ‘__doc__‘: None, ‘__package__‘: None, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x03889510>, ‘__spec__‘: None, ‘__annotations__‘: {}, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__file__‘: ‘E:/Python/DAY-11/tmp.py‘, ‘__cached__‘: None} Process finished with exit code 0
hash():查看哈希值。
s1=‘hello123123‘ s2=‘hello123123‘ print(hash(s1)) print(hash(s2)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py -1538468614 -1538468614 Process finished with exit code 0
hex():轉換十進制為十六進制。
print(hex(15)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 0xf Process finished with exit code 0
id():查看數據的唯一標識。
print(id(‘abc‘)) print(id(‘def‘)) print(id(‘abc‘)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 50389024 50435488 50389024 Process finished with exit code 0
input():等待用戶輸入並賦值給變量名。
username = input(‘Enter your username:‘) print(‘Your name:‘,username) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py Enter your username:test Your name: test Process finished with exit code 0
int():轉換一個數字或者字符串類型的數字為整數類型。
i = int(‘123‘) print(i,type(i)) x = int(1) print(x,type(x)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 123 <class ‘int‘> 1 <class ‘int‘> Process finished with exit code 0
isinstance():判斷一個對象是不是,列表、元組等類型。
l = [1,2,3,4,5] print(isinstance(l,list)) print(isinstance(l,tuple)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py True False Process finished with exit code 0
iter():轉換可叠代對象為叠代器。
l = [1,2,3,4,5] print(iter(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py <list_iterator object at 0x035F9550> Process finished with exit code 0
len():返回對象的長度。參數可以是字符串,列表,元組,字典,集合
l = [1,2,3,4,5] print(len(l)) print(len(‘abc‘)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 5 3 Process finished with exit code 0
list():新建一個新的列表。
l = list([1,2,3,4,5,6,7]) print(l,type(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py [1, 2, 3, 4, 5, 6, 7] <class ‘list‘> Process finished with exit code 0
locals():查看局部作用域。在全局作用域中使用局部作用域顯示的還是全局作用域。
print(locals()) print(globals() is locals()) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py {‘__name__‘: ‘__main__‘, ‘__doc__‘: None, ‘__package__‘: None, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x00C79510>, ‘__spec__‘: None, ‘__annotations__‘: {}, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__file__‘: ‘E:/Python/DAY-11/tmp.py‘, ‘__cached__‘: None} True Process finished with exit code 0
map():傳遞一個function,和一個iterable,function必須處理iterable中的所有元素。
l=[1,2,3,4] m=map(lambda x:x**2,l) print(list(m)) names=[‘alex‘,‘wupeiqi‘,‘yuanhao‘] print(list(map(lambda item:item+‘_SB‘,names))) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py [1, 4, 9, 16] [‘alex_SB‘, ‘wupeiqi_SB‘, ‘yuanhao_SB‘] Process finished with exit code 0
max():兩個值的最大值。
示例一: salaries={ ‘egon‘:3000, ‘alex‘:100000000, ‘wupeiqi‘:10000, ‘yuanhao‘:2000 } print(max(salaries)) #默認只能讀到字典的key,比較字符串是根絕a-z的順序比較的,先比較第一位字母。註:z>a print(max(salaries.values())) #比較value res=zip(salaries.values(),salaries.keys()) print(max(res)[-1]) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py yuanhao 100000000 alex Process finished with exit code 0 示例二: salaries={ ‘egon‘:3000, ‘alex‘:100000000, ‘wupeiqi‘:10000, ‘yuanhao‘:2000 } def func(x): return salaries[x] print(max(salaries,key=lambda x:salaries[x])) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py alex Process finished with exit code 0
min():同max一樣用法,不過是返回時兩者之間小的那個值。
next():從叠代器中讀取一次值。
l = [1,2,3,4,5,6,7,8] l = iter(l) print(next(l)) print(next(l)) print(next(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 1 2 3 Process finished with exit code 0
oct():轉換八進制。
print(oct(9)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 0o11 Process finished with exit code 0
open():打開一個文件進行文件操作的前提。註:open方式打開文件不會自動關閉,使用with open() as 可以自動關閉文件。
‘r‘ 打開閱讀(默認) ‘w‘ 打開寫入,首先截斷文件 ‘x‘ 打開以供獨占創建,如果文件已存在則失敗 ‘a‘ 打開以寫入,如果存在,則附加到文件的末尾 ‘b‘ 二進制模式 ‘t‘ 文本模式(默認) ‘+‘ 打開磁盤文件進行更新(讀寫) ‘U‘ universal newlines 模式(已棄用) ===================================== f = open (r‘E:\Python\Exercise\userinfo.txt‘,‘r‘,encoding=‘utf-8‘) for i in f: print(i) f.close() #關閉文件 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,30,13304320533,HR,2015-05-03 3,Rain Liu,25,1383235322,Sales,2016-04-22 4,Mack Cao,40,1356145343,HR,2009-03-01 Process finished with exit code 0
pow(): 平方,取余。
print(pow(3,2)) #平方 print(pow(3,2,2)) #取余 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 9 1 Process finished with exit code 0
print():打印信息。
print(‘Hello Python!‘) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py Hello Python! Process finished with exit code 0
range():生成一個指定範圍的數組。range特性顧頭不顧尾
for i in range(1,5,2): #從1開始到4結束,步長為2 print(i) print(‘----------------------‘) for i in range(1, 5): print(i) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 1 3 ---------------------- 1 2 3 4 Process finished with exit code 0
repr():將一個對象轉成字符串顯示,註:只是顯示用
l = [1,2,3,4,5,6] l_t = repr(l) print(l_t,type(l_t)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py [1, 2, 3, 4, 5, 6] <class ‘str‘> Process finished with exit code 0
zip():創建一個叠代器,聚合來自每個叠代器的元素。返回元組的叠代器。
l1 = [1,2,3,4] l2 = [‘a‘,‘b‘,‘c‘,‘d‘] l = zip(l1,l2) print(l) print(list(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py <zip object at 0x0358A080> [(1, ‘a‘), (2, ‘b‘), (3, ‘c‘), (4, ‘d‘)] Process finished with exit code 0
round():保留多少位小數。
print(round(3.3456,3)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 3.346 Process finished with exit code 0
set():新建一個集合。
s = set({1,2,3,4,56}) print(type(s)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py <class ‘set‘> Process finished with exit code 0
reversed():反轉。
l = [1,2,3,4,5,6] l1 = reversed(l) print(l1) for i in l1: print(i) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py <list_reverseiterator object at 0x00989550> 6 5 4 3 2 1 Process finished with exit code 0
str():新建一個字符串。
s = str(‘abc‘) print(s,type(s)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py abc <class ‘str‘> Process finished with exit code 0
sum():求和運算。
l = [1,2,3,4,5,6,7] print(sum(l)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py 28 Process finished with exit code 0
tuple():新建一個元組。
t = (1,2,3,4,5,6,7) print(t,type(t)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py (1, 2, 3, 4, 5, 6, 7) <class ‘tuple‘> Process finished with exit code 0
type():查看一個值得類型。
t = (1,2,3,4,5,6,7) print(type(t)) 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py <class ‘tuple‘> Process finished with exit code 0
sorted():排序。
l=[1,2,4,9,-1] print(sorted(l)) #從小到大 print(sorted(l,reverse=True)) #從大到小 執行結果: D:\Python\Python36-32\python.exe E:/Python/DAY-11/tmp.py [-1, 1, 2, 4, 9] [9, 4, 2, 1, -1] Process finished with exit code 0
遞歸調用:
在調用一個函數的過程中,直接或間接調用了該函數本身。
遞歸效率低下,需要在進入下一次遞歸時保留當前的狀態。
Python沒有尾遞歸,並且做了層級限制。
必須有一個明確的結束條件。
示例:
l = [1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]] def readdate(list_name): for i in list_name: if isinstance(i,list): readdate(i) else: print(i) readdate(l)
匿名函數:
自帶return,無需函數名,一定有個返回值。匿名函數只能運行一次。
冒號左邊是參數,右邊是函數體。
使用lambda定義匿名函數:
lambda x:x**2
示例: l=[1,2,3,4] m=map(lambda x:x**2,l) print(list(m)) names=[‘alex‘,‘wupeiqi‘,‘yuanhao‘] print(list(map(lambda item:item+‘_SB‘,names)))
Python基礎day-11[內置函數(未完),遞歸,匿名函數]