Page - 56 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 56 -
Text of the Page - 56 -
56 2 AFewMoreSteps
One more handyfunction fromnumpydeservesmention. If you have an array15
with numbers, you can shuffle those numbers in a randomized way with the
shuffle function,
In [1]: import numpy as np
In [2]: a = np.array([1, 2, 3, 4])
In [3]: np.random.shuffle(a)
In [4]: a
Out[4]: array([1, 3, 4, 2])
Note that alsonumpy allows the seed to be set. For example, setting the seed to 10
(asabove),couldbedoneby
np.random.seed(10)
The fact that a module by the name random is found both in the standard
Python libraryrandomand innumpy, calls for some alertness. With proper import
statements (discussed in Sect.1.4.1),however, there shouldbenoproblem.
For more details about thenumpy functions for pseudo-randomnumbers, check
out the documentation(https://docs.scipy.org/doc/).
2.5 Exercises
Exercise2.1: InteractiveComputingofVolume
Redothe task inExercise1.2byusingPythoninteractively.Comparewithwhatyou
gotpreviously fromthewrittenprogram.
Exercise2.2: InteractiveComputingofCircumferenceand Area
Redothe task inExercise1.3byusingPythoninteractively.Comparewithwhatyou
gotpreviously fromthewrittenprogram.
Exercise2.3:UpdateVariableatCommandPrompt
InvokePython interactivelyandperformthe followingsteps.
1. Initializeavariablex to2.
2. Add3 tox. Printout the result.
3. Printout the resultofx + 1*2and(x+1)*2. (Observehowparenthesesmakea
difference).
4. Whatobject typedoesx refer to?
Exercise2.4:Multiple StatementsonOneLine
a) Theoutputproducedby the followingtwo lineshasbeenremoved.Canyoutell,
fromjust reading the input,what theoutputwas in eachcase?
15 Instead of an array, we can also use a list, see Sect.5.1.
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