Modules
A module is just a file containing code that can be understood by Python- Any script we write is a module
- Other modules come with the Python distribution
- And others are third party (see, for example, PyPi)
- Bundled with every Python distribution
- Very comprehensive and high quality
- Keep the core language small
- Makes it clean and consistent
- And put most functionality in the standard library
The import
Statement
The Python keyword import
is used to access code in modulesLet's look at some examples, using the
math
module from the standard libraryImporting the Module
One way to load themath
module is as follows>>> import math
>>> math.e # A float (Euler's number e)
2.7182818284590451
>>> math.exp(10) # exp() is a function (the exponential function)
22026.465794806718
>>> math.pi # A float
3.1415926535897931
>>> math.sqrt(100) # Another function
10.0
>>> math.exp(math.log(10))
10.000000000000002
>>> math.cos(math.pi)
-1.0
- To access 'pi' we type 'math.pi'
- To access 'sqrt()' we type 'math.sqrt()', etc.
To access attribute, type 'moduleName.attributeName'
- E.g., math.pi
- Very useful
- You can also try 'help(math)'
Importing Attributes Directly
If I start with>>> import math
pi
I need to type math.pi
An alternative is to import
pi
directly>>> from math import pi
>>> pi
3.1415926535897931
>>> from math import pi, e
>>> pi
3.1415926535897931
>>> e
2.7182818284590451
In fact we can directly import all the attributes in
math
as follows>>> from math import *
>>> pi
3.1415926535897931
>>> sqrt(4)
2.0
- Suddenly there are lots of variables defined, and it's hard to keep track
Other Modules
Let's look at some more modules in the standard libraryThe Random Module
Examples>>> import random
>>> random.uniform(0, 1) # Uniform r.v. on (0,1)
0.91175197121395068
>>> random.uniform(0, 1) # Another one, independent of first
0.86542825268640777
>>> [random.uniform(0, 1) for i in range(3)]
[0.83426715541997887, 0.067833169185644748, 0.22589302179038462]
>>> random.normalvariate(0, 1) # Standard normal
-1.0375932163018793
>>> X = ['a', 'b', 'c', 'd']
>>> random.choice(X)
'b'
>>> random.choice(X)
'b'
>>> random.choice(X)
'c'
>>> X
['a', 'b', 'c', 'd']
>>> random.shuffle(X)
>>> X
['a', 'd', 'b', 'c']
Others
The os module is for interacting with operating system>>> import os
>>> os.getcwd() # Returns the current working directory
'/home/john/sync_dir/teaching/kyoto_08'
>>> os.listdir('.') # List files in current directory
['index.txt',
...
Problems
Write the following as programs- Using IDLE?
- Simulate a draw from Y = max{|X_1|,...,|X_10|}, where X_i ~ N(0, 1)
- And print out the result
- Hint: Use list comprehensions
- Simulate a draw from Y ~ Bin(n, 0.5) using random.choice()
- num of successes in n indep trials with success prob 0.5
- E.g., num of heads in n flips of fair coin
- The value of n is read in from the user at run time
Solutions
Solution to Problem 1import random
N = [random.normalvariate(0, 1) for i in range(10)]
M = [abs(x) for x in N]
Y = max(M)
print Y
import random
n = int(raw_input("What is the value of n? "))
C = 0, 1
M = [random.choice(C) for i in range(n)]
Y = sum(M)
print Y
import random
n = int(raw_input("What is the value of n? "))
C = 'heads', 'tails'
M = [random.choice(C) for i in range(n)]
Y = M.count('heads')
print Y
0 comments:
Post a Comment