讲述Python序列如何进行解包教程
讲述Python序列如何进行解包教程
Python序列具有很广泛的应用范围,在实际的应用中还是有不少的问题需要我们大家解决。下面我们就来看看相关的问题如何进行解决。希望在今后的工作中有所帮助。
- Python变量使用中的细节讲解
- Python抓取的具体应用解答
- Python列表与C#语言的相似度介绍
- Python字符串的广泛应用
- Python数据类型在常见集合中的语法
Python序列字符串,列表,元组)
Python序列的第一个元素从0开始,它不但可以从头开始访问,也可以从尾部访问,最后一个元素是a[-1],倒数第二个是a[-2],倒数第i个是a[-i].
列表的创建,遍历,修改等,列表中可以存储不同类型的元素,习惯上都使用列表存储通类型的数据。长度可以在运行时修改。
元组,通常存储异种数据的序列,这个也是习惯,非规则。长度事先确定的,不可以在程序执行期间更改。元组的创建可以访问:
- aList = []for number in range( 1, 11 ): aList += [ number ]
print "The value of aList is:", aList for item in aList: print item,
print for i in range( len( aList ) ): print "%9d %7d" %
( i, aList[ i ] )aList[ 0 ] = -100 aList[ -3 ] = 19print "Value of aList after modification:", aList- 7.3.
- hour = 2
- minute = 12
- second = 34
- currentTime = hour, minute, second # create tuple
- print "The value of currentTime is:", currentTime
Python序列解包
atupe=1,2,3)来创建元组,称为”元组打包”,因为值被“打包到元组中”,元组和其他序列可以“解包”即将序列中存储的值指派给各个标识符。例子:
# create sequencesaString = "abc"aList = [ 1, 2, 3 ]
aTuple = "a", "A",- # unpack sequences to variablesprint "Unpacking string..."
first, second, third = aStringprint "String values:", first,
second, third print "\nUnpacking list..."first, second,
third = aListprint "List values:", first, second, third
print "\nUnpacking tuple..."first, second, third = aTupleprint
"Tuple values:", first, second, third- # swapping two valuesx = 3y = 4 print "\nBefore
swapping: x = %d, y = %d" % ( x, y )x, yy = y, x # swap varia
blesprint "After swapping: x = %d, y = %d" % ( x, y )
以上就是对Python序列的相关介绍。希望对大家有所帮助。
热门文章:
相关内容
- 暂无相关文章
评论关闭