Home >Backend Development >Python Tutorial >Detailed knowledge of Python tuples

Detailed knowledge of Python tuples

小云云
小云云Original
2018-03-30 17:00:002070browse

This article mainly shares with you the detailed knowledge of Python tuples. I hope it can help everyone.

1. The differences between Python’s tuple and list classes:

a. The elements of the tuple cannot be modified, but the list can.

b. Use parentheses for tuples and square brackets for lists.

2. Tuple creation is very simple, just add elements in brackets and separate them with commas.

a. Create an empty tuple

tup = ()

b. When the tuple contains only one element, you need to add it after the element comma? It seems that it is OK without adding a comma (no error is reported when executing), to be confirmed

tup = (50,)

3, Tuple is similar to a string, subscript index Starting from 0, interception, combination, etc. can be performed.

4. Two tuples can be combined and spliced, but the elements in the tuple cannot be modified

tup1 = ('China','America','England')

tup2 = (1,2,3,4,5)

## tup3 = tup1 + tup2                                                                          NG

tup1[1] = 'Japan'                                                            NG

5. Elements in the tuple cannot be deleted, but you can use the del statement to delete The entire tuple

tup = (1,2,3,4,5)

## del tup[0]           NG

Del Tup OK After the operation is performed, the variable Tup does not exist. The operation of the variable will report an error.


##Tuple operator

Like strings, tuples can be connected using + and * signs Operation. This means that they can be combined and copied, resulting in a new tuple.

##3 in (1, 2, 3)TrueWhether the element existsfor x in (1, 2, 3): print x,1 2 3Iteration

Tuple index, interception

Because a tuple is also a sequence, we can access the element at a specified position in the tuple, or intercept a section of elements in the index, as shown below:

Tuple:

L = ('spam', 'Spam', 'SPAM!')
Python expression Result Description
len((1, 2, 3) ) 3 Calculate the number of elements
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Connection
('Hi!',) * 4 (' Hi!', 'Hi!', 'Hi!', 'Hi!') Copy
Python expression Result Description
L[2] 'SPAM!' Read the third element
L[-2] 'Spam' Reverse reading; read the penultimate element
L[1:] ('Spam ', 'SPAM!') Intercept element

无关闭分隔符

任意无符号的对象,以逗号隔开,默认为元组,如下实例:

#!/usr/bin/pythonprint 'abc', -4.24e93, 18+6.6j, 'xyz';x, y = 1, 2;print "Value of x , y : ", x,y;

以上实例运行结果:

abc -4.24e+93 (18+6.6j) xyzValue of x , y : 1 2

元组内置函数

Python元组包含了以下内置函数

序号 方法及描述
1 cmp(tuple1, tuple2)
比较两个元组元素。
2 len(tuple)
计算元组元素个数。
3 max(tuple)
返回元组中元素最大值。
4 min(tuple)
返回元组中元素最小值。
5 tuple(seq)
将列表转换为元组。

Related recommendations:

Instance analysis of Python tuple creation, assignment and update and delete operations

Detailed introduction to python tuples and dictionaries

Analysis of python tuple operation examples

The above is the detailed content of Detailed knowledge of Python tuples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn