python basics - tuples

tuple

Python tuples are similar to lists, except that the elements of the tuple cannot be modified. Use parentheses for tuples and square brackets for lists.

aTuple=('hello',77,99,18.6)
print(aTuple)

<1> Access tuple

aTuple=('hello',77,99,18.6)
print(aTuple)
print(aTuple[0])
print(aTuple[3])

Results of the:

 

 

<2> Modify the tuple

 

Note: It is not allowed to modify the data of the tuple in python, including the elements that cannot be deleted.

 

 

<3> The built-in functions of tuples count, index

index and count are used the same as in strings and lists

>>> a = ('a', 'b', 'c', 'a', 'b') >>> a.index('a', 1, 3) # 注意是左闭右开区间 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: tuple.index(x): x not in tuple >>> a.index('a', 1, 4) 3 >>> a.count('b') 2 >>> a.count('d') 0

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121803&siteId=291194637