2. tuple

  • Can store elements of any types

  • Immutable - cannot add, modify or remove items

2.1. Initializing

  • tuple() is more readable

  • () is used more often

2.1.1. Initialize empty

my_tuple = ()
my_tuple = tuple()

2.1.2. Initialize with one element

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

  • Brackets are optional

my_tuple = 1,
my_tuple = (1,)

2.1.3. Initialize with many elements

  • Comma after last element is optional

  • Brackets are optional

my_tuple = 1, 2
my_tuple = (1, 2)
my_tuple = 1, 2.0, None, False, 'Iris'
my_tuple = (1, 2.0, None, False, 'Iris')

2.2. Accessing elements

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'

2.3. Length of a tuple

my_tuple = (1, 2, 3)

len(my_tuple)
# 3

2.4. Assignments

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