python之元组

2025-10-18 06:12:21

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]: tupe1

Out[80]: (12, (12, 34), 'zhang')

In [81]: tupe2=("zhang",[12,34,"zhang"],23)

In [82]: tupe2

Out[82]: ('zhang', [12, 34, 'zhang'], 23)

In [83]: tupe3=('zhang',{"name":"zhang"},12)

In [84]: tupe3

Out[84]: ('zhang', {'name': 'zhang'}, 12)

python之元组

2、访问元组(一):

In [85]: tupe

Out[85]: (12, 33, 23.32, 'zhang', 'qing')

In [86]: tupe[0]

Out[86]: 12

In [87]: tupe[1]

Out[87]: 33

In [88]: tupe[2]

Out[88]: 23.32

In [89]: tupe[3]

Out[89]: 'zhang'

In [90]: tupe[1,3]

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-90-3f0c13dc8437> in <module>()

----> 1 tupe[1,3]

TypeError: tuple indices must be integers or slices, not tuple

In [91]: tupe[1:3]

Out[91]: (33, 23.32)

python之元组

3、访问元组(二):

In [92]: tupe1

Out[92]: (12, (12, 34), 'zhang')

In [93]: tupe1[0]

Out[93]: 12

In [94]: tupe1[1]

Out[94]: (12, 34)

In [95]: tupe1[2]

Out[95]: 'zhang'

In [96]: tupe1[1][1]

Out[96]: 34

In [97]: tupe1[1][0]

Out[97]: 12

python之元组

4、修改元组:

注意:不能对元组进行修改

In [98]: tupe

Out[98]: (12, 33, 23.32, 'zhang', 'qing')

In [99]: tupe[1]

Out[99]: 33

In [100]: tupe[1]=123

---------------------------------------------------------------------------

TypeError                       冷陵愁          Traceback (most recent call last)

<块八ipython-input-100-d8823ba5f314> in <module>()

----> 1 tupe[1]=123

TypeError: 'tuple' object does not support item assignment

In [101]: tupe[1]="zhang"

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-101-7e6f9e95e567> in <module>()

---->关塑 1 tupe[1]="zhang"

TypeError: 'tuple' object does not support item assignment

python之元组

5、元组内置函数之count:

count 与字符串和列表中的用法相同

例如:

In [102]: tupe

Out[102]: (12, 33, 23.32, 'zhang', 'qing')

In [103]: tupe.index(12)

Out[103]: 0

In [104]: tupe.index(33)

Out[104]: 1

In [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 tuple

In [106]: tupe.index('zhang',0,4)

Out[106]: 3

In [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之元组

6、元组的内置函数之index

index与字符串和列表中的用法相同

例如:

In [110]: tupe

Out[110]: (12, 33, 23.32, 'zhang', 'qing')

In [111]: tupe.count("zhang")

Out[111]: 1

In [112]: tupe.count("qing")

Out[112]: 1

In [113]: tupe.count(12)

Out[113]: 1

python之元组

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:179
  • 阅读量:56
  • 阅读量:33
  • 阅读量:70
  • 阅读量:192
  • 猜你喜欢