Skip to content

Commit f58191b

Browse files
committed
Added lesson 3 notebooks
1 parent 595f3c2 commit f58191b

File tree

2 files changed

+709
-0
lines changed

2 files changed

+709
-0
lines changed
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Conditional statements\n",
8+
"\n",
9+
"## Sources\n",
10+
"\n",
11+
"This lesson is based on the [Software Carpentry group’s](http://software-carpentry.org/) lessons on [Programming with Python](http://swcarpentry.github.io/python-novice-inflammation/).\n",
12+
"\n",
13+
"## Basics of conditional statements\n",
14+
"\n",
15+
"Conditional statements can change the code behaviour based on meeting certain conditions.\n",
16+
"\n",
17+
"### A simple conditional statement\n",
18+
"\n",
19+
"Let’s take a simple example."
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"it is not hot\n"
32+
]
33+
}
34+
],
35+
"source": []
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {},
40+
"source": [
41+
"What did we do here?\n",
42+
"First, we used the `if` and `else` statements to determine what parts of the code to execute.\n",
43+
"Note that both lines containing `if` or `else` end with a `:` and the text beneath is indented.\n",
44+
"What do these tests do?\n",
45+
"The if test checks to see whether the variable value for `temperature` is greater than 25.\n",
46+
"If so, `'it is hot'` would be written to the screen.\n",
47+
"Since 17 is smaller than 25, the code beneath the else is executed.\n",
48+
"The `else` statement code will run whenever the `if` test is false."
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"#### A familiar conditional scenario\n",
56+
"\n",
57+
"As it turns out, we all use logic similar to `if` and `else` conditional statements daily.\n",
58+
"Imagine you’re getting ready to leave your home for the day and want to decide what to wear.\n",
59+
"You might look outside to check the weather conditions.\n",
60+
"If it is raining, you will wear a rain jacket.\n",
61+
"Otherwise, you will not.\n",
62+
"In Python we could say:\n",
63+
"\n",
64+
"```python\n",
65+
"weather = 'Rain'\n",
66+
"\n",
67+
"if weather == 'Rain':\n",
68+
" print('Wear a raincoat')\n",
69+
"else:\n",
70+
" print('No raincoat needed')\n",
71+
"\n",
72+
"Wear a raincoat\n",
73+
"```\n",
74+
"Note here that we use the `==` to test if a value is exactly equal to another."
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"### else is not required\n",
82+
"\n",
83+
"The combination of if and else is very common, but both are not strictly required."
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 1,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": []
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": 2,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": []
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"Note that here we use only the `if` statement, and because 13 is not greater than 25, nothing is printed to the screen."
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"### Introducing second test\n",
112+
"\n",
113+
"We can also have a second test for an if statment by using the elif (else-if) statement."
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 3,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": []
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 4,
126+
"metadata": {},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"-3 is below freezing\n"
133+
]
134+
}
135+
],
136+
"source": []
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"Makes sense, right? Note here that we again use the == to test if a value is exactly equal to another. The complete list of these comparison operators is given in the table below.\n",
143+
"\n",
144+
"| Operator | Meaning |\n",
145+
"| -------- | ------------------------ |\n",
146+
"| < | Less than |\n",
147+
"| <= | Less than or equal to |\n",
148+
"| == | Equal to |\n",
149+
"| >= | Greater than or equal to |\n",
150+
"| > | Greater than |\n",
151+
"| != | Not equal to |"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"<hr>\n",
159+
"\n",
160+
"#### Poll question pause\n",
161+
"\n",
162+
"Time to check your understanding.\n",
163+
"Let's assume that yesterday it was 14°C, it is 10°C outside today, and tomorrow it will be 13°C.\n",
164+
"The following code compares these temperatures and prints something to the screen based on the comparison.\n",
165+
"\n",
166+
"```python\n",
167+
"yesterday = 14\n",
168+
"today = 10\n",
169+
"tomorrow = 13\n",
170+
"\n",
171+
"if yesterday <= today:\n",
172+
" print('A')\n",
173+
"elif today != tomorrow:\n",
174+
" print('B')\n",
175+
"elif yesterday > tomorrow:\n",
176+
" print('C')\n",
177+
"elif today == today:\n",
178+
" print('D')\n",
179+
"```\n",
180+
"\n",
181+
"Which of the letters `A`, `B`, `C`, and `D` would be printed to the screen?\n",
182+
"Select your answer from the poll options at https://geo-python.github.io/poll/.\n",
183+
"\n",
184+
"<hr>"
185+
]
186+
},
187+
{
188+
"cell_type": "markdown",
189+
"metadata": {},
190+
"source": [
191+
"### Combining conditions\n",
192+
"\n",
193+
"We can also use `and` and `or` to have multiple conditions."
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 5,
199+
"metadata": {},
200+
"outputs": [
201+
{
202+
"name": "stdout",
203+
"output_type": "stream",
204+
"text": [
205+
"One part is not true\n"
206+
]
207+
}
208+
],
209+
"source": []
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": 7,
214+
"metadata": {},
215+
"outputs": [
216+
{
217+
"name": "stdout",
218+
"output_type": "stream",
219+
"text": [
220+
"At least one test is true\n"
221+
]
222+
}
223+
],
224+
"source": []
225+
},
226+
{
227+
"cell_type": "markdown",
228+
"metadata": {},
229+
"source": [
230+
"These are just simple examples, but concepts that can be quite handy."
231+
]
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"metadata": {},
236+
"source": [
237+
"#### Another familiar conditional scenario\n",
238+
"\n",
239+
"Again, making decisions based on multiple conditions is something we regularly do.\n",
240+
"Imagine that we consider not only the rain, but also whether or not it is windy.\n",
241+
"If it is windy and raining, we’ll just stay home.\n",
242+
"Otherwise, we need appropriate clothing to go out.\n",
243+
"We can again handle this kind of decision with Python.\n",
244+
"\n",
245+
"```python\n",
246+
"weather = 'Rain'\n",
247+
"wind = 'Windy'\n",
248+
"\n",
249+
"if (weather == 'Rain') and (wind == 'Windy'):\n",
250+
" print('Just stay home')\n",
251+
"elif weather == 'Rain':\n",
252+
" print('Wear a raincoat')\n",
253+
"else:\n",
254+
" print('No raincoat needed')\n",
255+
"\n",
256+
"Just stay home\n",
257+
"```\n",
258+
"\n",
259+
"As you can see, we better just stay home if it is windy and raining."
260+
]
261+
}
262+
],
263+
"metadata": {
264+
"kernelspec": {
265+
"display_name": "Python 3",
266+
"language": "python",
267+
"name": "python3"
268+
},
269+
"language_info": {
270+
"codemirror_mode": {
271+
"name": "ipython",
272+
"version": 3
273+
},
274+
"file_extension": ".py",
275+
"mimetype": "text/x-python",
276+
"name": "python",
277+
"nbconvert_exporter": "python",
278+
"pygments_lexer": "ipython3",
279+
"version": "3.6.4"
280+
}
281+
},
282+
"nbformat": 4,
283+
"nbformat_minor": 2
284+
}

0 commit comments

Comments
 (0)