Page - 42 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 42 -
Text of the Page - 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.
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