\section{Exercises}
\begin{footnotesize} 
\subsection*{Targeted exercises} 

\subsubsection*{Exercises for Section \ref{Getting_yes/no_answers_to_questions_posed_by_comparison_statements}} 
\begin{enumerate}\setcounter{enumi}{-1}
	\item  What do the following conditional expressions evaluate to (\lstinline{True} or \lstinline{False})?  First, write down what you think it should be before running the code, then run it to see if you are right.  If you were wrong, explain what you did wrong. Suppose the following variables are already defined:

	\lstinline{trial_number = 4}\\
	\lstinline{pH = 2.53}\\
	\lstinline{acid = 'HCl'}\\
	\lstinline{conc_stock = 1.0 # M}\\
	
	\begin{enumerate}
	\item \lstinline{pH >= 2}
	\item \lstinline{acid != 'HF'}
	\item \lstinline{trial_number > 2}
	\item \lstinline{pH > 1}
	\item \lstinline{conc_stock / 1000 < 0.001}
	\item \lstinline{acid == 'HF'}
	\item \lstinline{'H' in acid}	
    \end{enumerate}
    
\item  Write a conditional expression to evaluate the following tests.  Use the variable name given in parentheses for your answer.  Suppose the following variables are already defined:

\lstinline{pKa = 7.6}\\
\lstinline{atomic_number = 19}\\
\lstinline{pH = 4.4}\\
\lstinline{atom_symbol = 'K'}\\
\lstinline{solvent_name = 'Isopropanol'}\\
\lstinline{formula = '(CH3)2CHOH'}\\
\lstinline{solvent_polarity = 3.9}

\textbf{example}: Is the $pK_a$ (\lstinline{pKa}) less than 5.0?: \lstinline{pKa < 5.0}

\begin{enumerate}
	\item Is the atomic number equal to 11?
	\item Is the pH at least 4 but less than 6?
	\item Is the atom symbol ‘K’
	\item Is the solvent ‘Methanol’ or ‘Acetonitrile’? (note: do this with a single comparison)
	\item Is the string 'OH' in 'formula'?
\end{enumerate}

\item  For the dictionary of solvent properties created in Exercise 3 of Chapter 4, write a code that determines if an alcohol is in the dictionary by testing to see if \lstinline{'ol'} is in any of the keys.  You can do this by getting a list of the keys using \lstinline{list(<dictionaryname>.keys())} and then iterating through that list, or by using \lstinline{for k in <dictionaryname>:}


\end{enumerate}
\subsubsection*{Exercises for Section \ref{Slicing_arrays_by_values_instead_of_indices_using_comparison_expressions}} 

\begin{enumerate}\setcounter{enumi}{2}
	\item  Create a sine wave with period of 1 with an $x$-axis that goes from 0 to 20 with 100 elements.  Make a plot of this sine wave where positive values are green and negative values are blue using slicing to produce two different $y$ arrays and two corresponding $x$ arrays that you can plot separately. If you set \lstinline{mode = "markers"} when you add scatter traces, then you will not get a line at zero.

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Slicing_based_on_multiple_conditions_using_and_and_or}} 
\begin{enumerate}\setcounter{enumi}{3}
	\item  What do the following conditional expressions evaluate to (\lstinline{True} or \lstinline{False})?  First, write down what you think it should be before running the code, then run it to see if you are right.  If you were wrong, explain what you did wrong. Suppose the following variables are already defined:

	\lstinline{trial_number = 4}\\
	\lstinline{pH = 2.53}\\
	\lstinline{acid = 'HCl'}\\
	\lstinline{conc_stock = 1.0 # M}\\

    \begin{enumerate}
        \item \lstinline{pH >= 2 and pH < 7}
	\item \lstinline{acid != 'HF' and acid != 'HBr'}
	\item \lstinline{trial_number > 2 and acid != 'HCl'}
	\item \lstinline{pH > 1 and pH < 7}
	\item \lstinline{acid == 'HCl' or acid == 'HF'}
	\item \lstinline{(pH > 2 and acid == 'HF') or (pH > 3 and acid == 'HCl')}
    \end{enumerate}

    
\item  Write a conditional expression to evaluate the following tests.  Use the variable name given in parentheses for your answer.  Suppose the following variables are already defined:

	\lstinline{pKa = 7.6}\\
	\lstinline{atomic_number = 19}\\
	\lstinline{pH = 4.4}\\
	\lstinline{atom_symbol = 'K'}\\
	\lstinline{solvent_name = 'Isopropanol'}\\
	\lstinline{formula = '(CH3)2CHOH'}\\
	\lstinline{solvent_polarity = 3.9}\\

    \begin{enumerate}
        \item Is the solvent anything other than `toluene' or `\textit{n}-hexane' and is the polarity less than 5?
	\item Is the pH greater than 4 and the $pK_a$ greater than 4?
	\item Does the formula contain a methyl group and an alcohol group?
	\item Is the atom potassium and the pH less than 2?
    \end{enumerate}

\end{enumerate}

\subsubsection*{Exercises for Section \ref{Visualizing_fit_results_using_codechembook.quickPlots.plotFit}} 
\begin{enumerate}\setcounter{enumi}{5}
	\item  Modify the final code from Chapter 5 to use the \lstinline{plotFit()} function from \lstinline{codechembook} and confirm that it works. 

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Learning_to_use_libraries_by_reading_documentation}} 
\begin{enumerate}\setcounter{enumi}{6}
	\item  Read the documentation for the \lstinline{plotFit()} function and make plots for the code from the end of the chapter but with the following changes:
    \begin{enumerate}
        \item Confidence intervals of  95\%.
	\item Hide the components of the fit.
	\item Show the residual but scale it to the same scale as the main plot.
    \end{enumerate}


\item  Sometimes you may want to have a string representation of a whole array.  Read the documentation for \lstinline{numpy.array2string()} and use it to create a string that represents the sin wave from Exercise 3 rounded to the nearest tenth.  Make sure that the output does not contain any newline (\lstinline{\n}) characters using keywords for the function.


\end{enumerate}
\subsubsection*{Exercises for Section \ref{Combining_lmfit_models}} 
\begin{enumerate}\setcounter{enumi}{8}
	\item  There are times where the behavior of a system is best described by the sum of the equations for two lines.  For instance you could have a reaction mechanism with distinct rate-determining steps, where one dominates at early times and another takes over later. You might also encounter this in thermal expansion of multi-phase materials.  Though the model needed to describe such behavior is simple, it would be somewhat challenging to use it in a fit within Excel and extracted parameter estimates and their uncertainties.  Write the code to produce a lmfit model that is made up of two linear models. 

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Accessing_the_number_of_times_a_loop_has_iterated_using_enumerate}} 
\begin{enumerate}\setcounter{enumi}{9}
	\item  If you had two lists  \lstinline{[1, 2, 3]} and \lstinline{[4, 5, 6, 7]} use \lstinline{enumerate()} to add the first list to the first three elements of the second list.

	\item  For the list \lstinline{[5, 1, 9, -3]} construct a new list using a nested \lstinline{for} loop. This new list must hold only the unique 6 pairwise products.

	
\end{enumerate}
\subsection*{Comprehensive exercises} 
\begin{enumerate}\setcounter{enumi}{11}
	\item  It is a good idea to check how robust the fitting results are—that is, how different can your initial guesses be, and still get the same final fit values. The fit for the Gaussians used in the final code of this chapter has three kinds of initial guesses: those for the position of the Gaussian, the intensity of the Gaussian, and the width of the Gaussian. For each of these parameter groups:
    \begin{itemize}
        \item Change the initial guesses in a way that you still get the same final fit (within the errors of the parameters).
	\item Change the initial guesses in a way that you do not get the same final fit (within the errors of the parameters). 
	\item Comment on how robust  this fitting seems to you.
    \end{itemize}

    
\item  Using the final code for the chapter, modify this so that there is no upper bound on the Gaussian width parameter. When you run this version, do you get a different result? If so, discuss why you might, or might not use this upper bound, and if you could justify using it.

\item  You think that there may be a small 4$^{th}$ Gaussian contribution in the data that was fit in the final code of this chapter, and it looks like it would be around 500 nm. Change the code to allow for fitting of this 4$^{th}$ peak.  Discuss the results of this new fit with respect to the one that was presented at the end of the chapter. 

\item  Modify the code we produced at the end of this chapter to add annotations that label the peaks of the actual signal (instead of the Gaussian components) with the best fitting peak centers. The annotation should include an arrow that points to this exact position in the data, which can be accomplished directly in the \lstinline{add_annotation()} method of the Plotly figure.

\item  It is technically not a good idea to fit bands to Gaussian functions when your $x$-axis is in wavelength. You should really be working in an energy unit, since the effects that give rise to peak shapes depend on energy rather than wavelength, and intensities in the two are related by a Jacobian transformation (see Exercise 22 of Chapter 2 for details). Rewrite the code at the end of this chapter to first convert the $x$-axis to electron volts (eV), then perform the fit in those units. Note: you will need to convert the guesses as well to get a good start. How do the results compare between these two  approaches?

\item  In Chapter 3, we plotted many of the same spectra that we fit here.  Generate a script that automatically fits each of these spectra and produces a single Plotly plot for each spectrum that was fit, each in its own subplot.  Do not include the residuals. You may find that this code eventually 'breaks.'  This is likely be because the guesses for parameters are no longer good places to start, for later titration points. 

\end{enumerate}
\end{footnotesize} 
