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

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

Bild der Seite - 42 -

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

Text der Seite - 42 -

42 2 AFewMoreSteps 2.2.3 Assignment We have learned previously that, for example,x = 2 is an assignment statement. Also,whendiscussingball.py inSect.1.2,we learnedthatwriting,e.g.,x = x + 4 causes the value ofx to be increased by 4. Alternatively, this update could have been achieved (slightly faster) withx += 4. In a similar way,x -= 4 reduces the valueofxby4,x *= 4multipliesxby4,andx /= 4dividesxby4,updating the valueofxaccordingly. The followingalso worksasexpected(but there isonepoint tomake): In [1]: x = 2 In [2]: y = x # y gets the value 2 In [3]: y = y + 1 # y gets the value 3 In [4]: x # x value not changed Out[4]: 2 Observe that, after theassignmenty = x, a changeinydidnotchangethevalueof x (also, if ratherxhad been changed,ywould have stayed unchanged).We would observe the same hadxbeen the name of afloator astring, as you will realize if you try this yourself.3 This probably seems obvious, but it is not the case for all kindsofobjects.4 2.2.4 ObjectTypeandTypeConversion The Type of an Object By now, we know that an assignment likex = 2 triggers the creation of an object by the namex. That object will be of typeint and have the value 2. Similarly, the assignment y = 2.0will generate an object named y, with value2.0and typefloat, since real numbers like 2.0are calledfloatingpoint numbers in computer language (by the way, note that floats in Python are often written with just a trailing “dot”, e.g., 2. in stead of 2.0). We have also learned thatwhenPython interprets, e.g.,s = ‘This is a string’, it stores the text (in betweenthequotes) inanobjectof typestrnameds.Theseobject types, i.e.,int, floatandstr, are still just a fewof themanybuilt-inobject types in Python.5 The Type Function There is a useful built-in function type that can be used to check the typeofanobject: 3 To test the string, you may try (in the order given):x = ‘yes’; y = x; y = y + ‘no’, and then give the commandsyandx to confirm thatyhas becomeyesnoandx is stillyes. 4 In Python, there is an important distinction between mutable and immutable objects. Mutable objects can be changed after they have been created, whereas immutable objects can not. Here, the integer referred to byx is an immutable object, which is why the change inydoes not change x. Among immutable objects we find integers, floats, strings and more, whereas arrays and lists (Sect.5.1) are examples of mutable objects. 5 https://docs.python.org/3/library/stdtypes.html.
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