16.0: Chapter 1
- Page ID
- 117496
\( \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{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)1.1 Background
- b. Each image in the animation represents a different type of computer program.
- a. This type of analysis requires basic knowledge of programming.
- c. Like mobile apps, wi-fi speakers run small programs that connect to servers.
- b. This book assumes readers understand the basics of algebra, but nothing too difficult.
- c. Python's syntax is more concise than Java's.
- a. Python does not require a semicolon at the end of each line. Braces are also not required.
1.2 Input/output
- c. The hello world program is just one line of code.
- c. The
end=""option removes the newline character after hello, producingHello world!on the same line. - c. The default space character that separates multiple items has been replaced with the dash character.
- c. The user input is stored in memory and can be referenced using
today_is. - b. The
print()function outputs"You entered:"followed by a space character, followed by the input data,Sophia, which is referenced by the variable name. - a. The input is stored exactly as the user enters the input. So, the retrieved value is displayed exactly as entered by the user.
1.3 Variables
- c. This would produce the output string,
"The city where you live is Chicago". - b. These two lines of code would produce the output string,
"Total = 6". - c. The single
=character is the assignment operator. - a. An assignment statement has the variable name on the left side and the value on the right side of the assignment statement.
- a.
medianis a descriptive name for a variable that stores amedianvalue. - b. A variable name can contain, but not start with, a digit.
- c. The name clearly indicates the value being stored is a zip code.
- a. Snake case separates each word with an underscore and uses only lowercase letters.
1.4 String basics
- c. A string that is enclosed by matching double quotes is a valid string. Note that
'7 days'is also a string. - c. The string,
fred78@gmail.com, is enclosed in matching double quotes, and is assigned to the variableemail. A string can contain characters such as "@" or "." in addition to letters and numbers. - c. The string has matching double quotes. Since a single quote is a part of this string, double quotes must be used.
- c. Note that the enclosing single quotes (') are not part of the string but the double quotes (") are.
- c. The
len()function returns the number of characters in the string. 6 characters exist in"Hi Ali"which are "H", "i", " ", "A", "l", and "i". - b. The length of an empty string equals 0.
- b. The
number_of_digitsvariable's value is2, and as such, the output is"Number 12 has 2 digits.". - c. The
1and0are enclosed in quotes indicating valid strings, and the + is not, indicating a concatenation operation. Thus, the string"10"is produced. Note that concatenation operator does not include any default separator characters like the space character. - c.
"A"and"wake"are valid strings, and the + is not enclosed in quotes, indicating a concatenation operation. Thus the string"Awake"is produced. Note that the concatenation operator does not include any default separator characters like the space character. - c. Note that space characters must be explicitly included when using the concatenation operator.
- c. A concatenation of the two strings is assigned to the variable
holiday. Note that"one-sided"would assign the same string toholiday.
1.5 Number basics
- c.
x = 1is an integer, and when printing the type of an integer variable, the output is<class 'int'>. - b.
y = 2.0is floating-point number. - a. Any value defined within quotation marks is a string value regardless of the content.
- c. Line 2 of the code evaluates as follows:
c = x - z = 7 - 2 = 5. Line 3 increments variablecby 1, making the total equal to 6. - b. The output of
3.5-1.5equals2.0, which is a floating-point number. - c. The result of the division operation is a floating-point number that isn't rounded down.
7/2 = 3.5. - c. The output of the
20/2equals10.0because the division operation results in a float. - c. The output of an arithmetic operator on any floating-point operand results in a floating-point value.
2 * 1.5 = 3.0. - a. 3 raised to the power 2 is 9, times 4 is 36, plus 1 is 37.
- b. Although addition has lower precedence than exponentiation, the parentheses cause
1 + 3to be evaluated first. - a. In Python, exponentiation, or raising to a power, has higher precedence than making a value negative. Python's
-16result can be confusing because, in math,"-4 squared"is16. - c. The statement uses the assignment operator (
=), the negative operator (-), and the exponentiation operator (**).
1.6 Error messages
- b. The filename can be found after the last slash (/). Other names, like Desktop, refer to folders.
- b. Line 2 is mentioned immediately after the filename.
- b. The type of error is always the first word on the last line of the error message.
- a. The middle three lines are indented. In Python, lines may not begin with spaces or tabs unexpectedly.
- c. The quote marks are missing at the end of the input prompt.
- b. The name for
"input"is misspelled.
1.7 Comments
- b. Comments help explain the purpose of code so that other programmers understand what the code intends.
- a. The hash character indicates the beginning of a comment.
- b. Having one space after the # is the community standard.
- c. This comment explains the intent of the code. If the code had a mistake, the programmer reading the comment might notice the mistake more easily.
- a. A blank line separates the input from the output. Then again, inserting a blank line in a three-line program is generally unnecessary.
- c. The # symbol "comments out" the line without removing the line permanently.
- a. The docstring is primarily written for others who will use the program.
- c. Docstrings must be a valid string, not a multi-line comment. Docstrings are stored in the program's memory, but comments are ignored.
- b. The amount of information in this docstring is just right. In most cases, the docstring should not be longer than the code.
1.8 Why Python?
- a. The language was named after Monty Python. Just for fun, the official documentation makes subtle references to Monty Python comedy sketches.
- b. Python's first release was in 1991, after being developed in the late 1980's.
- c. Making Python open source early on allowed for many programmers to provide feedback.
- b. For 20 years, Java and C traded places as the top language until Python took over in 2021!
- a. The line for Python crosses the line for JavaScript in the middle of 2018.
- b. TIOBE started tracking language popularity in 2001, several years after web search engines became popular.


