Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3a6ec17

Browse files
Update Lesson 2
1 parent abb396e commit 3a6ec17

File tree

1 file changed

+347
-0
lines changed

1 file changed

+347
-0
lines changed
Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"In thhis lesson I learn about data types and operators"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"Quiz: Arithmetic Operators\n",
15+
"\n",
16+
"Quiz 1: Electricity bills¶\n",
17+
"\n",
18+
"My electricity bills for the last three months have been 23,ドル 32ドル and 64ドル. What is my average monthly electricity bill over the three month period?\n",
19+
"\n",
20+
"Write an expression to calculate the mean,\n"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 1,
26+
"metadata": {},
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"39.666666666666664"
32+
]
33+
},
34+
"execution_count": 1,
35+
"metadata": {},
36+
"output_type": "execute_result"
37+
}
38+
],
39+
"source": [
40+
"avg = (23+32+64)/3 \n",
41+
"avg"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"Quiz 2: Tiles\n",
49+
"\n",
50+
"In this quiz you're going to do some calculations for a tiler. Two parts of a floor need tiling. One part is 9 tiles wide by 7 tiles long, the other is 5 tiles wide by 7 tiles long. Tiles come in packages of 6.\n",
51+
"\n",
52+
" How many tiles are needed?\n",
53+
" You buy 17 packages of tiles containing 6 tiles each. How many tiles will be left over?"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 3,
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"data": {
63+
"text/plain": [
64+
"4"
65+
]
66+
},
67+
"execution_count": 3,
68+
"metadata": {},
69+
"output_type": "execute_result"
70+
}
71+
],
72+
"source": [
73+
"tiles = ((9*7)+(5*7))\n",
74+
"tiles\n",
75+
"leftover_tiles = (17*6) - (tiles)\n",
76+
"leftover_tiles"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"Quiz: Variables and Assignment Operators\n",
84+
"Quiz: Assign and Modify Variables\n",
85+
"\n",
86+
"Now it's your turn to work with variables. The comments in this quiz (the lines that begin with #) have instructions for creating and modifying variables. After each comment write a line of code that implements the instruction.\n",
87+
"\n",
88+
"Note that this code uses scientific notation to define large numbers. 4.445e8 is equal to 4.445 *10* * 8 which is equal to 444500000.0."
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 7,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"449000000.0\n",
101+
"471450000.0\n",
102+
"447877500.0\n",
103+
"447627500.0\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"reservoir_volume = 4.445e8\n",
109+
"rainfall = 5e6\n",
110+
"\n",
111+
"# Use the multiplication assignment operator to decrease \n",
112+
"# the rainfall variable by 10% to account for runoff\n",
113+
"rainfall *= 0.9\n",
114+
"\n",
115+
"reservoir_volume += rainfall\n",
116+
"print(reservoir_volume)\n",
117+
"\n",
118+
"# Use the multiplication assignment operator to increase \n",
119+
"# reservoir_volume by 5% to account for stormwater that \n",
120+
"# flows into the reservoir in the days following the storm\n",
121+
"reservoir_volume *= 1.05\n",
122+
"print(reservoir_volume)\n",
123+
"\n",
124+
"# Use the multiplication assignment operator to decrease \n",
125+
"# reservoir_volume by 5% to account for evaporation\n",
126+
"reservoir_volume *= 0.95\n",
127+
"print(reservoir_volume)\n",
128+
"\n",
129+
"# Use the subtraction assignment operator to subtract \n",
130+
"# 2.5e5 cubic metres from reservoir_volume to account \n",
131+
"# for water that's piped to arid regions.\n",
132+
"\n",
133+
"reservoir_volume -= 2.5e5\n",
134+
"print(reservoir_volume)\n"
135+
]
136+
},
137+
{
138+
"cell_type": "markdown",
139+
"metadata": {},
140+
"source": [
141+
"Quiz: Booleans, Comparison Operators, and Logical Operators\n",
142+
"Quiz: Which is denser, Rio or San Francisco?\n",
143+
"\n",
144+
"Try comparison operators in this quiz! This code calculates the population densities of Rio de Janeiro and San Francisco.\n",
145+
"\n",
146+
"Write code to compare these densities. Is the population of San Francisco more dense than that of Rio de Janeiro?"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": 8,
152+
"metadata": {},
153+
"outputs": [
154+
{
155+
"name": "stdout",
156+
"output_type": "stream",
157+
"text": [
158+
"False\n"
159+
]
160+
}
161+
],
162+
"source": [
163+
"sf_population, sf_area = 864816, 231.89\n",
164+
"rio_population, rio_area = 6453682, 486.5\n",
165+
"\n",
166+
"san_francisco_pop_density = sf_population/sf_area\n",
167+
"rio_de_janeiro_pop_density = rio_population/rio_area\n",
168+
"\n",
169+
"# Write code that gives a boolean value True if San Francisco is denser than Rio, and False otherwise\n",
170+
"comparision_result = san_francisco_pop_density > rio_de_janeiro_pop_density#replace `None` with your code\n",
171+
"print(comparision_result)"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"Quiz: Fix the Quote¶\n",
179+
"\n",
180+
"The line of code in the following quiz will cause a SyntaxError, thanks to the misuse of quotation marks. First run it with Test Run to view the error message. Then resolve the problem so that the quote (from Henry Ford is correctly assigned to the variable ford_quote.\n"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 9,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"ford_quote = 'Whether you think you can, or you think you can\\'t--you\\'re right.'"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"metadata": {},
195+
"source": [
196+
"Quiz: Write a Server Log Message¶\n",
197+
"\n",
198+
"In this programming quiz, you’re going to use what you’ve learned about strings to write a logging message for a server.\n",
199+
"\n",
200+
"You’ll be provided with example data for a user, the time of their visit and the site they accessed. You should use the variables provided and the techniques you’ve learned to print a log message like this one (with the username, url, and timestamp replaced with values from the appropriate variables):\n",
201+
"\n",
202+
"Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20.\n",
203+
"\n",
204+
"Use the Test Run button to see your results as you work on coding this piece by piece."
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": 11,
210+
"metadata": {},
211+
"outputs": [
212+
{
213+
"name": "stdout",
214+
"output_type": "stream",
215+
"text": [
216+
"Kinari accessed the site http://petshop.com/pets/mammals/cats at 04:50.\n"
217+
]
218+
}
219+
],
220+
"source": [
221+
"username = \"Kinari\"\n",
222+
"timestamp = \"04:50\"\n",
223+
"url = \"http://petshop.com/pets/mammals/cats\"\n",
224+
"\n",
225+
"# write a log message using the variables above.\n",
226+
"# The message should have the same format as this one:\n",
227+
"# \"Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20.\"\n",
228+
"\n",
229+
"message = username +\" accessed the site \"+ url +\" at \"+timestamp +\".\" #replace \"\" with your code\n",
230+
"print(message)"
231+
]
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"metadata": {},
236+
"source": [
237+
"Quiz: len()\n",
238+
"\n",
239+
"Use string concatenation and the len() function to find the length of a certain movie star's actual full name. Store that length in the name_length variable. Don't forget that there are spaces in between the different parts of a name!\n"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": 12,
245+
"metadata": {},
246+
"outputs": [
247+
{
248+
"name": "stdout",
249+
"output_type": "stream",
250+
"text": [
251+
"True\n"
252+
]
253+
}
254+
],
255+
"source": [
256+
"given_name = \"William\"\n",
257+
"middle_names = \"Bradley\"\n",
258+
"family_name = \"Pitt\"\n",
259+
"# Todo: calculate how long this name is\n",
260+
"name_length = len(given_name+\" \"+middle_names+\" \"+family_name) #replace `None` with your code\n",
261+
"\n",
262+
"# Now we check to make sure that the name fits within the driving license character limit\n",
263+
"# Uncomment the code below. You don't need to make changes to the code.\n",
264+
"\n",
265+
"driving_license_character_limit = 28\n",
266+
"print(name_length <= driving_license_character_limit)"
267+
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"metadata": {},
272+
"source": [
273+
"Quiz: String Methods Coding Practice\n",
274+
"\n",
275+
"Below, we have a string variable that contains the first verse of the poem, If by Rudyard Kipling. Remember, \\n is a special sequence of characters that causes a line break (a new line).\n",
276+
"\n",
277+
"verse = \"If you can keep your head when all about you\\n Are losing theirs and blaming it on you,\\nIf you can trust yourself when all men doubt you,\\n But make allowance for their doubting too;\\nIf you can wait and not be tired by waiting,\\n Or being lied about, don’t deal in lies,\\nOr being hated, don’t give way to hating,\\n And yet don’t look too good, nor talk too wise:\"\n",
278+
"\n",
279+
"Use the workspace below to answer the following questions about verse and check your output in the quiz at the bottom of this page.\n",
280+
"\n",
281+
" What is the length of the string variable verse?\n",
282+
" What is the index of the first occurrence of the word 'and' in verse?\n",
283+
" What is the index of the last occurrence of the word 'you' in verse?\n",
284+
" What is the count of occurrences of the word 'you' in the verse?\n",
285+
"\n",
286+
"You will need to refer to Python's string methods documentation."
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": 1,
292+
"metadata": {},
293+
"outputs": [
294+
{
295+
"name": "stdout",
296+
"output_type": "stream",
297+
"text": [
298+
"If you can keep your head when all about you\n",
299+
" Are losing theirs and blaming it on you,\n",
300+
"If you can trust yourself when all men doubt you,\n",
301+
" But make allowance for their doubting too;\n",
302+
"If you can wait and not be tired by waiting,\n",
303+
" Or being lied about, don’t deal in lies,\n",
304+
"Or being hated, don’t give way to hating,\n",
305+
" And yet don’t look too good, nor talk too wise: \n",
306+
"\n",
307+
"Verse has a length of 362 characters.\n",
308+
"The first occurence of the word 'and' occurs at the 65th index.\n",
309+
"The last occurence of the word 'you' occurs at the 186th index.\n",
310+
"The word 'you' occurs 8 times in the verse.\n"
311+
]
312+
}
313+
],
314+
"source": [
315+
"verse = \"If you can keep your head when all about you\\n Are losing theirs and blaming it on you,\\nIf you can trust yourself when all men doubt you,\\n But make allowance for their doubting too;\\nIf you can wait and not be tired by waiting,\\n Or being lied about, don’t deal in lies,\\nOr being hated, don’t give way to hating,\\n And yet don’t look too good, nor talk too wise:\"\n",
316+
"print(verse, \"\\n\")\n",
317+
"\n",
318+
"print(\"Verse has a length of {} characters.\".format(len(verse)))\n",
319+
"print(\"The first occurence of the word 'and' occurs at the {}th index.\".format(verse.find('and')))\n",
320+
"print(\"The last occurence of the word 'you' occurs at the {}th index.\".format(verse.rfind('you')))\n",
321+
"print(\"The word 'you' occurs {} times in the verse.\".format(verse.count('you')))"
322+
]
323+
}
324+
],
325+
"metadata": {
326+
"kernelspec": {
327+
"display_name": "Python 3",
328+
"language": "python",
329+
"name": "python3"
330+
},
331+
"language_info": {
332+
"codemirror_mode": {
333+
"name": "ipython",
334+
"version": 3
335+
},
336+
"file_extension": ".py",
337+
"mimetype": "text/x-python",
338+
"name": "python",
339+
"nbconvert_exporter": "python",
340+
"pygments_lexer": "ipython3",
341+
"version": "3.11.0"
342+
},
343+
"orig_nbformat": 4
344+
},
345+
"nbformat": 4,
346+
"nbformat_minor": 2
347+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /