目錄python
萬能捕捉異常公式git
try: # 邏輯代碼 1/0 except Exception as e: print(e) division by zero
拷貝/淺拷貝/深拷貝都是針對可變類型數據而言的api
l1 = ['a','b','c',['d','e','f']] l2 = l1 l1.append('g') print(l1) print(l2) # ['a', 'b', 'c', ['d', 'e', 'f'], 'g'] # ['a', 'b', 'c', ['d', 'e', 'f'], 'g']
若是l2是l1的拷貝對象,則l1內部的任何數據類型的元素變化,則l2內部的元素也會跟着改變,由於可變類型值變id不變app
import copy l1 = ['a','b','c',['d','e','f']] l2 = copy.copy(l1) l1.append('g') print(l1) print(l2) l1[3].append('g') print(l1) print(l2) # ['a', 'b', 'c', ['d', 'e', 'f'], 'g'] # ['a', 'b', 'c', ['d', 'e', 'f']] # ['a', 'b', 'c', ['d', 'e', 'f', 'g'], 'g'] # ['a', 'b', 'c', ['d', 'e', 'f', 'g']]
若是l2是l1的淺拷貝對象,則l1內的不可變元素髮生了改變,l2不變;若是l1內的可變元素髮生了改變,則l2會跟着改變debug
import copy l1 = ['a','b','c',['d','e','f']] l2 = copy.deepcopy(l1) l1.append('g') print(l1) print(l2) l1[3].append('g') print(l1) print(l2) # ['a', 'b', 'c', ['d', 'e', 'f'], 'g'] # ['a', 'b', 'c', ['d', 'e', 'f']] # ['a', 'b', 'c', ['d', 'e', 'f', 'g'], 'g'] # ['a', 'b', 'c', ['d', 'e', 'f']]
若是l2是l1的深拷貝對象,則l1內的不可變元素髮生了改變,l2不變;若是l1內的可變元素髮生了改變,l2也不會變,即l2永遠不會由於l1的變化而變化code
age = 18 age = int('18')
+ - * / % // **
salary = 3.2 salary = float('3.2')
+ - * / % // **
name = 'nick' name = "lwx" name = ''' lwx lwx ''' name = """ lwx lwx """ name = "'lwx'" name = '"lwx"'
friends_list = ['longzeluola','canglaoshi','qiaobenai','nick'] lis = list('abcd')
friends_tuple = ('longzeluola','canglaoshi','qiaobenai','nick') tup = tuple('abcd')
nick_info_dict = { 'name':'nick', 'height':180, 'weight':140, 'hobby_list':['read','run','music','fishing','programming','coding','debugging'] } for k,v in nick_info_dict.items(): print(k,v)
s = set() s = {1,2,3,4,5,1}
==
一個值 | 多個值 |
---|---|
整型/浮點型/字符串 | 列表/元祖/字典/集合/ |
可變 | 不可變 |
---|---|
列表/字典/集合 | 整型/浮點型/字符串 |