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

Page - 13 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 13 -

Image of the Page - 13 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 13 -

1.5 MoreBasicConcepts 13 Sometimesyouwould like to repeat a commandyouhavegivenearlier, or per- haps give a command that is almost the same as an earlier one. Thenyou canuse theup-arrowkey. Pressing thisone timegivesyou thepreviouscommand,pressing two timesgivesyou thecommandbefore that, andsoon.With thedown-arrowkey you can go forward again. When you have the relevant command at the prompt, youmayedit it beforepressingenter (which letsPythonread it and takeaction). 1.5.2 Arithmetics,ParenthesesandRoundingErrors When the arithmetic operators+, -, *, / and ** appear in an expression, Python gives themacertainprecedence. Pythoninterprets theexpressionfromleft to right, takingoneterm(partofexpressionbetweentwosuccessive+or-)ata time.Within each term, ** is done before * and /. Consider the expression x = 1*5**2 + 10*3 - 1.0/4. There are three terms here and interpreting this, Python starts from the left. In thefirst term,1*5**2, it first does5**2which equals 25. This is thenmultipliedby1 togive25again. Thesecond termis10*3, i.e., 30. So thefirst two termsaddup to55. The last termgives0.25, so thefinal result is 54.75which becomes thevalueofx. Note that parentheses are often very important to group parts of expressions together in the intendedway. Let us sayx = 4 and that youwant to divide1.0 by x + 1. We know the answer is 0.2, but thewaywe present the task to Python is critical, as shownby the followingexample. In [1]: x = 4 In [2]: 1.0/x+1 Out [2]: 1.25 In [3]: 1.0/(x+1) Out [3]: 0.20000000000000001 In the first try, we see that 1.0 is divided by x (i.e., 4), giving 0.25, which is then added to 1. Python did not understand that our complete denominator was x+1. In our second try, we used parentheses to β€œgroup” the denominator, andwe gotwhatwewanted. That is,almostwhatwewanted! Sincemost numberscanbe represented only approximately on the computer, this gives rise towhat is called rounding errors. We should have got 0.2 as our answer, but the inexact number representationgaveasmall error.Usually, sucherrorsare sosmall compared to the other numbers of the calculation, that we do not need to botherwith them. Still, keep it inmind, sinceyouwill encounter this issue fromtime to time.Moredetails regardingnumber representationsonacomputer is given inSection3.4.3. 1.5.3 VariablesandObjects Variables inPythonwill be of a certain type. If youhave an integer, say youhave written x = 2 in some Python program, then x becomes an integer variable, i.e., a variable of type int. Similarly, with the statement x = 2.0, x becomes a float
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
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