|
| 1 | +--- |
| 2 | +format: live-html |
| 3 | +--- |
| 4 | + |
| 5 | +<script src='../../src/quiz.js'></script> |
| 6 | + |
| 7 | +# 1.1. Exercises |
| 8 | + |
| 9 | +## Importing packages |
| 10 | + |
| 11 | +<div id='mcq1'></div> |
| 12 | +<script> |
| 13 | + generateQuiz( |
| 14 | + 'mcq1', |
| 15 | + 'Question 1', |
| 16 | + 'How would you import a package named <code>numpy</code>?', |
| 17 | + { |
| 18 | + '<code>import numpy </code>': 'This is the basic way to import a Python package.', |
| 19 | + '<code>as np import numpy </code>': 'Unfortunately, this would not import <code>numpy</code>.', |
| 20 | + '<code>from numpy import numpy</code>': 'Are you sure you read the slides properly?', |
| 21 | + }, |
| 22 | + '<code>import numpy </code>', |
| 23 | + ); |
| 24 | +</script> |
| 25 | + |
| 26 | +<div id='mcq2'></div> |
| 27 | +<script> |
| 28 | + generateQuiz( |
| 29 | + 'mcq2', |
| 30 | + 'Question 2', |
| 31 | + 'How would you import <code>numpy</code> if you wanted to refer to it as <code>np</code>?', |
| 32 | + { |
| 33 | + '<code>as np import numpy </code>': 'This would actually result in an error!', |
| 34 | + '<code>Import numpy As np</code>': 'Be careful with capitals. In this case, when you use capitalization, neither <code>Import</code> nor <code>As</code> are Python keywords.', |
| 35 | + '<code>import numpy as np </code>': '', |
| 36 | + '<code>As np Import numpy </code>': 'This is not the correct way to import, and it uses capitalization on keywords which is incorrect.', |
| 37 | + }, |
| 38 | + '<code>import numpy as np </code>', |
| 39 | + ); |
| 40 | +</script> |
| 41 | + |
| 42 | +## Importing a Package Function |
| 43 | + |
| 44 | +<div id='mcq3'></div> |
| 45 | +<script> |
| 46 | + generateQuiz( |
| 47 | + 'mcq3', |
| 48 | + 'Question 1', |
| 49 | + 'How would you import the square root function <code>sqrt</code> from the <code>numpy</code> package?', |
| 50 | + { |
| 51 | + '<code>import sqrt from numpy</code>': 'Maybe try ordering this differently?', |
| 52 | + '<code>from numpy import sqrt</code>': '', |
| 53 | + '<code>from sqrt import numpy</code>': 'We are importing only the <code>sqrt</code> function from the <code>numpy</code> package.', |
| 54 | + '<code>import numpy from sqrt</code>': '<code>sqrt</code> is a single function that we want to import from <code>numpy</code>.', |
| 55 | + }, |
| 56 | + '<code>from numpy import sqrt</code>', |
| 57 | + ); |
| 58 | +</script> |
| 59 | + |
| 60 | + |
| 61 | +## Importing Packages... Again |
| 62 | + |
| 63 | +**Instructions:** |
| 64 | +Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes. |
| 65 | + |
| 66 | +**When you see `____` in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.** |
| 67 | + |
| 68 | +_**Make sure you remove the hash (`#`) symbol in the coding portions of this question. We have commented them so that the line won't execute and you can test your code after each step.**_ |
| 69 | + |
| 70 | +Ok, so we've seen this `numpy` package, let's actually load in one of the functions and use it! If you are wondering what this package does, don't worry, you'll learn more of this in the next module. `numpy` has a function called `power()`. |
| 71 | + |
| 72 | +**Tasks:** |
| 73 | + |
| 74 | +- Import the `power()` function from the `numpy` package. |
| 75 | +- Use the `power()` function to find 7 to the power of 5 and save it in an object named `power7_5` - you may want to use `?power` to see what arguments the function requires. |
| 76 | +- Display your results. |
| 77 | + |
| 78 | +```{pyodide} |
| 79 | +#| setup: true |
| 80 | +#| exercise: importing_packages_again |
| 81 | +import pandas as pd |
| 82 | +``` |
| 83 | + |
| 84 | +```{pyodide} |
| 85 | +#| exercise: importing_packages_again |
| 86 | +# Import the power() function from the numpy package |
| 87 | +____ |
| 88 | +
|
| 89 | +# Use the power function to find 7 to the power of 5 |
| 90 | +# Save your solution in an object named `power7_5` |
| 91 | +____ = ____ |
| 92 | +
|
| 93 | +# Display your results |
| 94 | +____ |
| 95 | +``` |
| 96 | + |
| 97 | +```{pyodide} |
| 98 | +#| exercise: importing_packages_again |
| 99 | +#| check: true |
| 100 | +from numpy import power |
| 101 | +from src.utils import print_correct_msg |
| 102 | +
|
| 103 | +assert result == power(7, 5), "Check your result." |
| 104 | +print_correct_msg() |
| 105 | +``` |
| 106 | + |
| 107 | +:::: { .hint exercise="importing_packages_again"} |
| 108 | +::: { .callout-note collapse="false"} |
| 109 | + |
| 110 | +## Hint 1 |
| 111 | + |
| 112 | +- Are you using `power(7, 5)`? |
| 113 | +- Are you importing using `from`? |
| 114 | + |
| 115 | +::: |
| 116 | +:::: |
| 117 | + |
| 118 | +:::: { .solution exercise="importing_packages_again" } |
| 119 | +::: { .callout-tip collapse="false"} |
| 120 | + |
| 121 | +## Fully worked solution: |
| 122 | + |
| 123 | +```{pyodide} |
| 124 | +# Import the power() function from the numpy package |
| 125 | +from numpy import power |
| 126 | +
|
| 127 | +# Use the power function to find 7 to the power of 5 |
| 128 | +# Save your solution in an object named `power7_5` |
| 129 | +power7_5 = power(7, 5) |
| 130 | +
|
| 131 | +# Display your results |
| 132 | +power7_5 |
| 133 | +``` |
| 134 | + |
| 135 | +::: |
| 136 | +:::: |
0 commit comments