Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Accessing Elements in a Tuple: Indexing Technique | Mastering Python Tuples
Python Data Structures
course content

Kurssisisältö

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Accessing Elements in a Tuple: Indexing Technique

Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0, just like with lists.

1234567
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the second movie print(movies[1]) # Accessing the sixth movie print(movies[5])
copy

Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1, the second to last is -2, and so on.

1234567
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the last movie print(movies[-1]) # Accessing the fourth movie from the end print(movies[-4])
copy

In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).

Tehtävä

Swipe to start coding

We are continuing to work with the tuple space_movies.

  • Get the first element of this tuple and store it in the variable first_movie.
  • Get the last element of the tuple and store it in the variable last_movie.
  • Initialize a new list favorite_movies and add these 2 movies to it.
  • Use indices to complete this task.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
toggle bottom row

book
Accessing Elements in a Tuple: Indexing Technique

Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0, just like with lists.

1234567
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the second movie print(movies[1]) # Accessing the sixth movie print(movies[5])
copy

Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1, the second to last is -2, and so on.

1234567
movies = ("Inception", "Interstellar", "The Dark Knight", "Tenet", "Dunkirk", "Memento", "Following") # Accessing the last movie print(movies[-1]) # Accessing the fourth movie from the end print(movies[-4])
copy

In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).

Tehtävä

Swipe to start coding

We are continuing to work with the tuple space_movies.

  • Get the first element of this tuple and store it in the variable first_movie.
  • Get the last element of the tuple and store it in the variable last_movie.
  • Initialize a new list favorite_movies and add these 2 movies to it.
  • Use indices to complete this task.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt