首页 > 代码库 > python学习-语句总结
python学习-语句总结
1 # f=open(‘a.txt‘,‘w‘,encoding=‘utf-8‘) 2 # 3 # f.write(‘11111\n‘) 4 # f.write(‘2222\n‘) 5 # f.write(‘3333\n4444\n‘) 6 # 7 # f.writelines([‘a\n‘,‘b\n‘,‘c\n‘]) 8 # 9 # f.close() 10 11 12 13 14 # import os 15 # with open(‘old.txt‘,‘r‘,encoding=‘utf-8‘) as read_f,\ 16 # open(‘.old.txt.swap‘,‘w‘,encoding=‘utf-8‘) as write_f: 17 # msg=read_f.read() 18 # # print(msg,type(msg)) 19 # msg=msg.replace(‘alex‘,‘SB‘) 20 # # print(msg) 21 # write_f.write(msg) 22 # 23 # os.remove(‘old.txt‘) 24 # os.rename(‘.old.txt.swap‘,‘old.txt‘) 25 26 27 28 29 import os 30 with open(‘old.txt‘,‘r‘,encoding=‘utf-8‘) as read_f,31 open(‘.old.txt.swap‘,‘w‘,encoding=‘utf-8‘) as write_f: 32 for line in read_f: 33 if ‘SB‘ in line: 34 line=line.replace(‘SB‘,‘alex‘) 35 write_f.write(line) 36 os.remove(‘old.txt‘) 37 os.rename(‘.old.txt.swap‘,‘old.txt‘)
1 # pythons=[‘alex‘,‘egon‘,‘yuanhao‘,‘wupeiqi‘,‘gangdan‘,‘biubiu‘] 2 # linuxs=[‘wupeiqi‘,‘oldboy‘,‘gangdan‘] 3 # 4 # res=[] 5 # for p in pythons: 6 # if p in linuxs: 7 # res.append(p) 8 # 9 # print(res) 10 #关系运算??? 11 12 13 #1 集合内可以有多个元素,但是每个元素都必须是不可变类型,即可hash类型 14 #2 集合内的元素唯一 15 #3 集合是无序的 16 # s={1,‘a‘,1,1,1,1,1,1} #s=set({1,‘a‘,1,1,1,1,1,1}) 17 18 19 # s1=set(‘hello‘) 20 # print(s1,type(s1)) 21 # s={‘a‘,3,9,‘b‘} 22 # print(s) 23 24 #集合优先掌握的方法 25 # pythons={‘alex‘,‘egon‘,‘yuanhao‘,‘wupeiqi‘,‘gangdan‘,‘biubiu‘} 26 27 # print(‘alex‘ not in pythons) 28 # print(pythons) 29 30 #关系运算 31 # s1={1,10,11,22} 32 # s2={1,11,33} 33 34 #交集 35 # print(s1 & s2) 36 37 #并集 38 # print(s1 | s2) 39 40 #差集 41 # print(s1 - s2) 42 # print(s2 - s1) 43 44 #对称差集 45 # print(s1 ^ s2) 46 47 48 49 #父集 50 # s1={1,2,3,4} 51 # s2={1,5} 52 # print(s1 >= s2) 53 # 54 # #子集 55 # print(s1 <= s2) 56 # print(s2 <= s1) 57 58 59 #集合练习一 60 61 # pythons={‘alex‘,‘egon‘,‘yuanhao‘,‘wupeiqi‘,‘gangdan‘,‘biubiu‘} 62 # linuxs={‘wupeiqi‘,‘oldboy‘,‘gangdan‘} 63 # 64 # 65 # # 1. 求出即报名python又报名linux课程的学员名字集合 66 # print(pythons & linuxs) 67 # # 2. 求出所有报名的学生名字集合 68 # print(pythons | linuxs) 69 # # 3. 求出只报名python课程的学员名字 70 # print(pythons - linuxs) 71 # # 4. 求出没有同时这两门课程的学员名字集合 72 # print(pythons ^ linuxs) 73 74 75 76 #集合练习二: 77 # 1. 有列表l=[‘a‘,‘b‘,1,‘a‘,‘a‘],列表元素均为可hash类型,去重,得到新列表,且新列表无需保持列表原来的顺序 78 # l=[‘a‘,‘b‘,1,‘a‘,‘a‘] 79 # 80 # s=set(l) 81 # print(s) 82 # print(list(s) 83 # ) 84 85 86 # 2.在上题的基础上,保存列表原来的顺序 87 # l=[‘a‘,‘b‘,1,‘a‘,‘a‘] 88 # 89 # l1=[] 90 # for item in l: 91 # if item not in l1: 92 # l1.append(item) 93 # print(l1) 94 # 95 # l1=[] 96 # s=set() 97 # for item in l: 98 # if item not in s: 99 # s.add(item) #{‘a‘,‘b‘,1} 100 # # l1.append(item) 101 # l1.append(item) 102 # 103 # print(l1) 104 105 # 106 # 4.有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序 107 # 108 # l=[ 109 # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}, 110 # {‘name‘:‘alex‘,‘age‘:73,‘sex‘:‘male‘}, 111 # {‘name‘:‘egon‘,‘age‘:20,‘sex‘:‘female‘}, 112 # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}, 113 # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}, 114 # ] 115 # 116 # l1=[] 117 # for item in l: 118 # if item not in l1: 119 # l1.append(item) 120 # print(l1) 121 122 123 124 125 # l1=[] 126 # s=set() 127 # for item in l: 128 # val=(item[‘name‘],item[‘age‘],item[‘sex‘]) 129 # # print(val) 130 # if val not in s: 131 # s.add(val) 132 # # print(val) 133 # l1.append(item) 134 # 135 # print(l1) 136 137 138 139 140 141 #集合的内置方法 142 pythons={‘alex‘,‘egon‘,‘yuanhao‘,‘wupeiqi‘,‘gangdan‘,‘biubiu‘} 143 linuxs={‘wupeiqi‘,‘oldboy‘,‘gangdan‘} 144 # 1. 求出即报名python又报名linux课程的学员名字集合 145 # print(pythons & linuxs) 146 # print(pythons.intersection(linuxs)) 147 148 # 2. 求出所有报名的学生名字集合 149 # print(pythons | linuxs) 150 # print(pythons.union(linuxs)) 151 # 3. 求出只报名python课程的学员名字 152 # print(pythons - linuxs) 153 # print(pythons.difference(linuxs)) 154 # 4. 求出没有同时这两门课程的学员名字集合 155 # print(pythons ^ linuxs) 156 # print(pythons.symmetric_difference(linuxs)) 157 158 s={1,2,3,‘a‘} 159 # s.add(4) 160 # print(s) 161 162 # print(s.pop()) 163 164 # s.remove(‘a‘) 165 # print(s) 166 167 # s.remove(‘vvvvvvvvvv‘) 168 # s.discard(‘aaaaaa‘) 169 # print(s)
1 my_girl_friends=[‘alex‘,‘wupeiqi‘,‘yuanhao‘,4,10,30] 2 # my_girl_friends=list([‘alex‘,‘wupeiqi‘,‘yuanhao‘,4,10,30]) 3 4 5 # print(type(my_girl_friends)) 6 7 # print(my_girl_friends[2]) 8 # 9 # print(my_girl_friends[1:3]) 10 11 # my_girl_friends.append(‘oldboy‘) 12 # print(my_girl_friends) 13 14 #pop按照索引 15 # my_girl_friends.pop() 16 # my_girl_friends.pop() 17 # my_girl_friends.pop(0) 18 # my_girl_friends.pop(0) 19 # my_girl_friends.pop(1) 20 21 #remove按照值 22 # my_girl_friends.remove(‘yuanhao‘) 23 # print(my_girl_friends) 24 25 26 # my_girl_friends.__len__() 27 # print(len(my_girl_friends)) 28 29 # print(‘wupeiqi‘ in my_girl_friends) 30 # 31 # msg=‘my name is egon111111‘ 32 # print(‘egon‘ in msg) 33 34 #其他操作(掌握) 35 # my_girl_friends=[‘alex‘,‘wupeiqi‘,‘alex‘,‘yuanhao‘,4,10,30] 36 # my_girl_friends.insert(1,‘Sb‘) 37 # print(my_girl_friends) 38 39 40 41 42 #其他操作(了解) 43 my_girl_friends=[‘alex‘,‘wupeiqi‘,‘alex‘,‘yuanhao‘,4,10,30] 44 # my_girl_friends.clear() 45 # print(my_girl_friends) 46 47 # l=my_girl_friends.copy() 48 # print(l) 49 50 # print(my_girl_friends.count(‘alex‘)) 51 52 # my_girl_friends.append(‘oldboy1‘) 53 # my_girl_friends.append(‘oldboy2‘) 54 # my_girl_friends.append(‘oldboy3‘) 55 # print(my_girl_friends) 56 # my_girl_friends.extend([‘oldboy1‘,‘oldboy2‘,‘oldboy3‘]) 57 # print(my_girl_friends) 58 59 # print(my_girl_friends.index(‘alex‘)) 60 # my_girl_friends.reverse() 61 # print(my_girl_friends) 62 63 # l=[3,-1,5,2] 64 # l.sort(reverse=True) 65 # print(l) 66 67 68 69 #练习一: 70 data=http://www.mamicode.com/[‘alex‘,84,[1900,3,38]] 71 # print(data[0]) 72 # print(data[1]) 73 # print(data[2][0]) 74 75 76 # name,age,birth=data 77 # print(name) 78 # print(age) 79 # print(birth) 80 # 81 # 82 # msg=‘hello‘ 83 # a,b,c,d,e=msg 84 # print(a,b,c,d,e) 85 86 87 # msg=‘hello‘ 88 # a,_,_,_,b=msg 89 # print(a) 90 # print(b) 91 92 # a,*_,b=msg 93 # print(a,b) 94 95 96 #队列:先进先出 97 fifo=[] 98 #入队 99 # fifo.append(‘first‘) 100 # fifo.append(‘second‘) 101 # fifo.append(‘third‘) 102 # print(fifo) 103 # #出队 104 # print(fifo.pop(0)) 105 # print(fifo.pop(0)) 106 # print(fifo.pop(0)) 107 108 #入队 109 # fifo.insert(0,‘first‘) 110 # fifo.insert(0,‘second‘) 111 # fifo.insert(0,‘third‘) 112 # print(fifo) 113 # 114 # #出队 115 # print(fifo.pop()) 116 # print(fifo.pop()) 117 # print(fifo.pop()) 118 119 120 121 #堆栈:先进后出 122 lifo=[]
1 按存值个数区分 2 标量/原子类型:数字,字符串 3 容器类型:列表,元组,字典 4 5 按可变不可变区分 6 可变:列表,字典 7 不可变:数字,字符串,元组 8 9 按访问方式区分 10 直接访问:数字 11 按照索引访问(序列类型):字符串,列表,元组 12 key访问(映射类型) 字典
1 age=(11,22,33,44,55,33)# 本质age=tuple((11,22,33,44,55)) 2 3 # print(age[2]) 4 # print(age[1:4]) 5 # print(len(age)) 6 # 7 # print(11 in age) 8 9 10 # print(age.index(33)) 11 # print(age.count(33)) 12 13 14 #元组练习 15 msg_dic={ 16 ‘apple‘:10, 17 ‘tesla‘:100000, 18 ‘mac‘:3000, 19 ‘lenovo‘:30000, 20 ‘chicken‘:10, 21 } 22 goods_l=[] 23 24 while True: 25 for key in msg_dic: 26 # print(‘Goods Name:%s Price:%s‘ %(key,msg_dic[key])) 27 print(‘\033[43mName:{name} Price:{price}\033[0m‘.format(price=msg_dic[key],name=key)) 28 choice=input(‘your goods name>>: ‘).strip() 29 if len(choice) == 0 or choice not in msg_dic:continue 30 count=input(‘your count>>: ‘).strip() 31 if count.isdigit(): 32 goods_l.append((choice,msg_dic[choice],int(count))) 33 print(goods_l)
1 info={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 2 #本质info=dict({‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}) 3 4 5 # print(info[‘age‘]) 6 # info[‘height‘]=1.80 7 # 8 # print(info) 9 # 10 # for key in info: 11 # print(key) 12 13 14 #字典的key必须是不可变类型,也成为可hash类型 15 # info={(1,2):‘a‘} 16 # print(info[(1,2)]) 17 18 #字典常用的方法(优先掌握) 19 info={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 20 # print(info.pop(‘name‘)) 21 # print(info) 22 # print(info.pop(‘asdfsadfasdfasfasdfasdfasdf‘,None)) 23 24 25 # print(info[‘name1‘]) 26 # print(info.get(‘name1‘)) 27 # print(info.get(‘nameasdfasdfasdfasdf‘,‘not key‘)) 28 29 30 #字典其他的方法 31 info={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 32 # print(info.popitem()) 33 # print(info.popitem()) 34 # print(info) 35 # 36 # print(info.keys(),type(info.keys())) 37 # print(info.values()) 38 39 # for key in info.keys(): 40 # print(key) 41 42 # for key in info.values(): 43 # print(key) 44 45 # for key in info: 46 # print(key,info[key]) 47 48 # print(info.items()) 49 # for key,value in info.items(): # key,value=http://www.mamicode.com/(‘name‘,‘egon‘) 50 # print(key,value) 51 52 53 # msg_dic={ 54 # ‘apple‘:10, 55 # ‘tesla‘:100000, 56 # ‘mac‘:3000, 57 # ‘lenovo‘:30000, 58 # ‘chicken‘:10, 59 # } 60 # for key,value in msg_dic.items(): 61 # print(key,value) 62 63 64 # info={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 65 # info.clear() 66 # print(info) 67 68 # print(info.items()) 69 # dic=info.fromkeys([‘name‘,‘age‘,‘sex‘],11111111) 70 # print(dic) 71 # 72 # dic=info.fromkeys([‘name‘,‘age‘,‘sex‘],None) 73 # print(dic) 74 75 # 76 # dic=dict(a=1,b=2,c=3) 77 # print(dic) 78 79 # print(info.items()) 80 81 # print(dict([(‘name‘, ‘egon‘), (‘age‘, 18), (‘sex‘, ‘male‘)])) 82 83 84 # dic=dict.fromkeys([‘name‘,‘age‘,‘sex‘],11111111) 85 # print(dic) 86 # print(info) 87 88 89 90 91 92 # print(info) 93 # dic={‘a‘:1,‘b‘:2,‘name‘:‘SHUAI‘} 94 # info.update(dic) 95 # print(info) 96 97 98 # d=dict.setdefault([‘a‘,‘b‘,‘c‘],[]) 99 # print(d) 100 # 101 # d={} 102 # print(d) 103 # d[‘name‘]=‘egon‘ 104 # d[‘age‘]=18 105 # d[‘sex‘]=‘male‘ 106 # # d[‘hobby‘]=[] 107 # # d[‘hobby‘].append(‘play basketball‘) 108 # # d[‘hobby‘].append(‘play football‘) 109 # 110 # d.setdefault(‘hobby‘,[]).append(‘play1‘) #d[‘hobby‘] 111 # d.setdefault(‘hobby‘,[]).append(‘play2‘) #d[‘hobby‘] 112 # d.setdefault(‘hobby‘,[]).append(‘play3‘) #d[‘hobby‘] 113 # print(d) 114 115 116 #字典练习 117 # nums=[11, 22, 33, 44, 55, 66, 77, 88, 99, 90] 118 # dic={ 119 # ‘k1‘:[], 120 # ‘k2‘:[], 121 # } 122 # for item in nums: 123 # if item >= 66: 124 # dic[‘k1‘].append(item) 125 # else: 126 # dic[‘k2‘].append(item) 127 # 128 # print(dic) 129 130 131 132 133 134 135 136 137 s=‘hello alex alex say hello sb sb‘ 138 words=s.split() 139 # print(words) 140 dic={} 141 for word in words: 142 # print(word) 143 if word not in dic: 144 dic[word]=1 145 #{‘hello‘:1,‘alex‘:1} 146 else: 147 dic[word]+=1 148 149 print(dic)
1 # name=‘egon‘ #name=str(‘egon‘) 2 # name=str(‘egon‘) 3 # print(type(name)) 4 # print(name) 5 6 #优先掌握 7 #索引 8 # name=‘egon‘ #name=str(‘egon‘) 9 # print(name[0]) 10 # print(name[1000]) 11 12 13 #移除空白 14 # name=input(‘username: ‘) 15 # # print(name) 16 # 17 # name=name.strip() 18 # print(name) 19 20 21 # name=input(‘username: ‘).strip() 22 # print(name) 23 24 25 # name=input(‘username: ‘) 26 # print(name.strip()) 27 28 29 # name=‘***egon********‘ 30 # # print(name.strip(‘*‘)) 31 # 32 # print(name.lstrip(‘*‘)) 33 # print(name.rstrip(‘*‘)) 34 35 36 #切分 37 # user_info=‘root:x:0:0::/root:/bin/bash‘ 38 # print(user_info.split(‘:‘)[5]) 39 40 # cmd_info=‘get|a.txt|333333333‘ 41 # # print(cmd_info.split(‘|‘)[0]) 42 # print(cmd_info.split(‘|‘,1)[0]) 43 44 # msg=‘name egon age 18‘ 45 # print(msg.split()) 46 47 48 #取长度 49 # name=‘egon‘ 50 # # print(name.__len__()) 51 # print(len(name)) #name.__len__() 52 53 54 #切出子字符串 55 # name=‘hello world‘ 56 # # print(name[1]) 57 # # print(name[2]) 58 # # print(name[3]) 59 # print(name[1:7:2]) 60 61 62 63 #字符的其他方法(掌握) 64 # name=‘alex_SB‘ 65 # print(name.endswith(‘SB‘)) 66 # print(name.startswith(‘alex‘)) 67 68 # name=‘alex say :i have one tesla,my name is alex‘ 69 # print(name.replace(‘alex‘,‘SB‘,1)) 70 71 72 # print(‘{} {} {}‘.format(‘egon‘,18,‘male‘)) 73 # print(‘{0} {1} {0}‘.format(‘egon‘,18,‘male‘)) 74 # print(‘NAME:{name} AGE:{age} SEX:{sex}‘.format(age=18,sex=‘male‘,name=‘egon‘)) 75 76 # num=‘123‘ 77 # print(num.isdigit()) 78 79 # oldboy_age=73 80 # while True: 81 # age=input(‘>>: ‘).strip() 82 # if len(age) == 0:continue 83 # if age.isdigit(): 84 # age=int(age) 85 # print(age,type(age)) 86 87 88 89 90 91 92 #字符其他需要了解的方法 93 94 name=‘egon hello‘ 95 # print(name.find(‘o‘)) 96 # print(name.find(‘x‘)) 97 # print(name.find(‘o‘,3,6)) 98 99 # print(name.index(‘o‘)) 100 # print(name.index(‘x‘)) 101 102 103 # print(name.count(‘o‘,1,3)) 104 # 105 # l=[‘egon‘,‘say‘,‘hello‘,‘world‘] #类别内容必须都是字符串 106 # print(‘:‘.join(l)) 107 108 # name=‘egon‘ 109 # print(name.center(30,‘*‘)) 110 # print(name.ljust(30,‘*‘)) 111 # print(name.rjust(30,‘*‘)) 112 # print(name.zfill(30)) 113 114 115 116 # name=‘egon\thello‘ 117 # print(name) 118 # print(name.expandtabs(1)) 119 120 # name=‘EGON‘ 121 # print(name.lower()) 122 # 123 # name=‘eg‘ 124 # print(name.upper()) 125 126 127 # name=‘egon say‘ 128 # 129 # print(name.capitalize()) #首字母大写 130 # print(name.swapcase()) #大小写翻转 131 # msg=‘egon say hi‘ 132 # print(msg.title()) #每个单词的首字母大写 133 134 # name=‘egon123‘ 135 # print(name.isalnum()) #字符串由字母和数字组成 136 # name=‘egon‘ 137 # print(name.isalpha()) #字符串只由字母组成 138 139 140 141 num1=b‘4‘ #Bytes 142 num2=u‘4‘ #unicode,python3中无需加u就是unicode 143 num3=‘四‘ #中文数字 144 num4=‘Ⅳ‘ #罗马数字 145 146 #bytes,unicode 147 # print(num1.isdigit()) 148 # print(num2.isdigit()) 149 # print(num3.isdigit()) 150 # print(num4.isdigit()) 151 152 153 #isdecimal:unicode 154 # print(num2.isdecimal()) 155 # print(num3.isdecimal()) 156 # print(num4.isdecimal()) 157 158 159 #isnumberic:unicode,汉字,罗马 160 # print(num2.isnumeric()) 161 # print(num3.isnumeric()) 162 # print(num4.isnumeric())
python学习-语句总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。