Skip to content

Commit efa39d9

Browse files
committed
module 7
1 parent bd7e344 commit efa39d9

24 files changed

Lines changed: 2427 additions & 11 deletions

_quarto.yml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,28 @@ website:
3232
style: 'docked'
3333
background: 'primary'
3434
contents:
35-
- section: "**M0. Welcome to Programming in Python for Data Science**"
35+
- section: "**Module 7: Importing Files and the Coding Style Guide**"
3636
contents:
37-
- text: '0. Welcome!'
38-
href: modules/index.qmd
39-
- href: modules/module0/module0-01-programming_in_python_for_data_science.qmd
40-
- text: '   1.1. Prerequisite confirmation'
41-
href: modules/module0/module0-02-are_you_ready.qmd
42-
- section: "**M1. Python & Pandas - An Unexpected Friendship**"
37+
- href: modules/module7/module7-00-module_learning_outcomes.qmd
38+
- href: modules/module7/module7-01-importing_python_libraries.qmd
39+
- text: '   1.1. Exercises'
40+
href: modules/module7/module7-02-importing_packages.qmd
41+
- href: modules/module7/module7-05-working_with_other_files.qmd
42+
- text: '   2.1. Exercises'
43+
href: modules/module7/module7-06-importin_your_own_functions_quesitons.qmd
44+
- href: modules/module7/module7-08-testing_your_own_functions_with_pytest.qmd
45+
- text: '   3.1. Exercises'
46+
href: modules/module7/module7-09-using_pytest_questions.qmd
47+
- href: modules/module7/module7-12-automatic_style_formatters.qmd
48+
- text: '   4.1. Exercises'
49+
href: modules/module7/module7-13-flake8_and_black.qmd
50+
- href: modules/module7/module7-15-formatting_that_can't_be_fixed_automatically.qmd
51+
- text: '   5.1. Exercises'
52+
href: modules/module7/module7-16-writing_useful_comments.qmd
53+
- href: modules/module7/module7-18-the_python_debugger.qmd
54+
- text: '   6.1. Exercises'
55+
href: modules/module7/module7-19-using_the_python_debugger.qmd
56+
- href: modules/module7/module7-20-what_did_we_just_learn.qmd
4357

4458
# Since we are declaring options for two formats here (html and revealjs)
4559
# each qmd file needs to include a yaml block including which format to use for that file.

environment.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ dependencies:
99
- scipy
1010
- matplotlib
1111
- jupyter
12-
- quarto
13-
- pip
14-
15-
12+
- quarto=1.6.43
13+
- flake8-nb
14+
- pytest
15+
- black[jupyter]
16+
- pip
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 0. Module Learning Outcomes
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/SbtORPZ2l6w?start=0&end=48&rel=0"
16+
title="Module 7 Video - Module Learning Outcomes"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module7_00.html"
27+
></iframe>
28+
29+
:::
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 1. Importing Python Libraries
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/cZwBfjQpHTU?start=0&end=172&rel=0"
16+
title="Module 7 Video - Importing Python Libraries"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module7_01.html"
27+
></iframe>
28+
29+
:::
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
::::
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 2. Working with Other Files
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/cZwBfjQpHTU?start=176&end=476&rel=0"
16+
title="Module 7 Video - Working with Other Files"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module7_05.html"
27+
></iframe>
28+
29+
:::
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
format: html
3+
---
4+
5+
<script src='../../src/quiz.js'></script>
6+
7+
# 2.1. Exercises
8+
9+
## Importing Your Own Functions Questions
10+
11+
<div id='mcq1'></div>
12+
<script>
13+
generateQuiz(
14+
'mcq1',
15+
'Question 1',
16+
'Where do you save your files so you can import them into new Jupyter notebooks?',
17+
{
18+
'<code>.python</code>': 'Not quite but you are on the right track.',
19+
'<code>.py</code>': '',
20+
'<code>.ipynb</code>': 'This is a Jupyter notebook file, not a file to import a function from.',
21+
},
22+
'<code>.py</code>',
23+
);
24+
</script>
25+
26+
<div id='mcq2'></div>
27+
<script>
28+
generateQuiz(
29+
'mcq2',
30+
'Question 2: Is the following statement True or False?',
31+
'You can import files containing functions in a similar way to how you import Python libraries.',
32+
{
33+
'True': 'That’s right! This makes things easy for us.',
34+
'False': 'Note quite, Python uses a similar importing style for libraries and saved scripts.',
35+
},
36+
'True',
37+
);
38+
</script>
39+
40+
## More Importing Your Own Functions Questions
41+
42+
<div id='mcq3'></div>
43+
<script>
44+
generateQuiz(
45+
'mcq3',
46+
'Question 1',
47+
'If I have a file named <code>baking.py</code> containing functions like <code>cake()</code> and <code>scones()</code> and I want to import it into a Jupyter notebook using the alias <code>bake</code>, which of the following would be required?',
48+
{
49+
'<code>import cake from baking</code>': '<code>cake</code> is the function name, not the library.',
50+
'<code>from baking import bake</code>': 'This isn’t quite right. <code>bake</code> is an alias, not a package.',
51+
'<code>import baking as bake</code>': 'This is right!',
52+
'<code>import bake as scone</code>': 'The alias we want to call <code>baking</code> is <code>bake</code>, not <code>scone</code>.',
53+
},
54+
'<code>import baking as bake</code>',
55+
);
56+
</script>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
format:
3+
html:
4+
page-layout: full
5+
---
6+
7+
# 3. Testing Your Own Functions with Pytest
8+
9+
::: {.panel-tabset .nav-pills}
10+
11+
## Video
12+
13+
<iframe
14+
class="video"
15+
src="https://www.youtube.com/embed/cZwBfjQpHTU?start=483&end=779&rel=0"
16+
title="Module 7 Video - Testing Your Own Functions with Pytest"
17+
frameborder="0"
18+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
19+
allowfullscreen
20+
></iframe>
21+
22+
## Slides
23+
24+
<iframe
25+
class="slide-deck"
26+
src="slides/module7_08.html"
27+
></iframe>
28+
29+
:::

0 commit comments

Comments
 (0)