Skip to content

Commit 0fba756

Browse files
authored
Add files via upload
1 parent 721b018 commit 0fba756

File tree

7 files changed

+134326
-0
lines changed

7 files changed

+134326
-0
lines changed

Chapter 08: Lists/ch08_hw1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fname = input('Enter file name: ')
2+
fhand = open(fname)
3+
lst = list()
4+
for line in fhand:
5+
line = line.rstrip()
6+
7+
words = line.split()
8+
9+
for wds in words:
10+
if wds not in lst:
11+
lst.append(wds)
12+
lst.sort()
13+
print(lst)
14+
15+
# # fhand = input('enter the file: ')
16+
# fhand = open('romeo.txt')
17+
# wds = list()
18+
# for line in fhand:
19+
# linermv = line.rstrip() #remove the newline between each line
20+
# linesplit = linermv.split()
21+
# print('line', linesplit)
22+
# for wd in linesplit:
23+
# if wd not in wds:
24+
# wds.append(wd)
25+
# print("wd:", wds)

Chapter 08: Lists/ch08_hw2.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# ###################beter version:##########################
2+
3+
# fhand = input('enter the file: ')
4+
fhand = open('mbox_short.txt')
5+
email = list()
6+
count = 0
7+
for line in fhand:
8+
linermv = line.rstrip() # type:str
9+
linesplit = linermv.split() # type:list
10+
if len(linesplit) > 3 and linesplit[0] == 'From':
11+
print(linesplit[1])
12+
count = count + 1
13+
print('There were', count, 'lines in the file with From as the first word')
14+
15+
# don't use str.startswith('From'), because it will select both From and From:
16+
17+
18+
# fname = input('Enter a file name: ')
19+
# fh = open(fname)
20+
# lst = list()
21+
# count = 0
22+
# for i in fh:
23+
# if i.startswith('From'):
24+
# i.rstrip()
25+
# words = i.split()
26+
# # if i.startswith('From: '): continue
27+
# if words[0] != 'From' : continue
28+
# count = count + 1
29+
# print(words[1])
30+
# print('There were', count, 'lines in the file with From as the first word')
31+
32+

Chapter 08: Lists/ch08_list

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
print('chapter 8 List')
2+
# Algorithm: "A set of rules or steps used to solve a problems"
3+
# Data Structure: "A particular way of organizing data in a computers"
4+
print('1. A list is a kind of collection ')
5+
# e.g. friends = ['Joe', 'Glenn', 'Sally']
6+
# []: list constent, and seperate by commas ","
7+
# A list element can be any python object, even another list
8+
# list can be empty
9+
print('List contants example')
10+
print([1, 24, 9]) #one type "int" in the list
11+
print([12, 'red', [1, 5]]) # mixed three types
12+
print('#####################################################')
13+
print('2. List and definaite loops')
14+
friends = ['Joe', 'Glenn', 'Sally']
15+
for friend in friends:
16+
print('Hello: ', friend)
17+
print('Done!')
18+
print('#####################################################')
19+
print('3. Looking inside lists')
20+
# we can get any single element in a list by using index: start from 0
21+
friends = ['Joe', 'Glenn', 'Sally']
22+
print(friends[2])
23+
print('#####################################################')
24+
print('4. lists are mutable: can be change')
25+
# strings are "immutable" <-- we cannot change the content of a string,
26+
# we must makea new string to make any changes
27+
fruit = 'Banana'
28+
print(fruit)
29+
x = fruit.lower() # <--it makes a copy of the string
30+
print(x)
31+
# Lists are "mutable" <-- we can change a element of a list by its index
32+
lotto = [12, 26, 41, 28]
33+
print(lotto)
34+
lotto[2] = 28
35+
print(lotto)
36+
print('#####################################################')
37+
print('5. The length of the list')
38+
# The "leng()" function takes a list as a parameter
39+
# and returns the numbers of elements in the list
40+
# !! len() tells the numbers of elements of any set or sequence
41+
greet ='hello Bob'
42+
print(len(greet))
43+
44+
x = [1, 2, 'Joe', [4, 6]]
45+
print(len(x))
46+
print('#####################################################')
47+
print('6. Using the "Range" Function')
48+
# The range function returns a list of numbers that range
49+
# from zero to one less than the parameter: 0 to (parameter-1)
50+
# we can construct an index loop using "for and an integer iterator"
51+
num4 = range(4)
52+
for i in num4:
53+
print(i, end=', ')
54+
print('Use rang in a loop')
55+
friends = ['Joe', 'Glenn', 'Sally']
56+
print(len(friends))
57+
for i in range(len(friends)):
58+
friend = friends[i]
59+
print('Hello: ', friend) # <-- within for loop!
60+
print('####################part2############################')
61+
print('7. Concatenating lists using "+"')
62+
a = [1, 2, 3]
63+
b = [4, 5, 6]
64+
c = a + b
65+
print(a, '\n', b, '\n', c, sep='')
66+
#sep -- 用来间隔多个对象,默认值是一个空格。
67+
print('####################part2############################')
68+
print('8. Slicing lists using ":"')
69+
print('Reminder: the second number is "up to but not inclusing"')
70+
t = [9, 41, 12, 3 ,74, 15]
71+
print(t)
72+
print(t[1:3])
73+
print(t[:4])
74+
print(t[3:])
75+
print('####################part2############################')
76+
print('9. List Methods')
77+
# append: adds a new element to the end of a list
78+
t = ['a', 'b', 'c', 'd', 'e', 'f']
79+
t[1:3] = ['x', 'y']
80+
print(t)
81+
# ['a', 'x', 'y', 'd', 'e', 'f']
82+
stuff= list()
83+
stuff.append('book')
84+
stuff.append(99)
85+
print(stuff)
86+
stuff.append([3, 4])
87+
print(stuff)
88+
89+
# extend: takes a list as an argument and appends all of the elements
90+
# >>> t1 = ['a', 'b', 'c']
91+
# >>> t2 = ['d', 'e']
92+
# >>> t1.extend(t2)
93+
# >>> print(t1)
94+
# ['a', 'b', 'c', 'd', 'e']
95+
# sort :arranges the elements of the list from low to high:
96+
# >>> t = ['d', 'c', 'e', 'b', 'a']
97+
# >>> t.sort()
98+
# >>> print(t)
99+
# ['a', 'b', 'c', 'd', 'e']
100+
# pop: delete element by its index, modifies the list and returns the element that was removed
101+
# >>> t = ['a', 'b', 'c']
102+
# >>> x = t.pop(1)
103+
# >>> print(t)
104+
# ['a', 'c']
105+
# >>> print(x)
106+
# b
107+
# del: delete element by its index
108+
# >>> t = ['a', 'b', 'c']
109+
# >>> del t[1]
110+
# >>> print(t)
111+
# ['a', 'c']
112+
# remove: delete by name
113+
# >>> t = ['a', 'b', 'c']
114+
# >>> t.remove('b')
115+
# >>> print(t)
116+
# ['a', 'c']
117+
print('####################part2############################')
118+
print('10. Is something in the list?')
119+
# Pythonprovies two operators that check if an item is in a list
120+
# "in" or "not in" & "True" or "false"
121+
some = [1, 9, 21, 10, 16]
122+
print('9 in some: ')
123+
print(9 in some)
124+
print('15 in some: ')
125+
print(15 in some)
126+
print('sort list:')
127+
friends = ['Joe', 'Glenn', 'Sally']
128+
print(friends)
129+
friends. sort()
130+
print(friends)
131+
# cannot use print(friends.sort())!!!! output: None
132+
print('simpler than loop')
133+
some = [1, 9, 21, 10, 16]
134+
print(len(some))
135+
print(max(some))
136+
# no use of some.max()!!!!!!
137+
print(min(some))
138+
print(sum(some))
139+
print('compare loop and list:')
140+
# ##########loop#############
141+
total = 0
142+
count = 0
143+
while True:
144+
inp = input('Enter a number:')
145+
if inp =='none': break
146+
val = float(inp)
147+
total = total + val
148+
count = count + 1
149+
print('Average: ', total/count)
150+
# ##########list#############
151+
numlist = list()
152+
while True:
153+
inp = input('Enter a number:')
154+
if inp =='none': break
155+
val = float(inp)
156+
numlist.append(val)
157+
print('Ave: ', sum(numlist)/len(numlist))
158+
# Difference: loop will keep memory of one number,
159+
# while list keep all numbers in memory before calculation
160+
print('####################part3############################')
161+
print('11. String and list')
162+
print('imortant example:')
163+
abc = 'With three words'
164+
stuff = abc.split()
165+
print(stuff)
166+
print(len(stuff))
167+
for i in stuff:
168+
print(i)
169+
print('delimiter: specifies which characters to use as word boundaries'
170+
'with no specify of delimiter, multiple spaces treated as one delimiter')
171+
line1 = 'A lot of spaces'
172+
etc1= line1.split()
173+
print(etc1)
174+
line2 = '1;2;3'
175+
etc2 = line2.split(';')
176+
print(etc2)
177+
print(len(etc2))
178+
print('#####################################################')
179+
print('12. parsing lines')
180+
fhand = open('mbox_short.txt')
181+
for line in fhand:
182+
line = line.rstrip()
183+
if not line.startswith('From '): continue
184+
words = line.split()
185+
print(words[2])
186+
print('#####################################################')
187+
print('13. double split pattern')
188+
189+
fhand = open('mbox_short.txt')
190+
for line in fhand:
191+
line = line.rstrip()
192+
if not line.startswith('From '): continue
193+
words = line.split()
194+
email = words[1]
195+
pieces = email.split('@')
196+
print(pieces)
197+
198+

Chapter 08: Lists/ch08_quiz

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
###
2+
Question 1
3+
How are "collection" variables different from normal variables?
4+
Collection variables can only store a single value
5+
Collection variables can store multiple values in a single variable
6+
Collection variables merge streams of output into a single stream
7+
Collection variables pull multiple network documents together
8+
9+
Answer: Collection variables can store multiple values in a single variable
10+
11+
Question 2
12+
What are the Python keywords used to construct a loop to iterate through a list?
13+
for / in
14+
foreach / in
15+
try / except
16+
def / return
17+
18+
Answer: for/in
19+
20+
Question 3
21+
For the following list, how would you print out 'Sally'?
22+
friends = [ 'Joseph', 'Glenn', 'Sally' ]
23+
print friends[2]
24+
print friends[3]
25+
print friends[2:1]
26+
print friends['Sally']
27+
28+
Answer: print friends[2]
29+
30+
Question 4
31+
What would the following Python code print out?
32+
fruit = 'Banana'
33+
fruit[0] = 'b'
34+
print fruit
35+
Nothing would print - the program fails with a traceback
36+
b
37+
[0]
38+
banana
39+
Banana
40+
B
41+
42+
Answer: othing would print - the program fails with a traceback
43+
44+
Question 5
45+
Which of the following Python statements would print out the length of a list stored in the variable data?
46+
print data.length()
47+
print len(data)
48+
print data.length
49+
print strlen(data)
50+
print length(data)
51+
print data.Len
52+
53+
Answer: print len(data)
54+
55+
Question 6
56+
What type of data is produced when you call the range() function?
57+
x = range(5)
58+
A boolean (true/false) value
59+
A list of words
60+
A list of integers
61+
A string
62+
A list of characters
63+
64+
Answer: A list of integers
65+
66+
Question 7
67+
What does the following Python code print out?
68+
a = [1, 2, 3]
69+
b = [4, 5, 6]
70+
c = a + b
71+
print len(c)
72+
[1, 2, 3, 4, 5, 6]
73+
[1, 2, 3]
74+
21
75+
[4, 5, 6]
76+
15
77+
6
78+
79+
Answer: 6
80+
81+
Question 8
82+
Which of the following slicing operations will produce the list [12, 3]?
83+
t = [9, 41, 12, 3, 74, 15]
84+
t[12:3]
85+
t[1:3]
86+
t[2:2]
87+
t[:]
88+
t[2:4]
89+
90+
Answer: t[2:4]
91+
92+
Question 9
93+
What list method adds a new item to the end of an existing list?
94+
push()
95+
index()
96+
pop()
97+
forward()
98+
add()
99+
append()
100+
101+
Answer: append()
102+
103+
Question 10
104+
What will the following Python code print out?
105+
friends = [ 'Joseph', 'Glenn', 'Sally' ]
106+
friends.sort()
107+
print friends[0]
108+
Joseph
109+
Sally
110+
Glenn
111+
friends
112+
113+
Answer: Glenn

0 commit comments

Comments
 (0)