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 - 7 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 7 -

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

Text der Seite - 7 -

1.2 APythonProgramwithVariables 7 guess) what is meant with the code. In simple cases, comments are probably not muchneeded,but will soonbe justified as the levelofcomplexitystepsup. Thenext line readbyPython is v0 = 5 # Initial velocity In Python, a statement like v0 = 5 is known as an assignment statement. After this assignment, any appearance ofv0 in the code will “represent” the initial velocity, being 5ms−1 in this case. This means that, whenever Python reads v0, it will replace v0 by the integer value 5. One simple way to think of this, might be as follows.With the assignmentv0 = 5, Python generatesa “box” in computer memory with the namev0written on top. The number 5 is then put into that box. Whenever Python later meets the name v0 in the code, it finds the box, opens it, takesout thenumber (here5)andreplaces thenamev0with thenumber. Thenext two lines g = 9.81 # Acceleration of gravity t = 0.6 # Time arealsoassignmentstatements,giving twomore“boxes” incomputermemory.The box named g will contain the value 9.81, while the box named t contains 0.6. Similarly, when Python later reads g and t in the code, it plugs in the numerical values found in thecorrespondingboxes. The assignments in a bitmoredetail When Python interprets the assignment statement v0 = 5, the integer 5 becomes an object of type int and the variable name on the left-hand side becomes a named reference for that object. Similarly, when interpreting the assignment statements g = 9.81 and t = 0.6, g and t become named references to objects created for the real numbers given. However, since we have real numbers, these objects will be of type float (in computer language, a realnumber is calleda “floatingpointnumber”). Now,with theseassignments inplace,Pythonknowsof threevariables(v0,g,t) and their values. These variables are then used by Python when it reads the next line, theactual“formula”, y = v0*t - 0.5*g*t**2 # Vertical position Again, according to its rules, Python interprets*as multiplication,− as minusand ** as exponentiation (let us also add here that, not surprisingly, + and / would havebeenunderstoodasadditionanddivision, if suchsignshadbeenpresent in the expression). Having read the line, Python performs the mathematics on the right- handside,andthenassignstheresult (in thiscasethenumber1.2342)to thevariable namey. Finally,Pythonreads print(y)
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