标签:
print函数中使用逗号输出多个表达式,打印的结果之间使用空格隔开。
print(‘name:‘,‘zyj‘,‘age:‘,‘24‘) print(1,2,3) #结果为1 2 3
import somemodule from somemodule import somefunction from somemodule import somefunction, anotherfunction, yetanotherfunction from somemodule import * import somemodule as newmodulename from somemodule import somefunction as newfunctionname from module1 import open as open1 from module2 import open as open2
1、多个赋值操作同时进行
x,y,z = 1,2,3 print(x,y,z) x,y = y,x y,z = z,y print(x,y,z)
2、序列解包:将多个值得序列解开,然后放到变量的序列中.注:必须进行等长元素赋值,否则会报错。
values = 1,2,3 print(values) x,y,z = values print(x) lst = [1,2,3] a,b,c = lst print(a,b,c) str = ‘hello‘ a,b,c,d,e = str print(a,b,c,d,e)
3、使用*的特性可以将其他参数都收集在一个变量中的实现,通过此方式返回的均为列表。
a,b,*rest = [1,2,3,4] print(a,b,rest) # rest = [3,4] *rest,a =(1,2) print(rest,a) # rest =[1] d = {‘name‘:‘zyj‘,‘score‘:‘99‘} d1 = d.keys() print(d1) # ([‘name‘,‘score‘]) *rest,c = d1 print(rest,c) #结果:[‘name‘] score
标签:
原文地址:http://www.cnblogs.com/zhaoyujiao/p/5140813.html