Contents
8.4 Challenge: FindtheFactorsofaNumber . . . . . . . 206
8.5 BreakOutofthePattern . . . . . . . . . . . . . . . 207
8.6 RecoverFromErrors . . . . . . . . . . . . . . . . . 211
8.7 Simulate Events and Calculate Probabilities . . . . . 217
8.8 Challenge: SimulateaCoinTossExperiment . . . . . 223
8.9 Challenge: SimulateanElection . . . . . . . . . . . 223
8.10 Summary and Additional Resources . . . . . . . . . 224
9 Tuples, Lists, and Dictionaries 226
9.1 TuplesAreImmutableSequences . . . . . . . . . . . 227
9.2 ListsAreMutableSequences . . . . . . . . . . . . . 237
9.3 Nesting,Copying,andSortingTuplesandLists . . . 251
9.4 Challenge: List of lists . . . . . . . . . . . . . . . . . 257
9.5 Challenge: Wax Poetic . . . . . . . . . . . . . . . . 258
9.6 StoreRelationships in Dictionaries . . . . . . . . . . 260
9.7 Challenge: CapitalCityLoop . . . . . . . . . . . . . 270
9.8 HowtoPickaDataStructure . . . . . . . . . . . . . 272
9.9 Challenge: Cats With Hats . . . . . . . . . . . . . . 273
9.10 SummaryandAdditionalResources . . . . . . . . . 274
10 Object-Oriented Programming (OOP) 276
10.1 Define a Class . . . . . . . . . . . . . . . . . . . . . 277
10.2 Instantiate an Object . . . . . . . . . . . . . . . . . 281
10.3 Inherit From Other Classes . . . . . . . . . . . . . . 287
10.4 Challenge: ModelaFarm . . . . . . . . . . . . . . . 296
10.5 SummaryandAdditionalResources . . . . . . . . . 297
11 Modules and Packages 298
11.1 WorkingWithModules . . . . . . . . . . . . . . . . 299
11.2 WorkingWithPackages . . . . . . . . . . . . . . . . 310
11.3 Summary and Additional Resources . . . . . . . . . 318
12 File Input and Output 320
12.1 FilesandtheFileSystem . . . . . . . . . . . . . . . 321
12.2 WorkingWithFilePathsinPython . . . . . . . . . . 324
12.3 CommonFileSystemOperations . . . . . . . . . . . 333
12.4 Challenge: Move All Image Files to a New Directory . 350
10