DS Sciprog Lab Release 0.1.0
Total Page:16
File Type:pdf, Size:1020Kb
DS Sciprog Lab Release 0.1.0 David Leoni Aug 27, 2020 CONTENTS 1 News 3 2 Slides 5 3 Office hours 7 4 Labs timetable 9 5 Tutoring 11 6 Exams 13 6.1 Schedule................................................. 13 6.2 Exam modalities............................................. 13 6.3 Exams how to.............................................. 14 6.4 Expectations............................................... 15 6.5 Grading.................................................. 16 6.6 Exams FAQ................................................ 16 6.7 Past exams................................................ 17 7 Resources 19 7.1 Part A Resources............................................. 19 7.2 Part B Resources............................................. 21 7.3 Editors.................................................. 21 7.4 Further readings............................................. 21 8 Acknoledgements 23 9 Past Exams 25 9.1 Data science............................................... 25 9.2 2017-18 (QCB).............................................. 328 9.3 2016-17 (QCB).............................................. 328 10 Slides 2019/20 329 10.1 Part A................................................... 329 11 Commandments 339 12 Introduction solutions 343 12.1 Download exercises zip......................................... 343 12.2 Installation................................................ 343 12.3 Python tutor............................................... 345 12.4 System console.............................................. 345 i 12.5 Python interpreter............................................ 345 12.6 Visual Studio Code............................................ 347 12.7 The debugger............................................... 352 12.8 Jupyter.................................................. 354 12.9 Course exercise formats......................................... 356 12.10 Exercises................................................. 357 13 Python basics solutions 361 13.1 Download exercises zip......................................... 361 13.2 Modules................................................. 361 13.3 Objects.................................................. 362 13.4 Variables................................................. 362 13.5 Numeric types.............................................. 365 13.6 Booleans................................................. 365 13.7 Numeric operators............................................ 368 13.8 Real numbers............................................... 370 14 Strings solutions 373 14.1 Download exercises zip......................................... 373 14.2 What to do................................................ 373 14.3 Introduction............................................... 374 14.4 Exercises with functions......................................... 381 14.5 Verify comprehension.......................................... 385 14.6 Further resources............................................. 392 15 Lists solutions 393 15.1 Download exercises zip......................................... 393 15.2 What to do................................................ 393 15.3 Introduction............................................... 394 15.4 Exercises with functions......................................... 412 15.5 Verify comprehension.......................................... 416 15.6 Recursive operations........................................... 440 15.7 Further exercises............................................. 443 16 Tuples solutions 445 16.1 Download exercises zip......................................... 445 16.2 What to do................................................ 445 16.3 Introduction............................................... 446 16.4 Verify comprehension.......................................... 451 17 Sets solutions 453 17.1 Download exercises zip......................................... 453 17.2 introduction................................................ 453 18 Dictionaries solutions 459 18.1 Download exercises zip......................................... 459 18.2 What to do................................................ 459 18.3 Introduction............................................... 460 18.4 Dictionary methods........................................... 462 18.5 Exercises with functions......................................... 469 18.6 Verify comprehension.......................................... 471 19 Control flow solutions 485 19.1 Download exercises zip......................................... 485 19.2 Introduction............................................... 485 ii 19.3 Execution flow.............................................. 486 19.4 Conditionals............................................... 486 19.5 Loops................................................... 490 19.6 Exercises................................................. 497 20 Functions - solutions 505 20.1 Download exercises zip......................................... 505 20.2 Introduction............................................... 505 20.3 Namespace and variable scope...................................... 509 20.4 Argument passing............................................ 510 20.5 Simple exercises............................................. 514 20.6 Verify comprehension.......................................... 519 20.7 Lambda functions............................................ 522 21 Error handling and testing solutions 525 21.1 Download exercises zip......................................... 525 21.2 Introduction............................................... 525 21.3 Unforeseen situations.......................................... 526 21.4 Check with the return.......................................... 527 21.5 Exceptions................................................ 528 21.6 assert................................................... 532 21.7 Testing.................................................. 535 21.8 Testing with asserts............................................ 536 21.9 Testing with Unittest........................................... 543 21.10 Functional programming......................................... 551 22 Matrices: list of lists solutions 553 22.1 Download exercises zip......................................... 553 22.2 Introduction............................................... 553 22.3 What to do................................................ 553 22.4 Exercises................................................. 555 23 Matrices: Numpy solutions 593 23.1 Download exercises zip......................................... 593 23.2 Introduction............................................... 593 23.3 np.array.................................................. 594 23.4 NaNs and infinities............................................ 596 23.5 Verify comprehension.......................................... 606 24 Data formats solutions 621 24.1 Download exercises zip......................................... 621 24.2 Introduction............................................... 621 24.3 1. line files................................................ 622 24.4 2. File CSV................................................ 633 25 Graph formats solutions 651 25.1 Download exercises zip......................................... 651 25.2 Introduction............................................... 651 25.3 Adjacency lists.............................................. 677 25.4 Networkx................................................. 686 25.5 Simple statistics............................................. 691 26 Visualization solutions 701 26.1 Download exercises zip......................................... 701 26.2 Introduction............................................... 701 iii 26.3 First example............................................... 702 26.4 Introducting numpy........................................... 706 26.5 Bar plots................................................. 715 26.6 Frequency histogram........................................... 722 26.7 Showing plots side by side........................................ 726 26.8 Other plots................................................ 729 27 Pandas solutions 731 27.1 Download exercises zip......................................... 731 27.2 1. Introduction.............................................. 731 27.3 2. Data analysis of Astro Pi....................................... 732 27.4 3. Indexing, filtering, ordering...................................... 738 27.5 4. MatPlotLib review........................................... 743 27.6 5. Calculating new columns....................................... 749 27.7 6. Object values............................................. 750 27.8 7. Transforming............................................. 755 27.9 8. Grouping................................................ 760 27.10 9. Exercise: meteo average temperatures................................ 767 27.11 10. Merging tables............................................ 772 27.12 11. Other exercises............................................ 779 28 Binary relations solutions 781 28.1 Download exercises zip......................................... 781 28.2 Introduction............................................... 781 28.3 Exercises................................................. 783 28.4 Further resources............................................. 795 29 OOP 797 29.1 Download exercises zip......................................... 797 29.2 What to do................................................ 797 29.3 1. Abstract Data