{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 3 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Section 3.4: List Indexes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T27. Make a small list of strings to use for testing index expressions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T28. Use an index expression to get the first item in the list:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T29. Because there are seven strings in this list we can find the last one at location 6:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T30. Type another expression to access the last item:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T31. Try asking Python for values at other locations, using any index between 0 and 6. Is the result what you expected?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T32. Ask for an index that is past the end of the list:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T33. Next try some expressions using `in`, which should evaluate to `True` or `False`, depending on whether the list contains the specified item:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T34. Use Python's `index` method to find out where the items are located:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T35. Enter a `for` loop in the code cell below. The loop should use `range` to iterate over the `notes` list and print something about each note:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T36. Enter the `for` loop that prints the square roots of numbers from 1 to 10 (don't forget to import the `sqrt` function if you haven't done it already):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T37. Repeat the previous exercise, but this time use `range(1,11,2)`. Did Python execute the body of the loop only for odd values of `i`? Do you see why?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `partial_total` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T38. Enter the definition of the `partial_total` function from Figure 3.6 in the code cell below. You can either type the definitions shown in the figure, or copy and paste the code from `partialtotal.py`.\n", "\n", "After the definition is complete type ⌃⏎ to execute the cell to make the function available in this notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a list named `a` containing the numbers 3, 5, 2, 16, and 7:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How would you call `partial_total` to get the sum of the first three numbers? Type the expression in the code cell below to verify your answer. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you're not sure how `partial_total` works add this print statement to the `for` loop, right after the statement that adds `a[i]` to `sum`. Type ⌃⏎ to update the function definition, then call the function with different values of `n`.\n", "\n", "
\n",
" print('i =', i, 'sum =', sum)\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T39. What happens if you pass a number to `partial_total` that is larger than the number of items in the list? For example, what does Python do if `a` has five items and you call `partial_total(7,a)`?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"♦ Add statements to your definition of `partial_total` so the function returns a correct value even if `n` is too large. If `n` is greater than the number of items in the list, simply return the sum of all the items."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"♦ Repeat the call to `partial_total(7,a)`. Does your function return the correct result now? "
]
}
],
"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
}