5.2. tuple

  • Can store elements of any types

  • Immutable - cannot add, modify or remove items

5.2.1. Initializing

  • tuple() is more readable

  • () is used more often

5.2.1.1. Initialize empty

my_tuple = ()
my_tuple = tuple()

5.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,)

5.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')

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

5.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'

5.2.3. tuple vs. others

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

5.2.3.1. 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

5.2.4. Length of a tuple

my_tuple = (1, 2, 3)

len(my_tuple)
# 3

5.2.5. Assignments

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