4.2. Sequence tuple

  • Can store elements of any types

  • Immutable - cannot add, modify or remove items

4.2.1. 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 is optional

Listing 44. Initialize empty
my_tuple = ()
my_tuple = tuple()
Listing 45. Initialize with one element
my_tuple = 1,
my_tuple = (1,)
Listing 46. Initialize with many elements
my_tuple = 1, 2
my_tuple = (1, 2)
Listing 47. Initialize with many elements
my_tuple = 1, 2.0, None, False, 'Iris'
my_tuple = (1, 2.0, None, False, 'Iris')

4.2.2. Type Annotation

my_tuple: tuple = ()
my_tuple: tuple = tuple()

my_tuple: tuple = ('a', 2, 3.3)
from typing import Tuple

my_tuple: Tuple[int, int, int] = (1, 2, 3)
my_tuple: Tuple[str, str, str] = ('setosa', 'virginica', 'versicolor')
my_tuple: Tuple[str, int, float] = ('a', 2, 3.3)

4.2.3. Membership Operators

Listing 48. Equals
(1, 2) == (1, 2)        # True
(1, 2) == (2, 1)        # False
Listing 49. Not equals
(1, 2, 3) != (1, 2)     # True
(1, 2) != (1, 2)        # False
Listing 50. Contains
1 in (1, 2)             # True
3 in (1, 2)             # False

(2) in (1, 2)           # True
(1, 2) in (1, 2)        # False
Listing 51. Missing
4 not in (1, 2)         # True
1 not in (1, 2)         # False

(2) not in (1, 2)       # False
(1, 2) not in (1, 2)    # True

4.2.4. Getting Items

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

my_tuple[0]         # 'a'
my_tuple[1]         # 'b'
my_tuple[3]         # 'd'
my_tuple = ('a', 'b', 'c', 'd')

my_tuple[-1]        # 'd'
my_tuple[-3]        # 'b'

4.2.5. tuple vs. others

4.2.5.1. tuple vs. str

what = 'foo'      # str
what = 'foo',     # tuple with str
what = 'foo'.     # SyntaxError: invalid syntax
what = ('foo')    # str
what = ('foo',)   # tuple with str
what = ('foo'.)   # SyntaxError: invalid syntax

4.2.5.2. tuple vs. float and int

what = 1.2        # float
what = 1,2        # tuple with two int

what = (1.2)      # float
what = (1,2)      # tuple with two int
what = 1.2,       # tuple with float
what = 1,2.3      # tuple with int and float

what = (1.2,)     # tuple with float
what = (1,2.3)    # tuple with int and float
what = 1.         # float
what = .5         # float
what = 1.0        # float
what = 1          # int

what = (1.)       # float
what = (.5)       # float
what = (1.0)      # float
what = (1)        # int
what = 10.5       # float
what = 10,5       # tuple with two ints
what = 10.        # float
what = 10,        # tuple with int
what = 10         # int

what = (10.5)     # float
what = (10,5)     # tuple with two ints
what = (10.)      # float
what = (10,)      # tuple with int
what = (10)       # int
what = 1.,1.      # tuple with two floats
what = .5,.5      # tuple with two floats
what = 1.,.5      # tuple with two floats

what = (1.,1.)    # tuple with two floats
what = (.5,.5)    # tuple with two floats
what = (1.,.5)    # tuple with two floats

4.2.6. Length

my_tuple = (1, 2, 3)

len(my_tuple)
# 3

4.2.7. When Use tuple or list

4.2.7.1. Tuple

  • is immutable

  • one contingent block of data in memory

4.2.7.2. List

  • mutable

  • implemented in memory as list of pointers to objects

  • objects are scattered in memory

4.2.8. Assignments

4.2.8.1. Create

  • Complexity level: easy

  • Lines of code to write: 13 lines

  • Estimated time of completion: 10 min

  • Filename: solution/tuple_create.py

English
  1. For given data input (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. Dla danych wejściowych (patrz sekcja input)

  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(...)