Download presentation
Presentation is loading. Please wait.
1
Numpy, pylab, matplotlib (follow-up)
Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Numpy, pylab, matplotlib
2
Follow-up from Prof. Heineman
Lecture notes from Friday are here:– Link also on course website (under Lecture Notes) Useful stuff at end of notes, beyond what he covered in lecture Read especially, §1.7, §1.8 Try the “Debugging Challenge” §1.8 Questions on quizzes like this CS-2301, D-Term 2014 Introduction
3
Follow-up from Prof. Heineman (continued)
Pylab Another way of accessing matplotlib I.e., a .py script:– from matplotlib.pylab import * import matplotlib.pylab __doc__ = matplotlib.pylab.__doc__ matplotlib.pylab is a longer script Containing # provide the recommended moduleabbrevs in # the pylab namespace import matplotlib.pyplot as plt import numpy as np import numpy.ma as ma Is anyone still having trouble installing matplotlib? CS-2301, D-Term 2014 Introduction
4
Follow-up from Prof. Heineman — numpy
Principal class is ‘array’ Somewhat like lists but, Multiple dimensions Different way of indexing 2nd and subsequent dimensions More efficient internally Tutorial: Check it out Much will be over your head now More useful after end of course CS-2301, D-Term 2014 Introduction
5
Numpy and arrays — Example
L = [1, 2, 3, 4, 5] M = [] N = [] P = [] for x in L: M.append(x*x) N.append(sqrt(x)) P.append(2**x) Q = [L, M, N, P] Q1 = numpy.array(Q) With Python lists Q[2][3] With numpy arrays Q1[2, 3] CS-2301, D-Term 2014 Introduction
6
Questions? CS-2301, D-Term 2014 Introduction
7
Follow-up from Prof. Heineman — “Compile often!”
What does this mean? Especially since Python is an interpretive language? Example:– typing code into Python shell Errors show up (almost) immediately Example:– typing into a module When do errors show up? What does “Run module” menu command do? Resets shell (Virtually) “types” content of module into shell As if you typed it yourself! Picks up some errors … … as interpreter But some errors cannot be caught until you actually call the function! CS-2301, D-Term 2014 Introduction
8
Additional advice — “Test often”
Example 1 — Homework #2 Q3 asks you to print out information for each term Why? Answer:– so you can see that it is working correctly! How many had it working the first time you tried it? Q6 asks you to use the function you developed in Q3 100 times (or more) What about all those lines of debug output? Options:– Comment out the print lines Copy Q3 function to Q6 function with new name Remove or comment out print lines Write a separate test program or function CS-2301, D-Term 2014 Introduction
9
Example — Pong Separate into two parts Physics part:–
Physics of the motion Display in window Physics part:– Motion in each dimension is independent of other dimension Function:– move1D(pos, vel, uBound, lBound) Returns new positions, accounting for bouncing off edges Testing physics part:– Set up some (artificial) parameters In a loop, call move1D(), get and print new position, use for next call Examine manually CS-2301, D-Term 2014 Introduction
10
Questions? CS-2301, D-Term 2014 Introduction
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.