Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 9 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 9 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 9 -

Image of the Page - 9 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 9 -

1.2 APythonProgramwithVariables 9 1.2.3 WhyUseVariables? But why do we introduce variables at all? Would it not be just as fine, or even simpler, to just use the numbersdirectly in the formula? Ifwedid,usingthesamenumericalvalues,ball.pywouldbecomeevenshorter, reading # Program for computing the height of a ball in vertical motion y = 5*0.6 - 0.5*9.81*0.6**2 # vertical position print(y) What is wrong with this? After all, we do get the correct result when running the code! Codingand MathematicalFormulation Ifyoucompare this codedformulawith thecorrespondingmathematical formulation y=v0tβˆ’0.5gt2, theequivalencebetweencodeandmathematics isnotasclearnowas inouroriginal programball.py,where the formulawascodedas y = v0*t - 0.5*g*t**2 Inour littleexamplehere, thismaynotseemdramatic.Generally,however,youbet- ter believe that when, e.g., trying to find errors in code that lacks clear equivalence tothecorrespondingmathematicalformulation,humancodeinterpretationtypically getsmuchharderand it might take youawhile to trackdownthosebugs! ChangingNumericalValues Inaddition, ifwewouldliketoredothecomputation for another point in time, say t = 0.9s, we would have to change the code in two places to arriveat thenewcode line y = 5*0.9 - 0.5*9.81*0.9**2 You may think that this is not a problem, but imagine some other formula (and program) where the same number enters in a 100 places! Are you certain that you can do the right edit in all those places without any mistakes?7 You should realize that by using a variable, you get away with changing in one place only! So, to changethepoint in timefrom0.6 to0.9 inouroriginalprogramball.py,wecould simply change t = 0.6 into t = 0.9. That would be it! Quick and much safer thanediting inmanyplaces. 7 Using the editor to replace 0.6 in all places might seem like a quick fix, but you would have to be sure you did not change 0.6 in the wrong places. For example, another number in the code, e.g.0.666, could easily be turned into 0.966, unless you were careful.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python