0、列表list、元組tuple、字典dict、集合set、字符串str
0.1 列表list:list1 = [ 1,'hello' , 999] 增:.append() / .insert(),刪:.remove() / pop('指定'), 查:list1[i] ,改:list1[2] = 888
0.2 元組tuple: tuple1 = (1,'hello' , 999)只讀列表, 增:-,刪:del tuple1, 查:tuple1[i] ,改:-,tuple3 =tuple1+tuple2
0.3 字典dict:dict1 = {'a1':'hello','a2':999} Key-value值和無序,增:dict1['new'] = '66'/.setdefault(key,value)/.update(dict2),刪:.pop('key'), 查:dict['key']/.get('key'),改:dict1['key']= 888
0.4 集合set:set1 = {1,3,8,'hello'} 無序,增:.add() /.update([1,3,99]),刪:.remove() / .pop('指定')/.discard(), 查:i in set1 ,改:list1[2] = 888
0.5 字符串str: str1 = 'hello 8' 不可修改,能夠切片,str1+str2
html
一、os與sys的區別:os與sys的區別
二、sys.argv()介紹:sys.argv
三、encode 與 decode:python
__author__ = 'baochongmao' msg = '你好' print(msg.encode(encoding = 'utf-8')) print(msg.encode(encoding = 'utf-8').decode(encoding = 'utf-8'))
D:\python3.7\python.exe D:/workspacePyCharm/PyLearning/day2/encode.py
b'\xe4\xbd\xa0\xe5\xa5\xbd'
你好git
四、list練習:api
names = ['chongmao','alex',5,8,'@wang','zhang',99] print(names) print(names[3]) print(names[0],'\n',names[:5],'\n',names[0:5],'\n',names[-1],'\n',names[-3:],'\n') #search or slice names.append('add_name') names.append('p_del') #.append() print(names) print(names.index(8)) #.index() del names[2] #del print(names) names.pop() print(names) names2 = [8,'a','c'] names.extend(names2) #.extend() print(names,'\n',names2) names.insert(1,'insert_n') print(names) names.pop(-2) #.pop() print(names) names.append('zhang') print(names.count(8)) #.count() print(names.count('zhang')) names.remove('alex') #.remove print(names) #names.sort() #print(names) names.reverse() #.reverse() print(names) names3 = ['Zhang', 'c', '8B', 'Add_name', '99', '#zhang', '@wang', 'Insert_n', 'chongmao'] names3.sort() #.sort() print(names3)
執行結果以下:app
五、list練習2iphone
import copy name1 = ['Zhang', 'c', '8B', 'Add_name',['alex','NEW'] , '99', '#zhang', '@wang', 'Insert_n', 'chongmao'] name2 = name1.copy() # shallow copy name3 = copy.copy(name1) # = .copy() name4 = copy.deepcopy(name1) #copy.deepcopy() print(name1) print(name2) name1[4][0] = 'ALEX' print(name1) print(name2) print(name3) print(name4) name5 = name4[::2] print(name5) for i in name5 : print(i)
結果以下:spa
六、元組tuple練習:3d
c1 = (1,3,99) print(type(c1))
七、字典dict練習:code
__author__ = 'Administrator' info = { 'stu1101': "TengLan Wu", 'stu1102': "LongZe Luola", 'stu1103': "XiaoZe Maliya", } print(info) info['new'] = 'teacher li' print(info) print(info['stu1102']) print(info.get('stu1101')) info['stu1103'] = 'wang zi' print(info) #del info['stu1103'] info.pop('stu1103') print(info) for i in info : print(i,info[i]) print(info.keys()) print(info.items()) ''' c = {'st':'san','stu1101':'lan wu'} b = info.update(c) print(b) ''' for k,b in info.items() : print(k,b) print(info.setdefault('1107',25),'\n',info) d = dict.fromkeys([1,2,8],'tested') print(d) listd = [d] print(listd)
八、 集合set練習:orm
__author__ = 'admin' dict_a = {'a':8,'b':"hello"} print(dict_a,type(dict_a)) la = [1,3,6,4,8,3,8,9] la[2] =989 print(la) a1 = set(la) print(a1,type(a1)) a2 = set([3,8,44,9,66,31]) print(a1.intersection(a2), a1 & a2) print(a1.union(a2), a1| a2) print(a1.difference(a2), a1-a2) print(a1.symmetric_difference(a2),a1^a2) #without [3,8,9] a3 =set([3,8,9]) print(a1.issubset(a2)) print(a3.issubset(a1),a1.issuperset(a3)) a3.add(888) a3.update(['hello',999]) print(a3) a4 = set([3,8,44,9,66,31,89]) print(a4) #a4.pop() #print(a4) a4.remove(31) print(a4) a4.discard(999) print(a4,a4.discard(999)) print(a1.isdisjoint(a2)) #不相交
九、字符串str練習:
__author__ = 'Administrator' name = 'chongmao' print(name.capitalize()) print(name.center(20,'+')) print('efw f @ F'.casefold()) print('Efw f @ F'.lower()) print('efw f @ F'.count('F')) print('efw f @ F'.endswith('F')) print('efw f @ F'.startswith('E')) print('efw \tf @ F'.expandtabs(10)) print('efw f @ F'.encode('utf-8')) print('efw f @ F'.find('f')) print('efw f @ F'.rfind('f')) print('efw f{0} @ F'.format(999)) print('efw f @ F'.index('f')) print('efw f @ F'.rindex('f')) print('777'.isalnum()) print('g'.isalpha()) print('efw f @ F'.isascii()) print('8A'.isdecimal()) print('chongmao'.islower()) print('chongmao'.istitle()) print('89'.isnumeric()) print('ch'.join(['1','2','3'])) p = str.maketrans('abcdefg','1234567') print('chongmao'.translate(p)) print('chongmao'.ljust(10,'$')) print('chongmao'.rjust(10,'$')) print('\nchongmao\n'.lstrip()) print('---') print('\nchongmao\n'.strip()) print('---') print('\nchongmao\n'.rstrip()) print('---') print('chongmao jac'.replace('a','A')) print('chongmao jac'.rsplit('a')) print('chongmao jac'.split('a')) print('chongmao jac'.strip()) print('chongmao jac'.strip('c')) print('cHOngmao jac'.swapcase()) print('chongmao jac'.title()) print('chongmao jac'.upper()) print('chongmao jac'.zfill(20))
10 購物車列表練習:
__author__ = 'Administrator' salary = input('Please input your salary:') goods_l = [('book',80),('iphone',6500),('mac',9999),('bike',699),('bread',30)] shopping_l = [] def print_shopping(): print('----shopping list----') for i in shopping_l : print(i) print('Your current balance is :',salary) exit() for i in goods_l : print(goods_l.index(i),i) #for index, item in enumerate(goods_l): # print(index,item) if salary.isdigit() : salary = int(salary) while True : choice = input('Please input the goods number (%s-%s):'%(0,len(goods_l))) if choice.isdigit() : choice = int(choice) if choice < len(goods_l) and choice >= -1 : p_item = goods_l[choice] if p_item[1] <= salary : shopping_l.append(p_item[0]) salary -= p_item[1] print('Add %s into your shopping cart,your current balance is:\033[31;1m %s\033[0m '%(p_item[0],salary)) else : print('You have no enough money.') print_shopping() else : print('Invalid number,please input again.') elif choice == 'q'or choice == 'Q' : print_shopping() # print('----shopping list----') # for i in shopping_l : # print(i) # print('Your current balance is :',salary) # exit() else: print('Invalid input,please input a number.') else: print('Invalid input')
結果示例: