Skip to content

Commit 438124c

Browse files
committed
markdown source builds
Auto-generated via `{sandpaper}` Source : b3d4025 Branch : main Author : Cody Hennesy <codyhennesy@gmail.com> Time : 2026-10-07 21:38:50 +0000 Message : Merge pull request #188 from LibraryCarpentry/aays-patch-exercises Spread out exercises in each episode. Fixes #165
1 parent 13b7479 commit 438124c

12 files changed

Lines changed: 640 additions & 626 deletions

‎conditionals.md‎

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,52 @@ for checkout in checkouts:
7676

7777
Notice that our `else` statement led to a false output that says 10 is under the limit. We can address this by adding a different kind of `else` statement.
7878

79+
::::::::::::::::::::::::::::::::::::::: challenge
80+
81+
## Age conditionals
82+
83+
Write a Python program that checks the age of a user to determine if they will receive a youth or adult library card. The program should:
84+
85+
1. Store `age` in a variable.
86+
2. Use an `if` statement to check if the age is 16 or older. If true, print "You are eligible for an adult library card."
87+
3. Use an `else` statement to print "You are eligible for a youth library card" if the age is less than 16.
88+
89+
If you finish early, try this challenge:
90+
91+
- In a new cell, adapt your program to loop through a list of age values, testing each age with the same output as above.
92+
93+
::::::::::::::: solution
94+
95+
## Solution
96+
97+
For parts 1 to 3:
98+
99+
```python
100+
age = 25
101+
102+
if age >= 16:
103+
print('You are eligible for an adult library card.')
104+
else:
105+
print('You are eligible for a youth library card.')
106+
```
107+
108+
For the challenge:
109+
```python
110+
ages = [10, 16, 30, 65]
111+
112+
for age in ages:
113+
if age >= 16:
114+
print('You are eligible for an adult library card.')
115+
else:
116+
print('You are eligible for a youth library card.')
117+
```
118+
119+
120+
:::::::::::::::::::::::::
121+
122+
::::::::::::::::::::::::::::::::::::::::::::::::::
123+
124+
79125
## Use `elif` to specify additional tests.
80126

81127
You can use `elif` (short for "else if") to provide several alternative choices, each with its own test. An `elif` statement should always be associated with an `if` statement, and must come before the `else` statement (which is the catch all).
@@ -153,52 +199,6 @@ for user in users:
153199
*Warning*: 120 is over the grad limit.
154200
```
155201

156-
::::::::::::::::::::::::::::::::::::::: challenge
157-
158-
## Age conditionals
159-
160-
Write a Python program that checks the age of a user to determine if they will receive a youth or adult library card. The program should:
161-
162-
1. Store `age` in a variable.
163-
2. Use an `if` statement to check if the age is 16 or older. If true, print "You are eligible for an adult library card."
164-
3. Use an `else` statement to print "You are eligible for a youth library card" if the age is less than 16.
165-
166-
If you finish early, try this challenge:
167-
168-
- In a new cell, adapt your program to loop through a list of age values, testing each age with the same output as above.
169-
170-
::::::::::::::: solution
171-
172-
## Solution
173-
174-
For parts 1 to 3:
175-
176-
```python
177-
age = 25
178-
179-
if age >= 16:
180-
print('You are eligible for an adult library card.')
181-
else:
182-
print('You are eligible for a youth library card.')
183-
```
184-
185-
For the challenge:
186-
```python
187-
ages = [10, 16, 30, 65]
188-
189-
for age in ages:
190-
if age >= 16:
191-
print('You are eligible for an adult library card.')
192-
else:
193-
print('You are eligible for a youth library card.')
194-
```
195-
196-
197-
:::::::::::::::::::::::::
198-
199-
::::::::::::::::::::::::::::::::::::::::::::::::::
200-
201-
202202
::::::::::::::::::::::::::::::::::::::: challenge
203203

204204
## Conditional logic: Fill in the blanks

‎data-visualisation.md‎

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -138,55 +138,6 @@ albany['circulation'].plot(kind='hist', bins=20,
138138

139139
![](fig/albany-circ-hist-9.png){alt="histogram of the Albany branch circulation."}
140140

141-
## Use Plotly for interactive plots
142-
143-
Let’s switch back to the full DataFrame in `df_long` and use another
144-
plotting package in Python called Plotly.
145-
146-
```python
147-
import plotly.express as px
148-
```
149-
150-
Now we can visualize how circulation counts have changed over time for selected branches. This can be especially useful for identifying trends, seasonality, or data anomalies. We willfirst create a subset of our data to look at branches starting with the letter 'A'. Feel free to select different branches. After subsetting, we will sort our new DataFrame by date and then plot our data by date and circulation count.
151-
152-
``` python
153-
# Creating a line plot for a few selected branches to avoid clutter
154-
selected_branches = df_long[df_long['branch'].isin(['Altgeld',
155-
'Archer Heights',
156-
'Austin',
157-
'Austin-Irving',
158-
'Avalon'])]
159-
selected_branches = selected_branches.sort_values(by='date')
160-
```
161-
162-
``` python
163-
fig = px.line(selected_branches, x=selected_branches.index, y='circulation', color='branch', title='Circulation Over Time for Selected Branches')
164-
fig.show()
165-
```
166-
167-
Here is a view of the [interactive output of the Plotly line chart](learners/line_plot_int.html).
168-
169-
170-
One advantage that Plotly provides over Matplotlib is that it has some interactive features out of the box. Hover your cursor over the lines in the output to find out more granular data about specific branches over time.
171-
172-
173-
### Bar plots with Plotly
174-
175-
Let’s use a barplot to compare the distribution of circulation counts
176-
among branches. We first need to group our data by branch and sum up the circulation counts. Then we can use the bar plot to show the
177-
distribution of total circulation over branches.
178-
179-
``` python
180-
# Aggregate circulation by branch
181-
total_circulation_by_branch = df_long.groupby('branch')['circulation'].sum().reset_index()
182-
183-
# Create a bar plot
184-
fig = px.bar(total_circulation_by_branch, x='branch', y='circulation', title='Total Circulation by Branch')
185-
fig.show()
186-
```
187-
188-
Here is a view of the [interactive output of the Plotly bar chart](learners/bar_plot_int.html).
189-
190141
::::::::::::::::::::::::::::::::::::::: challenge
191142

192143
## Plotting with Pandas
@@ -248,6 +199,56 @@ uptown['circulation'].plot(title='Uptown Circulation',
248199

249200
::::::::::::::::::::::::::::::::::::::::::::::::::
250201

202+
203+
## Use Plotly for interactive plots
204+
205+
Let’s switch back to the full DataFrame in `df_long` and use another
206+
plotting package in Python called Plotly.
207+
208+
```python
209+
import plotly.express as px
210+
```
211+
212+
Now we can visualize how circulation counts have changed over time for selected branches. This can be especially useful for identifying trends, seasonality, or data anomalies. We willfirst create a subset of our data to look at branches starting with the letter 'A'. Feel free to select different branches. After subsetting, we will sort our new DataFrame by date and then plot our data by date and circulation count.
213+
214+
``` python
215+
# Creating a line plot for a few selected branches to avoid clutter
216+
selected_branches = df_long[df_long['branch'].isin(['Altgeld',
217+
'Archer Heights',
218+
'Austin',
219+
'Austin-Irving',
220+
'Avalon'])]
221+
selected_branches = selected_branches.sort_values(by='date')
222+
```
223+
224+
``` python
225+
fig = px.line(selected_branches, x=selected_branches.index, y='circulation', color='branch', title='Circulation Over Time for Selected Branches')
226+
fig.show()
227+
```
228+
229+
Here is a view of the [interactive output of the Plotly line chart](learners/line_plot_int.html).
230+
231+
232+
One advantage that Plotly provides over Matplotlib is that it has some interactive features out of the box. Hover your cursor over the lines in the output to find out more granular data about specific branches over time.
233+
234+
235+
### Bar plots with Plotly
236+
237+
Let’s use a barplot to compare the distribution of circulation counts
238+
among branches. We first need to group our data by branch and sum up the circulation counts. Then we can use the bar plot to show the
239+
distribution of total circulation over branches.
240+
241+
``` python
242+
# Aggregate circulation by branch
243+
total_circulation_by_branch = df_long.groupby('branch')['circulation'].sum().reset_index()
244+
245+
# Create a bar plot
246+
fig = px.bar(total_circulation_by_branch, x='branch', y='circulation', title='Total Circulation by Branch')
247+
fig.show()
248+
```
249+
250+
Here is a view of the [interactive output of the Plotly bar chart](learners/bar_plot_int.html).
251+
251252
::::::::::::::::::::::::::::::::::::::: challenge
252253

253254
## Plot the top five branches

‎for-loops.md‎

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,38 @@ for number in range(0,3):
163163
2
164164
```
165165

166+
::::::::::::::::::::::::::::::::::::::: challenge
167+
168+
## Use range() in a loop
169+
170+
Print out the numbers 10, 11, 12, 13, 14, 15, using range() in a `for` loop.
171+
172+
::::::::::::::: solution
173+
174+
## Solution
175+
176+
```python
177+
178+
for num in range(10, 16):
179+
print(num)
180+
181+
```
182+
183+
```output
184+
10
185+
11
186+
12
187+
13
188+
14
189+
15
190+
```
191+
192+
193+
:::::::::::::::::::::::::
194+
195+
::::::::::::::::::::::::::::::::::::::::::::::::::
196+
197+
166198
## Accumulators
167199

168200
A common loop pattern is to initialize an *accumulator* variable to zero, an empty string, or an empty list before the loop begins. Then the loop updates the accumulator variable with values from a collection.
@@ -249,37 +281,6 @@ for veg in vegetables:
249281
::::::::::::::::::::::::::::::::::::::::::::::::::
250282
::::::::::::::::::::::::::::::::::::::: challenge
251283

252-
## Use range() in a loop
253-
254-
Print out the numbers 10, 11, 12, 13, 14, 15, using range() in a `for` loop.
255-
256-
::::::::::::::: solution
257-
258-
## Solution
259-
260-
```python
261-
262-
for num in range(10, 16):
263-
print(num)
264-
265-
```
266-
267-
```output
268-
10
269-
11
270-
12
271-
13
272-
14
273-
15
274-
```
275-
276-
277-
:::::::::::::::::::::::::
278-
279-
::::::::::::::::::::::::::::::::::::::::::::::::::
280-
281-
::::::::::::::::::::::::::::::::::::::: challenge
282-
283284
## Use a string index in a loop
284285

285286
How would you loop through a list with the values 'red', 'green', and 'blue' to create the acronym `rgb`, pulling from the first letters in each string? Print the acronym when the loop is finished.

‎getting-started.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exercises: 5
1818

1919
- How can I identify and use key features of JupyterLab to create and manage a Python notebook?
2020
- How do I run Python code in JupyterLab, and how can I see and interpret the results?
21-
-
21+
2222
::::::::::::::::::::::::::::::::::::::::::::::::::
2323

2424
## Why Python?
@@ -151,7 +151,7 @@ If you move your cursor back to the first cell, just after the `7 * 3` code, and
151151

152152
```python
153153
7 * 3
154-
2 +1
154+
2 + 1
155155
```
156156

157157
While Python runs both calculations Juypter will only display the output from the last line of code in a specific cell, unless you tell it to do otherwise.

0 commit comments

Comments
 (0)