Skip to content

Commit a9c176e

Browse files
committed
source commit: 3fce9c3
0 parents  commit a9c176e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+5916
-0
lines changed

CODE_OF_CONDUCT.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: "Contributor Code of Conduct"
3+
---
4+
5+
As contributors and maintainers of this project,
6+
we pledge to follow the [The Carpentries Code of Conduct][coc].
7+
8+
Instances of abusive, harassing, or otherwise unacceptable behavior
9+
may be reported by following our [reporting guidelines][coc-reporting].
10+
11+
12+
[coc-reporting]: https://docs.carpentries.org/topic_folders/policies/incident-reporting.html
13+
[coc]: https://docs.carpentries.org/topic_folders/policies/code-of-conduct.html

LICENSE.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: "Licenses"
3+
---
4+
5+
## Instructional Material
6+
7+
All Carpentries (Software Carpentry, Data Carpentry, and Library Carpentry)
8+
instructional material is made available under the [Creative Commons
9+
Attribution license][cc-by-human]. The following is a human-readable summary of
10+
(and not a substitute for) the [full legal text of the CC BY 4.0
11+
license][cc-by-legal].
12+
13+
You are free:
14+
15+
- to **Share**---copy and redistribute the material in any medium or format
16+
- to **Adapt**---remix, transform, and build upon the material
17+
18+
for any purpose, even commercially.
19+
20+
The licensor cannot revoke these freedoms as long as you follow the license
21+
terms.
22+
23+
Under the following terms:
24+
25+
- **Attribution**---You must give appropriate credit (mentioning that your work
26+
is derived from work that is Copyright (c) The Carpentries and, where
27+
practical, linking to <https://carpentries.org/>), provide a [link to the
28+
license][cc-by-human], and indicate if changes were made. You may do so in
29+
any reasonable manner, but not in any way that suggests the licensor endorses
30+
you or your use.
31+
32+
- **No additional restrictions**---You may not apply legal terms or
33+
technological measures that legally restrict others from doing anything the
34+
license permits. With the understanding that:
35+
36+
Notices:
37+
38+
* You do not have to comply with the license for elements of the material in
39+
the public domain or where your use is permitted by an applicable exception
40+
or limitation.
41+
* No warranties are given. The license may not give you all of the permissions
42+
necessary for your intended use. For example, other rights such as publicity,
43+
privacy, or moral rights may limit how you use the material.
44+
45+
## Software
46+
47+
Except where otherwise noted, the example programs and other software provided
48+
by The Carpentries are made available under the [OSI][osi]-approved [MIT
49+
license][mit-license].
50+
51+
Permission is hereby granted, free of charge, to any person obtaining a copy of
52+
this software and associated documentation files (the "Software"), to deal in
53+
the Software without restriction, including without limitation the rights to
54+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
55+
of the Software, and to permit persons to whom the Software is furnished to do
56+
so, subject to the following conditions:
57+
58+
The above copyright notice and this permission notice shall be included in all
59+
copies or substantial portions of the Software.
60+
61+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67+
SOFTWARE.
68+
69+
## Trademark
70+
71+
"The Carpentries", "Software Carpentry", "Data Carpentry", and "Library
72+
Carpentry" and their respective logos are registered trademarks of [Community
73+
Initiatives][ci].
74+
75+
[cc-by-human]: https://creativecommons.org/licenses/by/4.0/
76+
[cc-by-legal]: https://creativecommons.org/licenses/by/4.0/legalcode
77+
[mit-license]: https://opensource.org/licenses/mit-license.html
78+
[ci]: https://communityin.org/
79+
[osi]: https://opensource.org

bar_plot_int.html

Lines changed: 14 additions & 0 deletions
Large diffs are not rendered by default.

conditionals.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
---
2+
title: Conditionals
3+
teaching: 15
4+
exercises: 10
5+
---
6+
7+
::::::::::::::::::::::::::::::::::::::: objectives
8+
9+
- Correctly write programs that use `if` and `else` statements using Boolean expressions.
10+
- Trace the execution of conditionals inside of loops.
11+
12+
::::::::::::::::::::::::::::::::::::::::::::::::::
13+
14+
:::::::::::::::::::::::::::::::::::::::: questions
15+
16+
- How can programs do different things for different data?
17+
18+
::::::::::::::::::::::::::::::::::::::::::::::::::
19+
20+
## Use `if` statements to control whether or not a block of code is executed.
21+
22+
An `if` statement is a *conditional* statement that controls whether a block of code is executed or not. The syntax of an `if` statement is similar to a `for` statement:
23+
24+
- The first line opens with `if` and ends with a colon.
25+
- The body is indented (usually by 4 spaces)
26+
27+
```python
28+
checkouts = 11
29+
if checkouts > 10.0:
30+
print(f'{checkouts} is over the limit.')
31+
32+
checkouts = 8
33+
if checkouts > 10.0:
34+
print(f'{checkouts} is over the limit.')
35+
```
36+
37+
```output
38+
11 is over the limit.
39+
```
40+
41+
## Conditionals are often used inside loops.
42+
43+
There is not much of a point using a conditional when we know the value (as above), but they're useful when we have a collection to process.
44+
45+
```python
46+
checkouts = [0, 3, 10, 12, 22]
47+
for checkout in checkouts:
48+
if checkout > 10.0:
49+
print(f'{checkout} is over the limit.')
50+
```
51+
52+
```output
53+
12 is over the limit.
54+
22 is over the limit.
55+
```
56+
57+
## Use `else` to execute a block of code when an `if` condition is *not* true.
58+
59+
An `else` statement can be used following `if` to allow us to specify an alternative code block to execute when the `if` *branch* is not taken.
60+
61+
```python
62+
for checkout in checkouts:
63+
if checkout > 10.0:
64+
print(f'{checkout} is over the limit.')
65+
else:
66+
print(f'{checkout} is under the limit.')
67+
```
68+
69+
```output
70+
0 is under the limit.
71+
3 is under the limit.
72+
10 is under the limit.
73+
12 is over the limit.
74+
22 is over the limit.
75+
```
76+
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+
79+
## Use `elif` to specify additional tests.
80+
81+
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).
82+
83+
```python
84+
for checkout in checkouts:
85+
if checkout > 10.0:
86+
print(f'*Warning*: {checkout} is over the limit.')
87+
elif checkout == 10:
88+
print(f'{checkout} is at the exact limit.')
89+
else:
90+
print(f'{checkout} is under the limit.')
91+
```
92+
93+
```output
94+
0 is under the limit.
95+
3 is under the limit.
96+
10 is at the exact limit.
97+
*Warning*: 12 is over the limit.
98+
*Warning*: 22 is over the limit.
99+
100+
```
101+
102+
Conditions are tested once, in order and are not re-evaluated if values change. Python steps through the branches of the conditional in order, testing each in turn, so the order of your statements matters.
103+
104+
```python
105+
grade = 85
106+
if grade >= 70:
107+
print('grade is C')
108+
elif grade >= 80:
109+
print('grade is B')
110+
elif grade >= 90:
111+
print('grade is A')
112+
```
113+
114+
```output
115+
grade is C
116+
```
117+
118+
## Compound conditionals using `and` and `or`
119+
120+
Often, you want some combination of things to be true. You can combine
121+
relations within a conditional using `and` and `or`.
122+
123+
We can also check if something is less/greater than or equal to a value by using `>=` and `<=` operators.
124+
125+
```python
126+
checkouts = [3, 50, 120]
127+
users = ['fac', 'grad']
128+
129+
for user in users:
130+
for checkout in checkouts:
131+
#faculty checkout limit is 100
132+
if checkout >= 100 and user == 'fac':
133+
print(f"*Warning*: {checkout} is over the {user} limit.")
134+
135+
#grad limit is 50
136+
elif checkout >= 50 and user == 'grad':
137+
print(f"{checkout} is over the {user} limit.")
138+
139+
else:
140+
print(f"{checkout} is under the {user} limit.")
141+
142+
# print an empty line between users
143+
print()
144+
145+
```
146+
```output
147+
3 is under the fac limit.
148+
50 is under the fac limit.
149+
*Warning*: 120 is over the fac limit.
150+
151+
3 is under the grad limit.
152+
*Warning*: 50 is over the grad limit.
153+
*Warning*: 120 is over the grad limit.
154+
```
155+
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+
202+
::::::::::::::::::::::::::::::::::::::: challenge
203+
204+
## Conditional logic: Fill in the blanks
205+
206+
Fill in the blanks in the following program to check if both the name variable is present in the names list and the password variable is equal to 'true' before giving a user access to a library system.
207+
208+
If you have extra time after you solve the fill in the blanks, change the value of `password` and re-run the program to view the output.
209+
210+
```python
211+
names = ['Wang', 'Garcia', 'Martin']
212+
name = 'Martin'
213+
password = 'true'
214+
215+
___ item in names:
216+
print(item)
217+
if name == item ___ password == _____:
218+
print('Login successful!')
219+
elif password __ 'true':
220+
print(f'Your password is incorrect. Try again.')
221+
____ name __ item:
222+
print(f'- Name does not match. Testing the next item in the list for {name}...')
223+
224+
```
225+
::::::::::::::: solution
226+
227+
## Solution
228+
229+
```python
230+
names = ['Wang', 'Garcia', 'Martin']
231+
name = 'Martin'
232+
password = 'true'
233+
234+
for item in names:
235+
print(item)
236+
if name == item and password == 'true':
237+
print('Login successful!')
238+
elif password != 'true':
239+
print(f'Your password is incorrect. Try again.')
240+
elif name != item:
241+
print(f'- Name does not match. Testing the next item in the list for {name}...')
242+
```
243+
244+
```output
245+
Wang
246+
- Name does not match. Testing the next item in the list for Martin...
247+
Garcia
248+
- Name does not match. Testing the next item in the list for Martin...
249+
Martin
250+
Login successful!
251+
```
252+
253+
:::::::::::::::::::::::::
254+
255+
::::::::::::::::::::::::::::::::::::::::::::::::::
256+
257+
::::::::::::::::::::::::::::::::::::::: challenge
258+
259+
## Processing Files Based on Record Length
260+
261+
Modify this program so that it only processes files with fewer than 85 records.
262+
263+
```python
264+
import glob
265+
import pandas
266+
for filename in glob.glob('data/*.csv'):
267+
contents = pandas.read_csv(filename)
268+
____ ___(______) < ____:
269+
print(f'{filename} : {len(contents)}')
270+
```
271+
272+
::::::::::::::: solution
273+
274+
## Solution
275+
276+
```python
277+
import glob
278+
import pandas
279+
for filename in glob.glob('data/*.csv'):
280+
contents = pandas.read_csv(filename)
281+
if len(contents) < 85:
282+
print(f'{filename} : {len(contents)}')
283+
```
284+
285+
:::::::::::::::::::::::::
286+
287+
::::::::::::::::::::::::::::::::::::::::::::::::::
288+
289+
:::::::::::::::::::::::::::::::::::::::: keypoints
290+
291+
- Use `if` statements to control whether or not a block of code is executed.
292+
- Conditionals are often used inside loops.
293+
- Use `else` to execute a block of code when an `if` condition is *not* true.
294+
- Use `elif` to specify additional tests.
295+
- Conditions are tested once, in order.
296+
- Use `and` and `or` to check against multiple value statements.
297+
298+
::::::::::::::::::::::::::::::::::::::::::::::::::
299+
300+

0 commit comments

Comments
 (0)