Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 Tuples

Python3 Tuples

Python tuples are similar to lists, except that the elements of a tuple cannot be modified.

Tuples use parentheses ( ), while lists use square brackets [ ].

Creating a tuple is simple: just add elements inside parentheses and separate them with commas.

Example (Python 3.0+)

tup1

=

(

'Google'

,

'Runoob'

,

1997

,

2000

)

tup2

=

(

1

,

2

,

3

,

4

,

5

)

tup3

=

"a"

,

"b"

,

"c"

,

"d"

 

 Parentheses are not required

type

(

tup3

)

<

class

'tuple'

Create an empty tuple


tup1 = ()

When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses are treated as an operator:

Example (Python 3.0+)

tup1

=

(

50

)

type

(

tup1

)

   

Without the comma, the type is integer

<

class

'int'

tup1

=

(

50

,

)

type

(

tup1

)

   

With the comma, the type is tuple

<

class

'tuple'

Like strings, tuple indices start at 0, and tuples can be sliced, concatenated and so on.


Accessing tuples

Tuples can use subscript indices to access their values, as shown below:

Example (Python 3.0+)

#!/usr/bin/python3

tup1

=

(

'

Google

'

,

'

Runoob

'

,

1997

,

2000

)

tup2

=

(

1

,

2

,

3

,

4

,

5

,

6

,

7

)

print

(

"

tup1[0]:

"

,

tup1

[

0

]

)

print

(

"

tup2[1:5]:

"

,

tup2

[

1

:

5

]

)

The example above produces the following output:


tup1[0]:  Google
tup2[1:5]:  (2, 3, 4, 5)

Modifying tuples

The element values in a tuple cannot be modified, but we can concatenate and combine tuples, as shown below:

Example (Python 3.0+)

#!/usr/bin/python3

tup1

=

(

12

,

34.56

)

tup2

=

(

'

abc

'

,

'

xyz

'

)

The following operation that modifies a tuple element is illegal.

tup1[0] = 100

Create a new tuple

tup3

=

tup1

tup2

print

(

tup3

)

The example above produces the following output:


(12, 34.56, 'abc', 'xyz')

Deleting tuples

The element values in a tuple cannot be deleted, but we can use the del statement to delete the entire tuple, as shown below:

Example (Python 3.0+)

#!/usr/bin/python3

tup

=

(

'

Google

'

,

'

Runoob

'

,

1997

,

2000

)

print

(

tup

)

del

tup

print

(

"

The tuple tup after deletion:

"

)

print

(

tup

)

After the tuple is deleted in the above example, outputting the variable produces an exception message, as shown below:


删除后的元组 tup : 
Traceback (most recent call last):
  File "test.py", line 8, in 
    print (tup)
NameError: name 'tup' is not defined

Tuple operators

Like strings, tuples can use the +, += and * operators for operations. This means they can be combined and copied, producing a new tuple after the operation.

Python expression Result Description
len((1, 2, 3))
3 Count the number of elements

   

>>> a = (1, 2, 3) >>> b = (4, 5, 6) >>> c = a+b >>> c (1, 2, 3, 4, 5, 6) | (1, 2, 3, 4, 5, 6) | Concatenation; c is a new tuple containing all elements of a and b. | |

>>> a = (1, 2, 3)
>>> b = (4, 5, 6)
>>> a += b
>>> a
(1, 2, 3, 4, 5, 6)
| (1, 2, 3, 4, 5, 6) | Concatenation; a becomes a new tuple containing all elements of a and b. | |
('Hi!',) * 4
| ('Hi!', 'Hi!', 'Hi!', 'Hi!') | Copy | |
3 in (1, 2, 3)
| True | Whether the element exists | |
for x in (1, 2, 3):
print (x, end=" ")
| 1 2 3 | Iteration |


Tuple indexing and slicing

Because a tuple is also a sequence, we can access the element at a specified position and slice a range of indices, as shown below:

Tuple:


tup = ('Google', 'Runoob', 'Taobao', 'Wiki', 'Weibo','Weixin')

Python expression Result Description
tup[1] 'Runoob' Read the second element
tup[-2] 'Weibo' Read in reverse, reading the second-to-last element
tup[1:] ('Runoob', 'Taobao', 'Wiki', 'Weibo', 'Weixin') Slice elements, taking all elements starting from the second.
tup[1:4] ('Runoob', 'Taobao', 'Wiki') Slice elements, from the second to the fourth element (index 3).

The running example is as follows:

Example

tup

=

(

'Google'

,

'Runoob'

,

'Taobao'

,

'Wiki'

,

'Weibo'

,

'Weixin'

)

tup

[

1

]

'Runoob'

tup

[

2

]

'Weibo'

tup

[

1

:

]

(

'Runoob'

,

'Taobao'

,

'Wiki'

,

'Weibo'

,

'Weixin'

)

tup

[

1

:

4

]

(

'Runoob'

,

'Taobao'

,

'Wiki'

)


Tuple built-in functions

Python tuples include the following built-in functions

No. Method and description Example
1 len(tuple)
Count the number of elements in a tuple.

   

>>> tuple1 = ('Google', 'Runoob', 'Taobao') >>> len(tuple1) 3 >>> | | 2 | max(tuple)
Return the maximum value among the tuple elements. |

>>> tuple2 = ('5', '4', '8')
>>> max(tuple2)
'8'
>>>
| | 3 | min(tuple)
Return the minimum value among the tuple elements. |
>>> tuple2 = ('5', '4', '8')
>>> min(tuple2)
'4'
>>>
| | 4 | tuple(iterable)
Convert an iterable series into a tuple. |
>>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'Runoob', 'Baidu')
|

About tuples being immutable

What it means for a tuple to be immutable is that the contents in the memory the tuple points to cannot change.

tup

=

(

'r'

,

'u'

,

'n'

,

'o'

,

'o'

,

'b'

)

tup

[

0

]

=

'g'

   

Modifying elements is not supported

Traceback

(

most recent call last

)

:

  File

""

,

line

1

,

in

<

module

TypeError

:

'tuple'

object

does

not

support item assignment

id

(

tup

)

   

View memory addresses

4440687904

tup

=

(

1

,

2

,

3

)

id

(

tup

)

4441088800

   

The memory address is different now

From the above example we can see that the reassigned tuple tup is bound to a new object; the original object was not modified.