You are on page 1of 4

2021/5/13 4.2. Tuples — Python Notes (0.14.

0)

Python Notes (0.14.0)


P R E VIOUS | N E X T | M O D ULES | I N D EX

4.2. Tuples
SEARCH
Contents
Tuples Go
Constructing tuples Enter search terms
Tuple methods or a module, class or
Interests of tuples function name.
Misc This page: Show
source.
In Python, tuples are part of the standard language.
TABLE OF
This is a data structure very similar to the list data CONTENTS
structure. The main difference being that tuple
1. Quick Start /
manipulation are faster than list because tuples are
Tutorial
immutable.
2. Variables,
expressions,
4.2.1. Constructing tuples ¶ statements,
To create a tuple, place values within brackets: types

>>> l = (1, 2, 3) 1. Iterators


>>> l[0]
1 2. Generators

It is also possible to create a tuple without 3. Exceptions


parentheses, by using commas: 4. Decorators
>>> l = 1, 2 5. Classes
>>> l
(1, 2) 6. Namespace and
If you want to create a tuple with a single element, you scoping rules
must use the comma:
7. Packaging
>>> singleton = (1, )
8. Notes about
You can repeat a tuples by multiplying a tuple by a sorting lists and
number: dictionaries

>>> (1,) * 5 9. Notes about


(1, 1, 1, 1, 1) booleans and
Note that you can concatenate tuples and use logical operators
augmented assignement (*=, +=): 10. Notes on
>>> s1 = (1,0)
>>> s1 += (1,)
>>> s1
(1, 0, 1)

https://thomas-cokelaer.info/tutorials/python/tuples.html 1/4
2021/5/13 4.2. Tuples — Python Notes (0.14.0)

4.2.2. Tuple methods


Tuples are optimised, which makes them very simple
objects. There are two methods available only:

index, to find occurence of a value


count, to count the number of occurence of SEARCH
a value
Go
>>> l = (1,2,3,1) Enter search terms
>>> l.count(1) or a module, class or
2
>>> l.index(2) function name.
1 This page: Show
source.
TABLE OF
4.2.3. Interests of tuples CONTENTS
So, Tuples are useful because there are 1. Quick Start /
Tutorial
faster than lists 2. Variables,
protect the data, which is immutable expressions,
tuples can be used as keys on dictionaries statements,
types
In addition, it can be used in different useful ways:
1. Iterators

4.2.3.1. Tuples as key/value pairs to build 2. Generators


dictionaries
3. Exceptions
>>> d = dict([('jan', 1), ('feb', 2), ('march', 3)
>>> d['feb'] 4. Decorators
2
5. Classes

6. Namespace and

4.2.3.2. signing multiple values scoping rules

>>> (x,y,z) = ('a','b','c') 7. Packaging


>>> x
'a' 8. Notes about
>>> (x,y,z) = range(3) sorting lists and
>>> x
dictionaries
0
9. Notes about
booleans and
4.2.3.3. Tuple Unpacking
logical operators
Tuple unpacking allows to extract tuple elements
automatically is the list of variables on the left has the 10. Notes on
same number of elements as the length of the tuple

>>> data = (1,2,3)


>>> x, y, z = data
>>> x
1

https://thomas-cokelaer.info/tutorials/python/tuples.html 2/4
2021/5/13 4.2. Tuples — Python Notes (0.14.0)

4.2.3.4. Tuple can be use as swap function


This code reverses the contents of 2 variables x and y:

>>> (x,y) = (y,x)

Warning
Consider the following function:

def swap(a, b):


SEARCH
(b, a) = (a, b)
then: Go
a = 2 Enter search terms
b = 3 or a module, class or
swap(a, b)
#a is still 2 and b still 3 !! a and b ar function name.

This page: Show


source.

4.2.4. Misc TABLE OF


CONTENTS
1. Quick Start /
4.2.4.1. length Tutorial
To find the length of a tuple, you can use the len()
2. Variables,
function:
expressions,
>>> t= (1,2) statements,
>>> len(t) types
2
1. Iterators

4.2.4.2. Slicing (extracting a segment) 2. Generators

>>> t = (1,2,3,4,5) 3. Exceptions


>>> t[2:]
(3, 4, 5) 4. Decorators

5. Classes
4.2.4.3. Copy a tuple 6. Namespace and
To copy a tuple, just use the assignement: scoping rules

>>> t = (1, 2, 3, 4, 5) 7. Packaging


>>> newt = t
>>> t[0] = 5 8. Notes about
>>> newt
sorting lists and
(1, 2, 3, 4, 5)
dictionaries
Warning
9. Notes about
You cannot copy a list with the = sign because lists booleans and
are mutables. The = sign creates a reference not a
copy. Tuples are immutable therefore a = sign does logical operators
not create a reference but a copy as expected.

10. Notes on

4.2.4.4. Tuple are not fully immutable !!


If a value within a tuple is mutable, then you can
change it:

https://thomas-cokelaer.info/tutorials/python/tuples.html 3/4
2021/5/13 4.2. Tuples — Python Notes (0.14.0)

>>> t = (1, 2, [3, 10])


>>> t[2][0] = 9
>>> t
(1, 2, [9, 10])

4.2.4.5. Convert a tuple to a string


You can convert a tuple to a string with either: SEARCH
>>> str(t)
Go
or Enter search terms

>>> `t` or a module, class or


function name.

This page: Show


4.2.4.6. math and comparison source.
comparison operators and mathematical functions can TABLE OF
be used on tuples. Here are some examples: CONTENTS
>>> t = (1, 2, 3) 1. Quick Start /
>>> max(t) Tutorial
3
2. Variables,
expressions,
statements,
types

1. Iterators

2. Generators

3. Exceptions

4. Decorators

5. Classes

6. Namespace and
scoping rules

7. Packaging

8. Notes about
sorting lists and
dictionaries

9. Notes about
booleans and
logical operators

10. Notes on

https://thomas-cokelaer.info/tutorials/python/tuples.html 4/4

You might also like