|
| 1 | +\documentclass[12pt]{article} |
| 2 | + |
| 3 | +\setlength{\topmargin}{-.75in} \addtolength{\textheight}{2.00in} |
| 4 | +\setlength{\oddsidemargin}{.00in} \addtolength{\textwidth}{.75in} |
| 5 | + |
| 6 | +\usepackage{amsmath,color,graphicx} |
| 7 | +\usepackage{minted} |
| 8 | + |
| 9 | +\nofiles |
| 10 | + |
| 11 | +\pagestyle{empty} |
| 12 | + |
| 13 | +\setlength{\parindent}{0in} |
| 14 | + |
| 15 | + |
| 16 | +\begin{document} |
| 17 | + |
| 18 | +\noindent {\sc {\bf {\Large Final review}} |
| 19 | + \hfill EML4930/6934, Python Programming, Fall 2017} |
| 20 | +\bigskip |
| 21 | + |
| 22 | +\noindent {\sc {} |
| 23 | + \hfill {\large Name:} |
| 24 | + \hfill} |
| 25 | +\bigskip |
| 26 | +\bigskip |
| 27 | + |
| 28 | +\textbf{Instructions:} Please answer the questions below. You are not allowed to use any notes or calculator on this Exam. You are not allowed to work with your neighbor. |
| 29 | +\bigskip |
| 30 | + |
| 31 | + |
| 32 | +\begin{enumerate} |
| 33 | +\item Name one difference between Python 2 and Python 3. |
| 34 | +\item Show a properly syntax single line comment in Python. Show a bulk/multi-line comment in Python. |
| 35 | +\item Consider floor division in Python 3. What will 3//2 return? |
| 36 | +\item Consider a list named x. What is the easiest way to call the last item in list x? |
| 37 | +\item How should you properly denote a code block in Python? |
| 38 | +\item Consider the following code: |
| 39 | +\begin{minted} |
| 40 | +{python} |
| 41 | +for i in range(100): |
| 42 | + print(i) |
| 43 | +\end{minted} |
| 44 | +What will be the first value of i? What will be the last value of i? |
| 45 | +\item Consider the following code: |
| 46 | +\begin{minted} |
| 47 | +{python} |
| 48 | +x = [1, 2, 3, 4, 5] |
| 49 | +y = [i**2 for i in x] |
| 50 | +\end{minted} |
| 51 | +List the values in y. |
| 52 | +\item Define a function solve the quadratic equation |
| 53 | +\begin{equation} |
| 54 | +ax^2 + bx + c = 0 |
| 55 | +\end{equation} where $a,b,c$ are known numbers that are the input to your function. The solution is obtained as |
| 56 | +\begin{equation} |
| 57 | +x_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a}, x_2 = \frac{-b + \sqrt{b^2 - 4ac}}{2a} |
| 58 | +\end{equation} and your function should return $x_1$ and $x_2$. |
| 59 | +\item What is the output of the following code |
| 60 | +\begin{minted} |
| 61 | +{python} |
| 62 | +x = 1 |
| 63 | +def new_x(): |
| 64 | + x = 2 |
| 65 | + print(x) |
| 66 | +new_x() |
| 67 | +print(x) |
| 68 | +\end{minted} |
| 69 | +\item What is the name of the function within a class that runs automatically on each new instance of an object? |
| 70 | +\item Consider an object named a. What is one way to list all of the attributes (and methods) within the object a? |
| 71 | +\item How would you take the square root of 63.1516 in Python? (if you use a library you must stat the correct import) |
| 72 | +\item Consider a list name x that already contains a lot of information. Consider a list y = ['bob', 'loves Python', 87289]. How would you add y to the end of list x? |
| 73 | +\item z is a high dimensional numpy array. How would you find the index location of the maximum value of z? |
| 74 | +\item Code a: |
| 75 | +\begin{minted} |
| 76 | +{python} |
| 77 | +import numpy as np |
| 78 | +x = np.random.random(100000) |
| 79 | +y = [] |
| 80 | +for i in x: |
| 81 | + y.append(2.0*x) |
| 82 | +y = np.array(y) |
| 83 | +\end{minted} |
| 84 | + |
| 85 | +Code b: |
| 86 | +\begin{minted} |
| 87 | +{python} |
| 88 | +import numpy as np |
| 89 | +x = np.random.random(100000) |
| 90 | +y = 2.0*x |
| 91 | +\end{minted} |
| 92 | + |
| 93 | +Code a and code b do the exact same thing. Which code will run faster? (a, b, or both will run the same speed) Why? |
| 94 | +\item Your friend is new to Python and programming. He is running the following code: |
| 95 | +\begin{minted} |
| 96 | +{python} |
| 97 | +from __future__ import division |
| 98 | +import numpy as np |
| 99 | +x = np.ones(10, dtype='int') |
| 100 | +y = np.random.random(10) |
| 101 | +for i in range(10): |
| 102 | + x[i] = y[i]/2.0 |
| 103 | +\end{minted} |
| 104 | +but he keeps getting that x = array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]). Why is each item in x zero? |
| 105 | +\item given |
| 106 | +x = np.array([[4.0, 2.0],[-2.0,3.0]]) |
| 107 | + |
| 108 | +How would you transpose x? |
| 109 | +\item Write out the values in z |
| 110 | +\begin{minted} |
| 111 | +{python} |
| 112 | +x = np.array([[4.0, 2.0], |
| 113 | + [-2.0,3.0]]) |
| 114 | +y = np.array([[2.0, 1.0], |
| 115 | + [0.0, 1.0]) |
| 116 | +z = x*y |
| 117 | +\end{minted} |
| 118 | +\item Consider the following matrix multiplication |
| 119 | +\begin{equation} |
| 120 | +\begin{bmatrix} |
| 121 | +2 & 3 \\ |
| 122 | +1 & 0 \\ |
| 123 | +\end{bmatrix}\begin{bmatrix} |
| 124 | +5 & 2 \\ |
| 125 | +1 & 3 \\ |
| 126 | +\end{bmatrix}\begin{bmatrix} |
| 127 | +1 & 0 \\ |
| 128 | +2 & 0 \\ |
| 129 | +\end{bmatrix} = z |
| 130 | +\end{equation} Write out all the code (including imports) to solve for z in Python. |
| 131 | +\item Plot the function |
| 132 | +\begin{equation} |
| 133 | +y(x) = x^3 + 2x + 10.0 |
| 134 | +\end{equation} on the domain \begin{equation} |
| 135 | +10 \leq x \leq 19 |
| 136 | +\end{equation} using Python. Include all necessary imported libraries. |
| 137 | +\item Define a function that will return \textit{A}, \textit{B}. \textit{A} and \textit{B} are variables passed to the function. If B is not specified, it will default to a value of 1.0. |
| 138 | +\item Open readme.txt using Python and print the contents. |
| 139 | +\item What is the difference between |
| 140 | +\begin{minted} |
| 141 | +{python} |
| 142 | +f = open(myFile) |
| 143 | +\end{minted} |
| 144 | +and |
| 145 | +\begin{minted} |
| 146 | +{python} |
| 147 | +with open(myFile) as f: |
| 148 | +\end{minted} |
| 149 | +\item Why Does the following code produce x = array([0, 0, 1]) in Python? |
| 150 | +\begin{minted} |
| 151 | +{python} |
| 152 | +x = np.array((0,0,0)) |
| 153 | +for i in range(3): |
| 154 | + x[i] = i/2.0 |
| 155 | +\end{minted} |
| 156 | +Fix this code such that x = array([0.0, 0.5, 1.0]) after the for loop. |
| 157 | +\item Correct this code |
| 158 | +\begin{minted} |
| 159 | +{python} |
| 160 | +for i in range(10): |
| 161 | + if i > 5 |
| 162 | + break |
| 163 | +\end{minted} |
| 164 | +such that it no longer produces this SyntaxError |
| 165 | +\begin{minted} |
| 166 | +{python} |
| 167 | + if i > 5 |
| 168 | + ^ |
| 169 | +SyntaxError: invalid syntax |
| 170 | +\end{minted} |
| 171 | +\item Why would you use numpy.random.seed() when generating random numbers? |
| 172 | +\item Your friend wanted to grab the First item in the numpy array \textit{x}. Consider his code: |
| 173 | +\begin{minted} |
| 174 | +{python} |
| 175 | +x = np.random.random(100) |
| 176 | +y = x(1) |
| 177 | +\end{minted} |
| 178 | +produces the Following syntax error |
| 179 | +\begin{minted} |
| 180 | +{python} |
| 181 | + 1 x = np.random.random(100) |
| 182 | + 2 x = np.random.random(100) |
| 183 | +----> 3 y = x(1) |
| 184 | + |
| 185 | +TypeError: 'numpy.ndarray' object is not callable |
| 186 | +\end{minted} |
| 187 | +What two things is your friend missing? |
| 188 | +\item What does the function \textit{os.system} do? Name a potential example where \textit{os.system} could be useful in a Python program? |
| 189 | +\item What is the Difference between Built-In Python libraries like os and math and libraries like numpy, pandas, and matplotlib? |
| 190 | +\item Consider |
| 191 | +\mint{python}|a = [0.0, 1.0, ['hi', 'world'], 2.0, 'blue']| |
| 192 | +what will |
| 193 | +\mint{python}|print(len(a))| |
| 194 | +return? |
| 195 | +\end{enumerate} |
| 196 | + |
| 197 | + |
| 198 | +\end{document} |
0 commit comments