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

\subsubsection*{Exercises for Section \ref{Prompting_the_user_for_information_using_input}} 
\begin{enumerate}\setcounter{enumi}{-1}
	\item  Using the dictionary of solvent properties that you made in Exercise 3 from Chapter 4, write a code that asks the user for a solvent, then asks the user for a property, and then prints a sentence that tells the user the value of that property for the solvent they chose.

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Turning_numeral_characters_into_numbers}} 
\begin{enumerate}\setcounter{enumi}{0}
	\item  Modify the script from the end of  Chapter 0 so that it asks the user for the concentration of HCl, ionic strength, and concentration of the catalyst, then performs the calculation and outputs results in the same way as before.

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Inserting_calculation_results_into_strings_using_f-strings}} 
\begin{enumerate}\setcounter{enumi}{1}
	\item  Using f-strings and the \lstinline{print()} function, output a grammatically correct sentence that expresses the following (perform the math, if necessary, and write out the result to the user in plain English including units). For each item, you can use a single line of code to accomplish this: 

	\begin{enumerate}
	\item The number of moles of water in 14.7 g.
	\item The density of toluene in g/mL.
	\item The number of mL in one gallon.
	\item 72.5 degrees Fahrenheit in Celsius.
	
	\end{enumerate}
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Formatting_the_representation_of_numbers_in_strings}} 
\begin{enumerate}\setcounter{enumi}{2}
	\item   In two lines of code, define the variable specified and print the following statement using f-strings to display the value of the variable, with units. Remember that Python uses banker's rounding: 

	\begin{enumerate}
	\item Given \lstinline{normal_temp = 293.15}, print 'Normal temperature is 293.1 K.'
	\item Given \lstinline{vol = 1.672834e-3} , print 'The volume is 1.673 mL.'
	\item Given \lstinline{conc_NaCl = 0.0043782}, print '[NaCl] = 4.38 mM.'
	\item Given \lstinline{ethane_CC_bond_length =  153.5}, print 'Ethane’s C-C bond is 0.153 nm.' 
	
	\end{enumerate}
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Generating_symbols_that_aren't_on_your_keyboard_using_codechembook.symbols}} 
\begin{enumerate}\setcounter{enumi}{3}
	\item  Create the f-strings that produce the following text, using the \lstinline{codechembook.symbols} module to generate special characters:

        \begin{enumerate}
            \item $\lambda_{\textrm{max}}$  
	\item 3.27 $\mu$L.
	\item \ce{[CH3CH2COO-]} = 5.14 mM.
	\item N$_2$ + 3H$_2$ → 2NH$_3$.
	\item The C=C bond length of ethylene is 1.33 \AA.
	\item For $p$ orbitals, $\ell = 1$.
    \end{enumerate}
\end{enumerate}


\subsubsection*{Exercises for Section \ref{Producing_formatted_chemical_formulas_using_texttt_codechembook.quickHTMLFormula()}}
\begin{enumerate}\setcounter{enumi}{4}

    \item Repeat Exercise 13 from Chapter 4, using  \lstinline{codechembook.quickTools.quickHTMLFormula()}.
\end{enumerate}

\subsubsection*{Exercises for Section \ref{Finding_paths_to_files_in_folders_using_glob}} 
\begin{enumerate}\setcounter{enumi}{5}
	\item  Using the \lstinline{.glob()} method of pathlib Path objects:

	\begin{enumerate}
	\item Get a list of all the files in your Downloads folder.
	\item Sort the list of files you just made.
	\item Using \lstinline{len()}, print the number of .pdf files you have in your Downloads folder.
	\end{enumerate}
\end{enumerate}

\subsubsection*{Exercises for Section \ref{Dividing_a_string_into_multiple_substrings_with_split}} 
\begin{enumerate}\setcounter{enumi}{6}
	\item  You have an irritating instrument that outputs data in text files interspersed in sentences. For instance, it will produce a string that goes like this:
   
   ``at 1.58 seconds, conc = 40.2 mM;at 3.24 seconds, conc = 78.2 mM;at 4.98 seconds, conc = 119.4 mM;at 6.02 seconds, conc = 154.7 mM'' 
   
   You want to plot concentration versus time, but you first need to process this string. Using the \lstinline{.split()} method for strings, write a script that:
   \begin{itemize}
       \item Breaks apart the above string to extract the times and concentrations
	\item Processes these times and concentrations them into Numpy arrays.
	\item Plots these arrays with proper axis labels. 
	\item Makes no assumptions about how many data points there are.
     \end{itemize}
\end{enumerate}

\subsubsection*{Exercises for Section \ref{Finding_the_element_closest_to_a_given_number_in_a_Numpy_array}} 
\begin{enumerate}\setcounter{enumi}{7}
	\item  If you used the approach we demonstrated in this chapter to find a number closest to a target number, what happens if there are two numbers that are equally close to the target number?

	\item  Write code that will take a Numpy array and find a number in it, closest to some target number. Write this code such that, if there are two (or more) numbers that are equally close to the target number, you identify the smallest number. 

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Fitting_models_to_data_with_lmfit}} 
\begin{enumerate}\setcounter{enumi}{9}
	\item  You have measured the equilibrium constant for a chemical reaction at multiple temperatures. You wish to use a Van't Hoff plot to determine $\Delta H$ and $\Delta S$ for the reaction. The equation for this is $\ln(K_{eq}) = -\frac{\Delta H}{RT} + \frac{\Delta S}{R}$. Thinking about this proceedure, you realize that this form of the equation was historically used because it produces a linear relationship between $\ln(K_{eq})$ and $\frac{1}{T}$ and this was simpler to fit in the time before computers. However, both of these transformations can significantly change the values of the parameters that are estimated by the fit. So, what you should actually do is plot $K_{eq}$ versus  $T$ and fit that plot.  Write the equation you would use as the model for this fitting and identify the independent variable, the dependent variable, the parameters you would adjust in a fit, and any constants that would not change in the fit. 

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Analyzing_fit_results_in_lmfit}} 
\begin{enumerate}\setcounter{enumi}{10}
	\item  Use the Spyder IDE's variable explorer or \lstinline{print(vars(result))} to inspect the \lstinline{fitResult} object created by the final code in this chapter.  You will find that it is presented as if it had some dictionary structure.  The 'keys' are the attributes of the result object and are used to access the associated 'values.' For instance, if you wanted to find the R$^2$ value of the fit, you could use \lstinline{result.rsquared}, which should return something like \lstinline{0.9994452445068337}. Find the attributes for the following elements:
    \begin{enumerate}
        \item The best-fitting values
	\item Their uncertainties
	\item Whether the fit was aborted
	\item The model used
	\item $\chi^2$
    \end{enumerate}

	
\end{enumerate}
\subsubsection*{Exercises for Section \ref{Building_figures_with_multiple_subplots_in_Plotly}} 
\begin{enumerate}\setcounter{enumi}{11}
	\item  Make and display a Plotly figure with two subplots, one on top and one on bottom, in which the top plot is the first spectrum from our titration series in Chapter 3 and the bottom plot is the last one.

	\item  Make and display a Plotly figure with four subplots, in two rows and two columns, in which the top left plot is the first spectrum from our titration series in Chapter 3, the bottom right is the last spectrum, the top right is the first spectrum minus the last spectrum, and the bottom left is the last spectrum minus the first spectrum.

	
	
\end{enumerate}
\subsection*{Comprehensive exercises} 
\begin{enumerate}\setcounter{enumi}{13}

    \item In this chapter, we introduced a function that makes it easy to identify multiple files on your computer: \lstinline{quickSelectFolder()}. This will be nice to use when you don't know where the file is located, or when you are choosing different files each time you run your script.  However, there will be times when you simply want to use the same file over and over---for instance when you are developing and testing a code. In these cases, it can be much faster to provide the path to the file in the script. Modify the final code in this chapter, replacing the \lstinline{quickSelectFolder()} function with a line that creates a Path object with the absolute path to the folder and on which you use  the \lstinline{glob()} method. Verify this works, and describe when you might prefer this approach over the use of \lstinline{quickSelectFolder()}.

\item  You remember from analytical chemistry that there is a `linear regime' for absorbance values. You wonder about the behavior in transmittance. Convert the values used in the final code of this chapter to transmittance, and fit that to a line, displaying the data, fit, and residual of the fit. If you use R$^2$ of 0.99 as a threshold for `good' linearity, then how is this fit?

    \item  It is common to fit models that depend exponentially on some value, like kinetics, by taking the natural log of the dependent variable and performing a linear fit instead of fitting the raw data to an exponential function:
   
	   \hspace{5em} $[A](t) = [A]_0\exp{-kt} \rightarrow  \ln{\frac{[A](t)}{[A]_0}} = -kt$
   
	In the absence of noise, these two approaches to fitting are equivalent.  However, if your data is somewhat noisy, you can get different results from the two different approaches. 
    \begin{itemize}
        \item Download the file ‘expfit.csv’ and save it to your class folder.  This file has three columns: time (sec), exact concentration (M), and noisy concentration (M).  Load the data and create new arrays containing the natural log of the concentrations.  Then, fit both forms of the raw data to exponentials, and both forms of the natural log data to lines.  Compare the rates that you get from the linear model to the exponential model for the exact and noisy cases.  What do you notice?  Make a table here of your results and include your code when you turn in the assignment.
        \item The above problem can be mostly remedied.  Since LMFIT is basically trying to minimize $\chi^2$, which depends on the uncertainty of each point.  In the data you’ve been given, the uncertainty of each point is the same ($\sigma$ = 0.0001).  When you take the natural log of the $y$-values, you also have to compute new uncertainties in the logarithmic space, which are not all the same!  Thus, not all points have equal weight when calculating $\chi^2$.  LMFIT allows you to give each point a different weight using the weight keyword in \lstinline{Model.fit()} (check it out in the documentation).  The weights should be $\frac{1}{\sigma_i^2}$ for each point.  When you take the log, the new uncertainties $\sigma{'}_i^2$ in the logarithmic y axis can be computed by $\sigma{'}_i = \frac{\sigma_i}{y_i}$.  Using this relationship, revise your code to do the linear fit with appropriate weights, and confirm that it makes the results more consistent.
    \end{itemize}

    
\item   Using the solvent dictionary from Exercise 3 in Chapter 4, make a Plotly figure that has a grid of subplots equal to or greater than the number of solvents in that dictionary.  Using RDKit, add a drawing of one solvent molecule to each subplot. It okay if you end up with blank subplots. In order to specify which subplot you are adding a figure to, you can use the \lstinline|xref=f"x{i}"| and \lstinline|yref=f"y{i}"| within the \lstinline{add_layout_image()} method of the Plotly figure object. Here, \lstinline{i} indicates the subplot number. Plotly counts subplots left to right, top to bottom (like you read English), and starts at the number 1. 

\end{enumerate}
\end{footnotesize} 
