16.6: Chapter 7
- Page ID
- 117502
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
7.1 Module basics
- b. The code has ten
defstatements, each of which defines a function. - a. Many modules consist of function definitions only. The functions are intended to be called in other modules.
- c.
area.cube(5)is6 * 5**2, which is6 * 25, which is150. - a. When importing a module from another file, the suffix .py is removed.
- b. The circle function must be called using the area variable that refers to the module.
- b. Adding the function to the area module makes the function easier to reuse in other programs.
7.2 Importing names
- a. This statement imports the sqrt function defined in the math module.
- b. A variable is defined for each function imported: sin, cos, and tan.
- b. A name error occurs when a variable name is not found, which might be due to a missing import statement.
- b. This statement replaces the previously imported circle function, even though
circle()andarea.circle()have different parameters. - a. Assigning the area variable replaces the previously imported module with the integer
51. The variable can no longer be used to call functions in the area module. - b. The most recent definition of
hello()requires one argument, so this line would result in an error.
7.3 Top-level code
- b. Whenever this module is imported, Python will output
"Defining sphere function"as a side effect. - a. Even though nothing is printed, calling the sphere function ten million times causes a delay in the main program.
- a. All code is at the top level, and no functions are defined. Importing this module would run all code as a side effect.
- c. The variable
__name__is the module's name, unless the module was run as the main program. In that case,__name__is"__main__". - a. Since test.py was imported, the value of
__name__is"test". - c. This line is found in many Python modules to check if the module was run as the main program.
7.4 The help function
- b. The digits have 0 red and 0 green, so the color must be a shade of blue. Navy is a darker version of standard blue,
#0000FF. - c. FF in hexadecimal is 255, the maximum amount of red, green, or blue in a color.
- a. The red, green, and blue values for orange are higher than the red, green, and blue values for green.
- b. The hexadecimal for YELLOW is
#FFFF00, meaning 255 red + 255 green + 0 blue. - c. Both the
darkenfunction and theBLUEvariable must be accessed via thecolorsvariable. - a. Function names that begin with an underscore are intended to be used only within the current module.
- b. Only docstrings are included in the documentation. All comments are ignored by the help function.
- a. The functions are sorted alphabetically by name to make looking up a function easier.
- a. Functions that begin with an underscore are intended to be used only within a module, not in other modules.
7.5 Finding modules
- a. This reference describes the standard library, including built-in functions, types, and modules.
- b. As explained in the documentation of the
calendarmodule,MONDAYis0andSUNDAYis6. - a. The
tkintermodule stands for Tk interface. Tk is a standard toolkit for creating windows, text boxes, buttons, and other graphical widgets. - c.
MoviePyis a module for video editing, andPillowis a module for image processing. - a. According to the "Features" section of the documentation,
Arrow's is a "fully-implemented, drop-in replacement fordatetime." - a. This function converts a color code to a color name. Ex:
hex_to_name("#daa520")returns"goldenrod".


