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))

 

posted @ 2016-06-30 20:03  知_行  阅读(804)  评论(0编辑  收藏  举报