python元组
Today we are going to learn about Python Tuple. This is very similar to Python List. If you don’t know about list you can find them here.
今天,我们将学习Python Tuple。 这与Python List非常相似。 如果您不知道清单,可以在这里找到它们。
Python元组 (Python Tuple)
Python tuple is a sequence of Python objects. They are lot like lists. But the differences between a tuple and a list is that tuples can not be changed like we can change an item of a list. Also tuples use parentheses while lists use square brackets.
Python元组是一系列Python对象。 他们很像列表。 但是元组和列表之间的区别在于,元组不能像我们可以更改列表的项一样进行更改。 元组也使用括号,而列表使用方括号。
Python Tuple is a sequence of comma-separated values between parentheses. For example;
Python元组是括号之间用逗号分隔的值的序列。 例如;
#an empty tuple
emptyTup=()
#tuple of string
strTup=('This','is','a','tuple')
#tuple of integers
intTup=(1,2,3,4,5)
在Python元组中访问值 (Accessing a Value in Python Tuple)
Accessing a value in a python tuple is quite similar to the way to access an item in a list. Through index number we can access a value in a tuple. First element has index number zero, second has index number one, and so forth.
访问python元组中的值与访问列表中项目的方式非常相似。 通过索引号,我们可以访问元组中的值。 第一个元素的索引号为零,第二个元素的索引号为1,依此类推。
#tuple of string
strTup=('This','is','a','tuple')
#accessing first element
print(strTup[0])
#accessing second element
print(strTup[1])
#accessing fourth element
print(strTup[3])
The output of the above code will be like below-
上面代码的输出如下:
Tuple in python also support negative indexing like list. A negative index means index number from the right of that tuple.
python中的元组还支持负索引,例如列表。 负索引表示该元组右边的索引号。
#tuple of string
strTup=('This','is','a','tuple')
#accessing first element from the right
print(strTup[-1])
#accessing second element from the right
print(strTup[-2])
#accessing second element from the right
print(strTup[-4])
The output will be like below.
输出将如下所示。
在Python Tuple中更新和删除 (Update and Delete in Python Tuple)
As we have mentioned earlier tuples in python are not changeable. So you can not update a single tuple element. But we can take two tuple and concat them to create a new one.
正如我们前面提到的,python中的元组不可更改。 因此,您不能更新单个元组元素。 但是我们可以采用两个元组并合并它们以创建一个新的元组。
Also removing an individual tuple element is not possible. Though we can remove an entire tuple using del
statement.
同样,不能删除单个元组元素。 虽然我们可以使用del
语句删除整个元组。
#tuple 1
tup1=(1,2,3)
#tuple 2
tup2=(4,5)
#tuple 3
tup3=tup1+tup2
print(tup3)
#to delete tuple 1
del tup1
#this will show a traceback as tup1 is deleted. So it is not defined now
print(tup1)
The above code’s output will be like following picture.
上面代码的输出如下图所示。
一些内置的Python Tuple函数 (Some built-in Python Tuple Functions)
There are some built-in functions available to manipulate tuple in python. Take a look at the following code for understanding.
有一些内置函数可用于在python中操作元组。 看下面的代码以了解。
#a string tuple
tup=('this','is','a','tuple')
#len(tuple) gives total length of a tuple
print(len(tup))
#max(tuple) gives maximum element of a tuple
print(max(tup))
#min(tuple) gives minimum element of a tuple
print(min(tup))
#count(x) gives number of occurances of x in the tuple
print(tup.count('is'))
#index(x) gives index of first occurances of x in the tuple
print(tup.index('a'))
You should get the following output if you run the above code.
如果运行上面的代码,则应该获得以下输出。
So that’s it for python tuple. Make sure to run every piece of code on you own. Feel free to leave a comment if you have any doubt.
#happy_coding 🙂
这就是python元组。 确保自己运行每段代码。 如有任何疑问,请随时发表评论。
#happy_coding🙂
Reference: https://docs.python.org/3.7/tutorial/datastructures.html#tuples-and-sequences
参考: https : //docs.python.org/3.7/tutorial/datastructures.html#tuples-and-sequences
python元组