Skip to content

Commit 0644711

Browse files
committed
Updates 20170628.md
Auto commit by GitBook Editor
1 parent 5190477 commit 0644711

File tree

4 files changed

+363
-13
lines changed

4 files changed

+363
-13
lines changed

20170627.md

Lines changed: 203 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ print(list(range(9, -1, -1)))
3636

3737
\[9, 8, 7, 6, 5, 4, 3, 2, 1, 0\]
3838

39-
40-
4139
Make the loop print the numbers from 0 to 19 instead of 0 to 9.
4240

4341
```python
@@ -93,12 +91,10 @@ hobbies = []
9391
for x in range(0,3):
9492
newhobbies = raw_input("your new hobbies")
9593
hobbies.append(str(newhobbies))
96-
94+
9795
print hobbies
9896
```
9997

100-
101-
10298
```python
10399
n = [[1, 2], [4, 5, 6, 7]]
104100
def flatten(lists):
@@ -191,8 +187,6 @@ else:
191187
print "You win!"
192188
```
193189

194-
195-
196190
猜數字遊戲
197191

198192
```python
@@ -211,8 +205,6 @@ else:
211205
print "You lose."
212206
```
213207

214-
215-
216208
取代
217209

218210
```python
@@ -228,8 +220,6 @@ print s
228220

229221
X bird in the hand...
230222

231-
232-
233223
The`,`character after our
234224

235225
`print`statement means that our next`print`
@@ -249,6 +239,208 @@ print
249239

250240
X bird in the hand...
251241

242+
---
243+
244+
### For/else
245+
246+
Just like with`while`,`for`loops may have an`else`associated with them.
247+
248+
```python
249+
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
250+
print 'You have...'
251+
for f in fruits:
252+
if f == 'tomato':
253+
print 'A tomato is not a fruit!'
254+
break #到tomato就會停止
255+
print 'A', f
256+
else:
257+
print 'A fine selection of fruits!'
258+
```
259+
260+
You have...
261+
262+
A banana
263+
264+
A apple
265+
266+
A orange
267+
268+
A tomato is not a fruit!
269+
270+
```python
271+
#沒有break的版本
272+
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
273+
print 'You have...'
274+
for f in fruits:
275+
if f == 'tomato':
276+
print 'A tomato is not a fruit!'
277+
print 'A', f
278+
else:
279+
print 'A fine selection of fruits!'
280+
```
281+
282+
You have...
283+
284+
A banana
285+
286+
A apple
287+
288+
A orange
289+
290+
A tomato is not a fruit!
291+
292+
A tomato
293+
294+
A pear
295+
296+
A grape
297+
298+
A fine selection of fruits!
299+
300+
---
301+
302+
Looping over a dictionary
303+
304+
```python
305+
d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}
306+
for key in d:
307+
# Your code here!
308+
#if d[key] == 'berry':
309+
print key, d[key]
310+
```
311+
312+
a apple
313+
314+
b berry
315+
316+
c cherry
317+
318+
---
319+
320+
### 內建函數enumerate\(\)
321+
322+
`enumerate`works by supplying a corresponding index to each element in the list that you pass it. Each time you go through the loop,
323+
324+
`index`will be one greater, and`item`will be the next item in the sequence. It's very similar to using a normal`for`loop with a list, except this gives us an easy way to count how many items we've seen so far
325+
326+
```python
327+
choices = ['pizza', 'pasta', 'salad', 'nachos']
328+
print 'Your choices are:'
329+
for index, item in enumerate(choices):
330+
print index+1, item
331+
# index從0開始所以+1
332+
```
333+
334+
Your choices are:
335+
336+
1 pizza
337+
338+
2 pasta
339+
340+
3 salad
341+
342+
4 nachos
343+
344+
---
345+
346+
### 內建函數zip\(\)
347+
348+
`zip`will create pairs of elements when passed two lists, and will stop at the end of the shorter list.`zip`can handle three or more lists as well!
349+
350+
```python
351+
#zip()用來混合list_a & list_b
352+
list_a = [3, 9, 17, 15, 19]
353+
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
354+
for a, b in zip(list_a, list_b):
355+
# Add your code here!
356+
#print a,b 列出ab
357+
if a > b: #a,b比大小
358+
print a
359+
else:
360+
print b
361+
```
362+
363+
3
364+
365+
9
366+
367+
17
368+
369+
15
370+
371+
30
372+
373+
---
374+
375+
### 內建函數sum\(\) , float\(\)
376+
377+
```py
378+
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
379+
def grades_sum(scores):
380+
total = sum(scores)
381+
#sum 自動總和
382+
return total
383+
384+
def grades_average(scores):
385+
return grades_sum(scores)/float(len(scores))
386+
#float轉換成浮點數
387+
#len()長度(list數量)
388+
print grades_average(grades)
389+
```
390+
391+
---
392+
393+
Standard Deviation:
394+
395+
The standard deviation is the square root of the variance.
396+
397+
```python
398+
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
399+
400+
# all the grades
401+
def print_grades(grades):
402+
for grade in grades:
403+
print grade
404+
# sum of grades
405+
def grades_sum(grades):
406+
total = 0
407+
for grade in grades:
408+
total += grade
409+
return total
410+
# average of grades
411+
def grades_average(grades):
412+
sum_of_grades = grades_sum(grades)
413+
average = sum_of_grades / float(len(grades))
414+
return average
415+
# variance of grades
416+
def grades_variance(scores):
417+
average = grades_average(scores)
418+
variance = 0
419+
for score in scores:
420+
variance += (average - score) ** 2
421+
result = variance/float(len(scores))
422+
return result
423+
# deviation of grades
424+
def grades_std_deviation(variance):
425+
return variance ** 0.5 #開根號
426+
427+
variance = grades_std_deviation(grades_variance(grades))
428+
#取得variance的值再計算deviation
429+
print (variance)
430+
```
431+
432+
433+
434+
435+
436+
437+
438+
439+
440+
441+
442+
443+
252444

253445

254446

0 commit comments

Comments
 (0)