Page - 14 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 14 -
Text of the Page - 14 -
14 1 TheFirstFewSteps
variable (thewordfloat is just computer language for a real number). In any case,
Python thinks of x as an object, of type int or float. Another common type of
variable is str, i.e. a string, needed when youwant to store text. When Python
interpretsx = "This is a string", it stores the text (inbetween thequotes) in
thevariablex. Thisvariable is thenanobjectof typestr. Youmayconvertbetween
variable types if itmakes sense. If, e.g.,x is an int object,writingy = float(x)
willmakey afloatingpoint representationofx. Similarly, youmaywriteint(x)
to produce an int if x is originally of type float. Type conversionmay also occur
automatically, as shownjust below.
Names of variables should be chosen so that they are descriptive. When com-
puting amathematical quantity that has some standard symbol, e.g.Λ, this should
be reflected in thenameby letting thewordalphabepart of thenamefor the cor-
responding variable in the program. If you, e.g., have a variable for counting the
numberof sheep, thenoneappropriatenamecouldbeno_of_sheep. Suchnaming
makes itmucheasier for a human to understand thewritten code. Variable names
mayalsocontainanydigit from0to9,orunderscores,butcannotstartwithadigit.
Lettersmaybe lowerorupper case,which toPython is different. Note that certain
namesinPythonarereserved,meaningthatyoucannotusetheseasnamesforvari-
ables. Some examples are for, while, if, else, global,return and elif. If
youaccidentallyusea reservedwordasavariablenameyouget anerrormessage.
Wehaveseen that, e.g.,x = 2will assign thevalue2 to thevariablex. Buthow
dowewrite it ifwewant to increasex by4? Wemaywrite an assignment likex
= x + 4, or (givinga faster computation)x += 4. NowPython interprets this as:
takewhatever value that is inx, add4, and let the result become the newvalue of
x. Inotherwords, theold valueofx is usedon the right hand side of=, nomatter
howmessy theexpressionmightbe, and the result becomes thenewvalueofx. In
asimilarway,x -= 4 reduces thevalueofxby4,x *= 4multipliesxby4,andx
/= 4dividesxby4,updating thevalueofxaccordingly.
What if x = 2, i.e., an object of type int, andwe add 4.0, i.e., a float? Then
automatic type conversion takes place, and the newxwill have the value 6.0, i.e.,
anobjectof typefloat as seenhere,
In [1]: x = 2
In [2]: x = x + 4.0
In [3]: x
Out [3]: 6.0
NotethatPythonprogrammers,andPython(inprintouts),oftenwrite,e.g.,2.which
bydefinition is the integer2 representedasafloat.
1.5.4 IntegerDivision
Another issue that is important to be aware of is integer division. Let us look at
a small example, assumingwewant todivideonebyfour.
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