{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Workbook for Chapter 7 of Explorations in Computing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Section 7.3: Modules and Encapsulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Set Up the Experiments"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instead of using functions from RandomLab the project in this notebook uses the module shown in Figure 7.6 of the book.\n",
"\n",
"You can use your IDE or text editor to create a file named `prng.py`, and type in the four assignment statements and two function definitions, or you can download the file from `prng.py`.\n",
"\n",
"**Important:** Save your `prng.py` file to the same folder as this notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T26. Call `dir` to verify the only names defined are the special system variables with names that start with underscores and names defined by IPython (`In`, `Out`, `exit`, `get_ipython`, and `quit`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dir()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T27. Define a variable and call `dir` again:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T28. Try listing the names in the math module:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T29. Importing math will add the name `math` to the set of names on our \"whiteboard\":"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T30. Now we should be able to get a list of names in that module:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T31. If math is a name on the whiteboard, do you suppose you can just type the name to ask Python to show you the value associated with the name? Make a prediction about what will happen if you type `math`, then test your hypothesis:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T32. The previous exercise shows that `math` is the name of a module. Recall that the `type` function tells us what type of object a name refers to:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T33. To clear the names defined in an IPython Notebook session select Restart from the Kernel menu."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T34. Use a `from` statement to import selected names from the math module:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T35. Use `dir` to get a list of names. Do you see how `sqrt` and `pi` are there, but `math` is not?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T36. Ask Python to show you the values of the imported items:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T38. Run the following code cell to import the `rand` function from your `prng` module:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from prng import rand"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Note: ** If you get an error message that says \"No module named prng\" check to make sure your `prng.py` file is in the same directory as this notebook (and that the file name is spelled correctly, with `.py` at the end of the name)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T39. Call `rand` a few times. Is it producing the sequence of values you expect?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T40. Restart the IPython Kernel again , and this time import the entire `prng` module:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T41. Print the contents of the module's namespace:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T42. Use the qualified name syntax to call `rand`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T43. Do you think the global variables are also accessible? Try asking for a value:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T44. Call `reset` to tell the module to start making values from a different sequence:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}