Learn python the hard way (Day 4)
10209 ワード
After logic, loop, function, Now I ' ll introduce data structor about python, such as string, list, tuple, dictionary. Yes these are very useful and basically Let's go!
string
str= ' '
list: listor= [ ]
dictionary: dic = { index: element }
ok, dictionary is very strong, and we ' ll use it to deal with our complex passion.
string
str= ' '
[root@Alicia python_zed]# vi ex25.py
1 # more and more and more exercise
2
3 def break_words(stuff):
6
7 words = stuff.split(' ')
8
9 return words
10
11 def sort_words(words):
12
13 """sort the words"""
14
15 return sorted(words)
16
17 def print_first_word(words):
18
19 "Prints the first word after popping it off."
20
21 word = words.pop(0)
22
23 print word
24
25 def print_last_word(words):
26
27 """Prints the last word after popping it off."""
28
29 word = words.pop(-1)
30
31 print word
32
33 def sort_sentence(sentence):
34
35 """Takes in a full sentence and returns the sorted words."""
36
37 words = break_words(sentence)
38
39 return sort_words(words)
40
41 def print_first_and_last(sentence):
42
43 """prints the first and last words of the sentence."""
44
45 words = break_words(sentence)
46
47 print_first_word(words)
48
49 print_last_word(words)
50
51 def print_first_and_last_sorted(sentence):
52
53 """sorts the words the print the first and last words"""
54
55 words = sort_sentence(sentence)
56
57 print_first_word(words)
58
59 print_last_word(words)
[root@alicia python_zed]# vi ex26.py
1 import ex25 # import module ex25, have no .py, only name
2
3 def break_words(stuff):
4 """This function will break up words for us."""
5 words = stuff.split(' ') # split string by ' ' to every words in the list
6 return words
7
8 def sort_words(words):
9 """Sorts the words."""
10 return sorted(words)
11
12 def print_first_word(words):
13 """Prints the first word after popping it off."""
14 word = words.pop(0) # pop the first word in the queue
15 print word
16
17 def print_last_word(words):
18 """Prints the last word after popping it off."""
19 word = words.pop(-1) # the last word in the queue
20 print word
21
22 def sort_sentence(sentence):
23 """Takes in a full sentence and returns the sorted words."""
24 words = break_words(sentence)
25 return sort_words(words)
26
27 def print_first_and_last(sentence):
28 """Prints the first and last words of the sentence."""
29 words = break_words(sentence)
30 print_first_word(words)
31 print_last_word(words)
32
33 def print_first_and_last_sorted(sentence):
34 """Sorts the words then prints the first and last one."""
35 words = sort_sentence(sentence)
36 print_first_word(words)
37 print_last_word(words)
38
39
40 print "Let's practice everything."
42
43 poem = """
44 \tThe lovely world
45 with logic so firmly planted
46 cannot discern
the needs of love
47 nor comprehend passion from intuition
50 """
51
52
53 print "--------------"
54 print poem
55 print "--------------"
56
57 five = 10 - 2 + 3 - 5
58 print "This should be five: %s" % five
59
60 def secret_formula(started):
61 jelly_beans = started * 500
62 jars = jelly_beans / 1000
63 crates = jars / 100
64 return jelly_beans, jars, crates
65
66
67 start_point = 10000
68 beans, jars, crates = secret_formula(start_point) #tuple
69
70 print "With a starting point of: %d" % start_point
71 print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)
72
73 start_point = start_point / 10
74
75 print "We can also do that this way:"
76 print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)
77
78
79 sentence = "All god\tthings come to those who weight."
80
81 words = ex25.break_words(sentence) # call func in ex25 module
82 sorted_words = ex25.sort_words(words)
83
84 print_first_word(words) # call func in the local
85 print_last_word(words)
86 print_first_word(sorted_words)
87 print_last_word(sorted_words)
88 sorted_words = ex25.sort_sentence(sentence)
89 print sorted_words
90
91 print_first_and_last(sentence)
92
93 print_first_and_last_sorted(sentence)
----------------------------------------------------------------
[root@alicia python_zed]# python ex26.py
Let's practice everything.
You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion
where there is none.
--------------
This should be five: 6
With a starting point of: 10000
We'd have 5000000 jeans, 5000 jars, and 50 crates.
We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crabapples.
All
weight.
All
who
['All', 'come', 'god\tthings', 'those', 'to', 'weight.', 'who']
All
weight.
All
who
list: listor= [ ]
[root@Alicia python_zed]# vi ex39.py
1 # list operation
2
3 ten_things = "Apple Oranges Crows Telephone Light Sugar"
4
5 print "Wait there's not 10 things in that list, let's fix them"
6
7 stuff = ten_things.split(' ')
8
9 more_stuff = ["Day", "Neight","Song", "Frisbee", "Corn", "Banana", "Girl","Boy"]
10
11 while len(stuff)!=10:
12 next_one = more_stuff.pop() # one pop the last one in the list
13 print "adding..,", next_one
14 stuff.append(next_one) # add one into the list
15 print "There's %d items now." %len(stuff)
16
17
18 print "There we go:", stuff
19
20 print "Let's do some thing with stuff."
21
22 print stuff[1] # the second element in the list
23
24 print stuff[-1] # the last element in the list
25
26 print ' '.join(stuff) # use white_space to join elements of stuff
27
28 print '#'.join(stuff[3:5]) # use # to join stuff[3] and stuff[4]
29
----------------------------------------------------------
[root@Alicia python_zed]# python ex39.py
Wait there's not 10 things in that list, let's fix them
adding.., Boy
There's 7 items now.
adding.., Girl
There's 8 items now.
adding.., Banana
There's 9 items now.
adding.., Corn
There's 10 items now.
There we go: ['Apple', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']
Let's do some thing with stuff.
Oranges
Corn
Apple Oranges Crows Telephone Light Sugar Boy Girl Banana Corn
Telephone#Light
dictionary: dic = { index: element }
[root@Alicia python_zed]# vi ex40.py
1 # dictionary
2
3 cities = {'CA':'San Francisco', 'MI':'Detroit','FL':'Jacksonville'}
4
5 cities['NY']= 'New York' # add one element
6
7 cities['OR']= 'Portland' # add the other element
8
9 def find_city(themap, state):
10 """if state is in the dictionary's index,retrun his value"""
11
12 if state in themap:
13 return themap[state]
14 else:
15 return "Not found"
16
17 # ok , pay attension
18
19 cities['_find']= find_city # create a element which type is function
20
21 while True:
22
23 print "State?(Enter to quit)",
24
25 state = raw_input(">")
26
27 if not state: break
28
29 # this line is the most important ever! study!
30
31 city_found = cities['_find'](cities,state) # city_found = find_city(cities, state)
32
33 print city_found
34
------------------------------------------------------------
[root@Alicia python_zed]# python ex40.py
State?(Enter to quit) >CA
San Francisco
State?(Enter to quit) >O
Not found
State?(Enter to quit) >OR
Portland
State?(Enter to quit) >
ok, dictionary is very strong, and we ' ll use it to deal with our complex passion.