python之元组

2024-10-16 05:37:09

1、定义一个元组:例如:In [77]: tupe=(12,33,23.32,"zhang","qing")In [78]: tupeOut[78]: (12, 33, 23.32, 'zhang', 'qing')In [79]: tupe1=(12,(12,34),"zhang")In [80]: tupe1Out[80]: (12, (12, 34), 'zhang')In [81]: tupe2=("zhang",[12,34,"zhang"],23)In [82]: tupe2Out[82]: ('zhang', [12, 34, 'zhang'], 23)In [83]: tupe3=('zhang',{"name":"zhang"},12)In [84]: tupe3Out[84]: ('zhang', {'name': 'zhang'}, 12)

python之元组

3、访问元组(二):In [92]: tupe1Out[92]: (12, (12, 34), 'zhang')In [93]: tupe1[0]Out[93]: 12In [94]: tupe1[1]Out[94]: (12, 34)In [95]: tupe1[2]Out[95]: 'zhang'In [96]: tupe1[1][1]Out[96]: 34In [97]: tupe1[1][0]Out[97]: 12

python之元组

5、元组内置函数之count:count 与字符串和列表中的用法相同例如:In [102]: tupeOut[102]: (12, 33, 23.32, 'zhang', 'qing')In [103]: tupe.index(12)Out[103]: 0In [104]: tupe.index(33)Out[104]: 1In [105]: tupe.index(3)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-105-0a22a2aab0ff> in <module>()----> 1 tupe.index(3)ValueError: tuple.index(x): x not in tupleIn [106]: tupe.index('zhang',0,4)Out[106]: 3In [107]: tupe.index('zhang',0,3)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-107-19c37b0692f1> in <module>()----> 1 tupe.index('zhang',0,3)ValueError: tuple.index(x): x not in tuple

python之元组
猜你喜欢