python入门基础教程随笔2-python数据结构-元组

# encoding:utf-8
number_tuple = (1,2,3,4,5,6,7,8,9,10) # 创建一个元组,元组 创建后 不可更改,这是跟列表list的区别
string_tuple = ('bc-----','cde','aaa','sasdfa')
hunhe_tuple = (1,2,3,'bc')

aaa = number_tuple[4]
bbb = string_tuple[0]
ccc = hunhe_tuple[3]
print(str( aaa )+str(bbb)+str( ccc ))
print('第一个是:{0},第二个是:{1},第三个是:{2}'.format(aaa,bbb,ccc))
print(len(number_tuple+string_tuple+hunhe_tuple)) #打印以上三种列表类型里共有几个元素
print("(hello)" *4)

#元组的截取
print(string_tuple[1])
print(string_tuple[-1])
print(hunhe_tuple[3:])

#创建只包含一个元素的元组
a_tuple = (2,) #需要以为都好来消除歧义

#创建 tuple中的list
mixed_tuple = (1,2,3,['a','b'])
print('mixed_tuple:' + str(mixed_tuple))

#更改混合元组里面的list的值,元组本身没有更改
mixed_tuple[3][0] = 'a--'
mixed_tuple[3][1] = 'b--'
print('mixed_tuple:' + str(mixed_tuple))

posted on 2016-06-19 18:13  python入门基础教程  阅读(128)  评论(0)    收藏  举报