Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 9 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 9 -

Bild der Seite - 9 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python