BaşlayınÜcretsiz Başlayın

Leveraging attributes on namedtuples

Once you have a namedtuple, you can write more expressive code that is easier to understand. Remember, you can access the elements in the tuple by their name as an attribute. For example, you can access the species of the namedtuples in the previous exercise using the .species attribute.

Here, you'll use the tuples you made in the previous exercise to see how this works.

Bu egzersiz

Data Types in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Iterate over the first twenty entryss in the labeled_entries list:
    • If it is a Chinstrap species:
      • Print the entry's sex and body_mass separated by a :.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Iterate over the first twenty entries in labeled_entries
for ____ in ____:
    # if the entry's species equals Chinstrap
    ____
      # Print each entry's sex and body_mass seperated by a colon
      print(f'{____}:{____}')
Kodu Düzenle ve Çalıştır