Workspace
Tedilte Abraham/

Course Notes: Data Types for Data Science in Python

0
Beta
Spinner
# Rewrite the for loop to use enumerate
indexed_names = []
for i,name in enumerate(names):
    index_name = (i,name)
    indexed_names.append(index_name) 
print(indexed_names)

# Rewrite the above for loop using list comprehension
indexed_names_comp = [(i,name) for i,name in enumerate(names)]
print(indexed_names_comp)

# Unpack an enumerate object with a starting index of one
indexed_names_unpack = [*enumerate(names, 1)]
print(indexed_names_unpack)
# Create the empty list: labeled_entries
labeled_entries = []

# Iterate over the weight_log entries
for species, sex, flipper_length, body_mass in weight_log:
    # Append a new WeightEntry instance to labeled_entries
    labeled_entries.append(WeightEntry(species, body_mass, flipper_length, sex))
    
# Print a list of the first 5 mass_to_flipper_length_ratio values
print([entry.mass_to_flipper_length_ratio for entry in labeled_entries[:5]])

Import dataclass

from dataclasses import dataclass

@dataclass class WeightEntry: # Define the fields on the class species: str sex: int body_mass: int flipper_length: str

# Define a property that returns the body_mass / flipper_length @property def mass_to_flipper_length_ratio(self): return self.body_mass / self.flipper_length
# Write and run code here
# Import dataclass
from dataclasses import dataclass

@dataclass
class WeightEntry:
    # Define the fields on the class
    species: str
    sex: int
    body_mass: int
    flipper_length: str
        
    # Define a property that returns the body_mass / flipper_length
    @property
    def mass_to_flipper_length_ratio(self):
        return self.body_mass / self.flipper_length
  • AI Chat
  • Code