3.2. Sequence Tuple

3.2.1. Rationale

  • Can store elements of any types

  • Immutable - cannot add, modify or remove items

3.2.2. Type Definition

  • () is used more often

  • tuple() is more readable

  • Single element tuple require comma at the end (important!)

  • Brackets are optional

  • Comma after last element of one element tuple is optional

data = ()
data = tuple()

data = (1,)
data = (1, 2, 3)
data = (1.1, 2.2, 3.3)
data = (True, False)
data = ('a', 'b', 'c')
data = ('a', 1, 2.2, True, None)

data = 1,
data = 1, 2, 3
data = 1.1, 2.2, 3.3
data = True, False
data = 'a', 'b', 'c'
data = 'a', 1, 2.2, True, None

3.2.3. Type Casting

  • tuple() converts argument to tuple

data = 'abcd'
tuple(data)
# ('a', 'b', 'c', 'd')
data = ['a', 'b', 'c', 'd']
tuple(data)
# ('a', 'b', 'c', 'd')
data = ('a', 'b', 'c', 'd')
tuple(data)
# ('a', 'b', 'c', 'd')
data = {1, 2, 3}
tuple(data)
# ('a', 'b', 'c', 'd')
data = frozenset({'a', 'b', 'c', 'd'})
tuple(data)
# ('a', 'b', 'c', 'd')

3.2.4. Get Item

data = ('a', 'b', 'c', 'd')

data[0]         # 'a'
data[1]         # 'b'
data[2]         # 'c'
data[3]         # 'd'

3.2.5. Tuple or Int, Float, Str

data = 1
type(data)
# <class 'int'>

data = 1,
type(data)
# <class 'tuple'>

data = 1.
type(data)
# <class 'float'>
type(1.2)        # <class 'float'>
type(1,2)        # <class 'tuple'>
type(1.2,)       # <class 'tuple'>
type(1,2.3)      # <class 'tuple'>

type(1.)         # <class 'float'>
type(1,)         # <class 'tuple'>
type(1.,)        # <class 'tuple'>
type(.2)         # <class 'float'>
type(.2,)        # <class 'tuple'>
type(1.2)        # <class 'float'>
type(1)          # <class 'int'>

type(1.,1.)      # <class 'tuple'>
type(.2,.2)      # <class 'tuple'>
type(1.,.2)      # <class 'tuple'>

type('foo')      # <class 'str'>
type('foo',)     # <class 'tuple'>
type('foo'.)     # SyntaxError: invalid syntax

3.2.6. Tuple or List

Both:

  • ordered

  • possible to getitem and slice

  • elements can be duplicated

  • elements of any types

Tuple:

  • immutable

  • one contingent block of data in memory

List:

  • mutable

  • implemented in memory as list of pointers to objects

  • objects are scattered in memory

../../_images/memory-tuple.png

Figure 3.1. Define tuple

../../_images/memory-list.png

Figure 3.2. Define list

../../_images/memory-all.png

Figure 3.3. Define str, tuple and list

3.2.7. Assignments

3.2.7.1. Sequence Tuple Create

English
  1. Create tuple result with elements:

    • 'a'

    • 1

    • 2.2

  2. Print result

  3. Print number of elements in result

Polish
  1. Stwórz tuple result z elementami:

    • 'a'

    • 1

    • 2.2

  2. Wypisz result

  3. Wypisz liczbę elementów result

3.2.7.2. Sequence Tuple Many

English
  1. Use data from "Input" section (see below)

  2. Create a tuple representing all Species

  3. Calculate mean for each numerical values column

  4. To convert table use multiline select with alt key in your IDE

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Stwórz tuple z nazwami gatunków

  3. Wylicz średnią arytmetyczną dla każdej z kolumn numerycznych

  4. Do przekonwertowania tabelki wykorzystaj zaznaczanie wielu linijek za pomocą klawisza alt w Twoim IDE

Input
"Sepal length", "Sepal width", "Petal length", "Petal width", "Species"
"5.8", "2.7", "5.1", "1.9", "virginica"
"5.1", "3.5", "1.4", "0.2", "setosa"
"5.7", "2.8", "4.1", "1.3", "versicolor"
"6.3", "2.9", "5.6", "1.8", "virginica"
"6.4", "3.2", "4.5", "1.5", "versicolor"
"4.7", "3.2", "1.3", "0.2", "setosa"
"7.0", "3.2", "4.7", "1.4", "versicolor"
"7.6", "3.0", "6.6", "2.1", "virginica"
"4.9", "3.0", "1.4", "0.2", "setosa"
"4.9", "2.5", "4.5", "1.7", "virginica"
"7.1", "3.0", "5.9", "2.1", "virginica"
The whys and wherefores
  • Defining tuple

  • Learning IDE features

Hints
  • mean = sum(...) / len(...)