Skip to content

Commit b4de26a

Browse files
committed
update
1 parent e2acd53 commit b4de26a

18 files changed

+3444
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Chained Comparison Operators\n",
8+
"\n",
9+
"An interesting feature of Python is the ability to *chain* multiple comparisons to perform a more complex test. You can use these chained comparisons as a shorthand for larger Boolean Expressions.\n",
10+
"\n",
11+
"In this lecture we will learn how to chain comparison operators and we will also introduce two other important statements in python: **and** and **or**.\n",
12+
"\n",
13+
"Let's look at a few examples of using chains:"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 1,
19+
"metadata": {
20+
"collapsed": false
21+
},
22+
"outputs": [
23+
{
24+
"data": {
25+
"text/plain": [
26+
"True"
27+
]
28+
},
29+
"execution_count": 1,
30+
"metadata": {},
31+
"output_type": "execute_result"
32+
}
33+
],
34+
"source": [
35+
"1 < 2 < 3"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"The above statement check if 1 was less than 2 **and** if 2 was less than 3. We could have written this using an **and** statement in Python:"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 2,
48+
"metadata": {
49+
"collapsed": false
50+
},
51+
"outputs": [
52+
{
53+
"data": {
54+
"text/plain": [
55+
"True"
56+
]
57+
},
58+
"execution_count": 2,
59+
"metadata": {},
60+
"output_type": "execute_result"
61+
}
62+
],
63+
"source": [
64+
"1<2 and 2<3"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"The **and** is used to make sure two checks have to be true in order for the total check to be true. Let's see another example:"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 3,
77+
"metadata": {
78+
"collapsed": false
79+
},
80+
"outputs": [
81+
{
82+
"data": {
83+
"text/plain": [
84+
"True"
85+
]
86+
},
87+
"execution_count": 3,
88+
"metadata": {},
89+
"output_type": "execute_result"
90+
}
91+
],
92+
"source": [
93+
"1 < 3 > 2"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"The above checks if 3 is larger than both the other numbers, so you could use **and** to rewrite it as:"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 4,
106+
"metadata": {
107+
"collapsed": false
108+
},
109+
"outputs": [
110+
{
111+
"data": {
112+
"text/plain": [
113+
"True"
114+
]
115+
},
116+
"execution_count": 4,
117+
"metadata": {},
118+
"output_type": "execute_result"
119+
}
120+
],
121+
"source": [
122+
"1<3 and 3>2"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"Its important to note that Python is checking both instances of the comparisons. We can also use **or** to write comparisons in Python. For example:"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 5,
135+
"metadata": {
136+
"collapsed": false
137+
},
138+
"outputs": [
139+
{
140+
"data": {
141+
"text/plain": [
142+
"True"
143+
]
144+
},
145+
"execution_count": 5,
146+
"metadata": {},
147+
"output_type": "execute_result"
148+
}
149+
],
150+
"source": [
151+
"1==2 or 2<3"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"Note how it was true, this is beacuse with the **or** operator, we only need one *or* the other two be true. Let's see one more example to drive this home:"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 6,
164+
"metadata": {
165+
"collapsed": false
166+
},
167+
"outputs": [
168+
{
169+
"data": {
170+
"text/plain": [
171+
"True"
172+
]
173+
},
174+
"execution_count": 6,
175+
"metadata": {},
176+
"output_type": "execute_result"
177+
}
178+
],
179+
"source": [
180+
"1==1 or 100==1"
181+
]
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
186+
"source": [
187+
"Great! For an overview of this quick lesson: You should have a comfortable understanding of using **and** and **or** statements as well as reading chained comparison code.\n",
188+
"\n",
189+
"Go ahead and go to the quiz for this section to check your understanding!"
190+
]
191+
}
192+
],
193+
"metadata": {
194+
"kernelspec": {
195+
"display_name": "Python 2",
196+
"language": "python",
197+
"name": "python2"
198+
},
199+
"language_info": {
200+
"codemirror_mode": {
201+
"name": "ipython",
202+
"version": 2
203+
},
204+
"file_extension": ".py",
205+
"mimetype": "text/x-python",
206+
"name": "python",
207+
"nbconvert_exporter": "python",
208+
"pygments_lexer": "ipython2",
209+
"version": "2.7.10"
210+
}
211+
},
212+
"nbformat": 4,
213+
"nbformat_minor": 0
214+
}

0 commit comments

Comments
 (0)