#-------------字符串格式化------------------、 #test1 info={ 'name':'lanzili', 'age':20, 'school':"sysu" } print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%info) name='lanzili' age=20 school="sysu" print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%vars()) print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%vars()) """ def vars(p_object=None): # real signature unknown; restored from __doc__ vars([object]) -> dictionary Without arguments, equivalent to locals(). With an argument, equivalent to object.__dict__. return {} """ print("He is name {0[name]} ,{0[age]} years old ,and major in {0[school]}".format(info))#不能丢掉大括号 def foo(x,y,z): return x+y+z from functools import partial f=partial(foo,2,4)#这一步类似于为参数设定默认值 a=f(3)#等价于foo(2,4,3) print(a) def info(name,age,school): print("He is name %(name)s ,%(age)d years old ,and major in %(school)s"%vars()) #info('lanzili',22,'sysu') #f2=partial(info,'lan','sysu')#错误写法,不能跳过age这个参数,要按照顺序 #f2=partial(info,age='lan')#也不能使用关键词来定义 #f2(12,'sysu')#info() got multiple values for argument 'age' #------------------常见转换-------------------- #略去str(),int(),float(),complex(),repr(),format(),tuple(),list(),set(),dict(),frozenset(),set() #eval 可以对字符串进行求值 a='123+456*23' print(eval(a))#10611 #chr(int),将整数转化成ascii print(chr(99))#c #ord(char),将字符转化为int print(ord("c"))#99 #hex(int),bin(int),oct(int),分别将整数转化为16进制,2进制和八进制 #list生成器与条件表达式,很骚 lst=[100,2,324,32,423,435,12,34,45] lst_new=[i if i<100 else 100 for i in lst]#先写条件表达式,然后写迭代式 print (lst_new)
转载于:https://my.oschina.net/u/3641281/blog/1556865