python基础2
- 集合
a.特点:具有无序性,单一性,不可重复性
b.用法:
b1.pop(随机删除) remove(删除不存在元素会报错) discard(删除不存在元素不会报错) 三者的区别
s={1,2,3,'Hacker_Teen','ugly','I'} #i=0 #while i<len(s): # s.pop() # print(s) # i=i+1 #------------------------***------------------- #s.remove('ugly') #print(s) #------------------------***------------------- s.discard('3') print(s)
b2.集合关系的交、并、差、交叉补集集的运算
python_study = {'wjc','xz','lz','wjc'} C_study = {'wjc','xz','gxq','cxf'} py_1 = set(python_study) C_1 = set(C_study) print(py_1) print(C_1) #----------------取交集------------ #print(py_1.intersection(C_1)) #print((py_1)&(C_1)) #----------------取并集------------ #print(py_1.union(C_1)) #print(py_1|C_1) #----------------取差集------------ #print(py_1.difference(C_1)) #print(py_1-C_1) #----------------交差补集------------ #print(py_1.symmetric_difference(C_1)) #print(py_1^C_1)
b3.字符串格式化
#----------------字符串格式化------------ #self_introduce='I am %s,my age is %d'%('Hacker_Teen',21) #print(self_introduce) #print('percen%.2f'%98.67432)
#------------打印百分号------------------
#print('percen %.2f %%'%98.67432)
#-------------用字典传输-------------------
#self_introduce='I am %(name)s,my age is %(age)d'%({'name':'Hacker_Teen','age':21})
#print(self_introduce)
#----------------采用分隔符------------
print('wjc','xz','lz','gxq',sep=':')
b4.format的用法
#--------------format的用法--------------- li=['Hacker_Teen',21,'coding'] di={'name':'Hacker_Teen','age':21,'hobby':'coding'} #print('I am {0}.Age is {1}.My hobby is {2}'.format('Hacker_Teen',21,'coding')) #print('I am {name}.Age is {age}.My hobby is {hobby}'.format(name='Hacker_Teen',age=21,hobby='coding')) #test1='I am {:s}.Age is {:d}.My hobby is {:s}'.format(*li) #print(test1) test2='I am {name}.Age is {age}.My hobby is {hobby}'.format(**di) print(test2)
- 函数