4.5. Sequence Indexing

4.5.1. Accessing element with index

  • Index must be positive or negative int or zero

  • Index must be less or equal to length of object

  • Negative index starts from the end and go right to left

Listing 59. Indexing from start
text = 'We choose to go to the Moon!'

text[0]         # 'W'
text[1]         # 'e'
text[23]        # 'M'
Listing 60. Indexing from the end
text = 'We choose to go to the Moon!'

text[-1]        # '!'
text[-5]        # 'M'
Listing 61. Accessing not existing element
text = 'We choose to go to the Moon!'

text[100]
# IndexError: string index out of range

text[-100]
# IndexError: string index out of range

4.5.2. Indexing sequences

  • Indexes on set are not possible

Listing 62. Indexing str
DATA = 'abcde'

DATA[0]             # 'a'
DATA[1]             # 'b'
DATA[2]             # 'c'

DATA[-0]            # 'a'
DATA[-1]            # 'e'
DATA[-2]            # 'd'
Listing 63. Indexing list
DATA = ['a', 'b', 'c', 'd', 'e']

DATA[0]             # 'a'
DATA[1]             # 'b'
DATA[2]             # 'c'

DATA[-0]            # 'a'
DATA[-1]            # 'e'
DATA[-2]            # 'd'
Listing 64. Indexing tuple
DATA = ('a', 'b', 'c', 'd', 'e')

DATA[0]             # 'a'
DATA[1]             # 'b'
DATA[2]             # 'c'

DATA[-0]            # 'a'
DATA[-1]            # 'e'
DATA[-2]            # 'd'
Listing 65. Indexing set. Indexes on set are not possible
DATA = {'a', 'b', 'c', 'd', 'e'}

DATA[0]             # TypeError: 'set' object is not subscriptable
DATA[1]             # TypeError: 'set' object is not subscriptable
DATA[2]             # TypeError: 'set' object is not subscriptable

DATA[-0]            # TypeError: 'set' object is not subscriptable
DATA[-1]            # TypeError: 'set' object is not subscriptable
DATA[-2]            # TypeError: 'set' object is not subscriptable

4.5.3. Assignments

4.5.3.1. Select

English
  1. For given data input (see below)

  2. Write header (first line) to header: tuple variable

  3. Create output: list

  4. Convert to list data from row at index 2, 6, 9 and add to output

  5. Convert to tuple data from row at index 12, 15, 16 and add to output

  6. Add empty set, list and tuple to output

  7. Use only indexes

  8. Do not use for, while or slice()

Polish
  1. Dla danych wejściowych (patrz sekcja input)

  2. Zapisz nagłówek (pierwsza linia) do zmiennej header: tuple

  3. Stwórz data: list

  4. Przekonwertuj do list dane z wierszy o indeksach 2, 6, 9 i dodaj do data

  5. Przekonwertuj do tuple dane z wierszy o indeksach 12, 15, 16 i dodaj do data

  6. Dodaj pusty set, list oraz tuple do data

  7. Użyj tylko indeksów

  8. Nie używaj for, while lub slice()

Input
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'),
    (4.6, 3.4, 1.4, 0.3, 'setosa'),
    (5.4, 3.9, 1.7, 0.4, 'setosa'),
    (5.7, 2.8, 4.5, 1.3, 'versicolor'),
    (5.0, 3.6, 1.4, 0.3, 'setosa'),
    (5.5, 2.3, 4.0, 1.3, 'versicolor'),
    (6.5, 3.0, 5.8, 2.2, 'virginica'),
    (6.5, 2.8, 4.6, 1.5, 'versicolor'),
    (6.3, 3.3, 6.0, 2.5, 'virginica'),
    (6.9, 3.1, 4.9, 1.5, 'versicolor'),
    (4.6, 3.1, 1.5, 0.2, 'setosa'),
]
Output
header: tuple
# ('Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species')

data: list
# [[5.1, 3.5, 1.4, 0.2, 'setosa'],
#  [4.7, 3.2, 1.3, 0.2, 'setosa'],
#  [4.9, 3.0, 1.4, 0.2, 'setosa'],
#  (4.6, 3.4, 1.4, 0.3, 'setosa'),
#  (5.0, 3.6, 1.4, 0.3, 'setosa'),
#  (5.5, 2.3, 4.0, 1.3, 'versicolor'),
#  set(), [], ()]
The whys and wherefores
  • Using nested data structures

  • Using indexes

  • Type casting