Page - 26 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 26 -
Text of the Page - 26 -
26 1 TheFirstFewSteps
commentsonconstructions that are fastor slow,but themain focusof thisbook
is to teachhowtowrite correctprograms,not the fastest possibleprograms.
Deletingdatanolongerinuse
Pythonhasautomaticgarbagecollection,meaningthat there isnoneedtodelete
variables (or objects) that are no longer in use. Python takes care of this by
itself. This is opposed to, e.g.,Matlab,where explicit deleting sometimesmay
be required.
Tip:howtodealwithlonglines
Ifastatement inaprogramgets toolong, itmaybecontinuedon thenext lineby
inserting a back-slash at the end of the line before proceeding on the next line.
However,noblanksmustoccurafter theback-slash!
The present introductory book just provides a tiny bit of all the functionality
that Pythonhas to offer. An important sourceof information is the official Python
documentationwebsite (http://docs.python.org/),whichprovidesaPython tutorial,
the Python Library Reference, a Language Reference, andmore. Several excel-
lentbooksarealsoavailable(http://wiki.python.org/moin/PythonBooks),butnotso
manywith a scientific computing focus. One exception is Langtangen’s compre-
hensivebookA Primer onScientific Programmingwith Python, Springer,2016.
1.6 Exercises
Exercise1.1:Errormessages
Saveacopyof theprogramball.pyandconfirmthat thecopyrunsas theoriginal.
You are nowsupposed to introduce errors in the code, one byone. For each error
introduced, save and run the program, and comment howwell Python’s response
corresponds to the actual error. When you are finishedwith one error, re-set the
programtocorrectbehavior(andcheckthat itworks!) beforemovingonto thenext
error.
a) Insert thewordhelloon theempty lineabove theassignment tov0.
b) Removethe# sign in frontof thecomment initial velocity.
c) Removethe= sign in theassignment tov0.
d) Change the reservedwordprint intopint.
e) Change thecalculationofy toy = v0*t.
f) Change the lineprint y toprint x.
g) Replace the statement
y = v0*t - 0.5*g*t**2
by
y = v0*t - (1/2)*g*t**2
Filename:testing_ball.py.
back to the
book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik