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 61. Indexing from start
text = 'We choose to go to the Moon!'

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

text[-1]        # '!'
text[-5]        # 'M'
Listing 63. 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 64. Indexing str
DATA = 'abcde'

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

DATA[-0]            # 'a'
DATA[-1]            # 'e'
DATA[-2]            # 'd'
Listing 65. 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 66. 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 67. 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 2, 6, 9 and add to OUTPUT

  5. Convert to tuple data from row 12, 15, 16 and add to OUTPUT

  6. Add empty set 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 OUTPUT: list

  4. Przekonwertuj do list dane z wierszy 2, 6, 9 i dodaj do OUTPUT

  5. Przekonwertuj do tuple dane z wierszy 12, 15, 16 i dodaj do OUTPUT

  6. Dodaj pusty set do OUTPUT

  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
OUTPUT: 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