python之打包,解包

Posted 知_行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之打包,解包相关的知识,希望对你有一定的参考价值。

#coding:utf-8
#字符串,列表,元组打包与解包
aString = abc
aList = [1, 2, 3]
aTuple = a, A, 1

print(Unpacking string......)
first, second, third = aString
print("string values:", first, second, third)

print(\nUnpacking list......)
first, second, third = aList
print("string values:", first, second, third)

print(\nUnpacking tuple......)
first, second, third = aTuple
print("string values:", first, second, third)

#swapping two values
x = 3
y = 4
print(\nBefore swapping: x = %d,y =%d % (x, y))
#首先将右边的部分打包成一个元组,即(4,3),而后解包指派给变量x,y
x, y = y, x #swap Variables
print(After swapping: x = %d, y = %d % (x, y))

 

以上是关于python之打包,解包的主要内容,如果未能解决你的问题,请参考以下文章