總結

Python 的資料型別

Jason Myers

Instructor

序列型別

  • 串列

['Chocolate Chip', 'Peanut Butter']

  • 具名組(Tuple)

('Sugar', 'Eggs')

  • 字串

'Cookies are wonderful'

Python 的資料型別

字典(Dictionary)

  • 安全新增與移除

squirrels_by_park.pop("City Hall Park", {})

  • 解包項目

for field, value in squirrels_by_park.items():

  • 處理巢狀資料
for park in squirrels_by_park:
    print(squirrels_by_park[park].get('color', 'N/A'))
Python 的資料型別

數值與邏輯型別

  • 整數 int(1)
  • 浮點數 float(1.333333334)
  • 小數 Decimal(5.50)
  • 布林值 True or False
  • 集合 {'Anzac', 'Oatmeal Raisin'}
Python 的資料型別

複合資料型別

  • 計數器

    Counter(nyc_eatery_types)
    
  • 預設字典

    eateries_by_park = defaultdict(list)
    for park_id, name in nyc_eateries_parks:
      eateries_by_park[park_id].append(name)
    
  • 具名元組

    namedtuple('Worm', ['species', 'sex', 'mass'])
    
Python 的資料型別

複合資料型別

  • 資料類別(dataclass)
@dataclass
class WeightEntry:
    species: str
    flipper_length: int
    body_mass: int
    sex: str
Python 的資料型別

恭喜你!

Python 的資料型別

Preparing Video For Download...