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 - 7 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 7 -

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

Text of the Page - 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)
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