You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b3d4025
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: episodes/conditionals.md
+46-46Lines changed: 46 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,6 +76,52 @@ for checkout in checkouts:
76
76
77
77
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.
78
78
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.')
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:
153
199
*Warning*: 120 is over the grad limit.
154
200
```
155
201
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.')
{alt="histogram of the Albany branch circulation."}
140
140
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
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
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
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
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.
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.
@@ -151,7 +151,7 @@ If you move your cursor back to the first cell, just after the `7 * 3` code, and
151
151
152
152
```python
153
153
7*3
154
-
2+1
154
+
2+1
155
155
```
156
156
157
157
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