56
2 A Few More Steps
One more handy function from numpy deserves mention. If you have an array 15
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 also numpy allows the seed to be set. For example, setting the seed to 10
(as above), could be done by
np.random.seed(10)
The fact that a module by the name random is found both in the standard
Python library random and in numpy, calls for some alertness. With proper import
statements (discussed in Sect. 1.4.1), however, there should be no problem.
For more details about the numpy functions for pseudo-random numbers, check
out the documentation (https://docs.scipy.org/doc/).
2.5 Exercises
Exercise 2.1: Interactive Computing of Volume
Redo the task in Exercise 1.2 by using Python interactively. Compare with what you
got previously from the written program.
Exercise 2.2: Interactive Computing of Circumference and Area
Redo the task in Exercise 1.3 by using Python interactively. Compare with what you
got previously from the written program.
Exercise 2.3: Update Variable at Command Prompt
Invoke Python interactively and perform the following steps.
1. Initialize a variable x to 2.
2. Add 3 to x. Print out the result.
3. Print out the result of x + 1*2 and (x+1)*2. (Observe how parentheses make a
difference).
4. What object type does x refer to?
Exercise 2.4: Multiple Statements on One Line
a) The output produced by the following two lines has been removed. Can you tell,
from just reading the input, what the output was in each case?
15 Instead of an array, we can also use a list, see Sect. 5.1.
2 A Few More Steps
One more handy function from numpy deserves mention. If you have an array 15
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 also numpy allows the seed to be set. For example, setting the seed to 10
(as above), could be done by
np.random.seed(10)
The fact that a module by the name random is found both in the standard
Python library random and in numpy, calls for some alertness. With proper import
statements (discussed in Sect. 1.4.1), however, there should be no problem.
For more details about the numpy functions for pseudo-random numbers, check
out the documentation (https://docs.scipy.org/doc/).
2.5 Exercises
Exercise 2.1: Interactive Computing of Volume
Redo the task in Exercise 1.2 by using Python interactively. Compare with what you
got previously from the written program.
Exercise 2.2: Interactive Computing of Circumference and Area
Redo the task in Exercise 1.3 by using Python interactively. Compare with what you
got previously from the written program.
Exercise 2.3: Update Variable at Command Prompt
Invoke Python interactively and perform the following steps.
1. Initialize a variable x to 2.
2. Add 3 to x. Print out the result.
3. Print out the result of x + 1*2 and (x+1)*2. (Observe how parentheses make a
difference).
4. What object type does x refer to?
Exercise 2.4: Multiple Statements on One Line
a) The output produced by the following two lines has been removed. Can you tell,
from just reading the input, what the output was in each case?
15 Instead of an array, we can also use a list, see Sect. 5.1.
