Page - 10 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 10 -
Text of the Page - 10 -
10 1 TheFirstFewSteps
1.2.4 MathematicalNotationVersusCoding
Make sure you understand that, from the outset, we had a pure mathematical
formulationofour formula
y=v0tβ0.5gt2,
which does not contain any connection to programming at all. Remember, this
formula was derived hundreds of years ago, long before computers entered the
scene!Whenwenextwrotea pieceofcode thatapplied this formula, that codehad
to obey the rules of the programming language, which in this case is Python. This
means,forexample,thatmultiplicationhadtobewrittenwithastar,simplybecause
that is the way multiplication is coded in Python. In some other programming
language, the multiplication could in principle have been coded otherwise, but the
mathematical formulationwouldstill read thesame.
We have seen how the equals sign (=) is interpreted in Python code. This
interpretation is very different from the interpretation in mathematics, as might be
illustratedby the following little example. Inmathematics,x=2βxwould imply
that 2x = 2, giving x = 1. In Python code, however, a code line like x = 2 -
xwould be interpreted, not as an equation, but rather as an assignment statement:
compute the righthandside bysubtracting thecurrentvalueofx from2 and let the
resultbe thenewvalueofx. In thecode, thenewvalueofxcould thusbeanything,
alldependingon thevaluexhadabove theassignmentstatement!
1.2.5 WriteandRunYourFirstProgram
Reading only does not teach you computer programming: you have to program
yourself and practice heavily before you master mathematical problem solving via
programming. In fact, this is very much like learning to swim. Nobody can do that
by just reading about it! You simply have to practice real swimming to get good
at it. Therefore, it is crucial at this stage that you start writing and running Python
programs. We just went through the programball.py above, so let us next write
andrun thatcode.
But first a warning: there are many things that must come together in the right
way for ball.py to run correctly. There might be problems with your Python
installation, with your writing of the program (it is very easy to introduce errors!),
orwith thelocationof thefile, just tomentionsomeofthemostcommondifficulties
forbeginners.Fortunately,suchproblemsaresolvable,andifyoudonotunderstand
how to fix the problem,ask somebody.Very often the guy next to you experienced
thesameproblemandhasalreadyfixed it!
Start up Spyder and, in the editor (left pane), type in each line of the program
ball.pyshownearlier.Thenyousavetheprogram(selectFile->save as)where
you prefer and finally run it (selectRun ->Run, ...or press F5). With a bit of luck,
youshouldnowget thenumber1.2342out intherightmostlowerpaneintheSpyder
GUI. If so, congratulations,you have just executedyourfirst self-written computer
programinPython!
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik