Skip to content

Commit 8c2b545

Browse files
committed
update all
1 parent c7ffffd commit 8c2b545

File tree

4 files changed

+86
-15
lines changed

4 files changed

+86
-15
lines changed

basic/05copy&deepcopy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# python中,对象的赋值为 对象引用(内存地址)的传递
1616
list2 = list1
1717
# id() 获取变量在内存中的地址
18-
# 地址相同
18+
# list1 和 list2 地址相同,指向相同对象
1919
print(id(list1))
2020
print(list1)
2121
print([id(ele) for ele in list1])

basic/05dict & set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# key 唯一对应一个 value(1 对 1)
1010
# key 必须为不可变对象,如字符串等
1111
dict1 = {"geyang": 100, "bob": 99, "gavin": 95, 101:101}
12-
12+
#print(dict1.items())
1313
# 访问字典
1414
print(dict1["geyang"])
1515
print(dict1[100])

functional program/04lambda.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
"""
77

88
# 匿名函数
9-
# 不需要显示的定义函数,直接传入匿名函数
9+
# 不需要使用def显示的定义函数,直接传入匿名函数
1010

1111
def f(x):
1212
return x * x
1313

1414
# 匿名函数的语法为
15-
# lambda 表示匿名函数 冒号前面的x表示函数参数
15+
# lambda [arg1[, arg2, ... argn]]: expression
16+
# lambda 表示匿名函数 冒号前面表示函数参数
1617
# 匿名函数的限制 函数体只有一句语句 不用写return
1718
# 匿名函数的优点:函数没有名字 匿名函数也是一个函数对象
1819
func1 = lambda paramName: paramName * paramName
@@ -25,5 +26,6 @@ def f(x):
2526

2627
# 匿名函数作为返回值
2728
def build(x, y):
28-
return lambda: x * x + y * y
29+
return lambda x,y : x * x + y * y
30+
2931

review.py

+79-10
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,22 @@
1919
del(str1[0])
2020

2121
list1 = [1, 2, 3]
22+
list1.append(4)
23+
list1.insert(0,3)
24+
result = list1.pop()
25+
del(list1[0])
26+
list1[0] = 4
2227
print(list1[:])
2328
print(list1[0])
24-
del(list1[0])
29+
30+
set1 = {1, 2, 3, 1}
31+
print(set1)
32+
33+
dict1 = {"name": "geyang", "age": 24}
34+
dict1["height"] = 173
35+
del(dict1["height"])
36+
dict1["height"] = 174
37+
result = dict1.get("heihgt")
2538
# 添加元素
2639
# 元组不可以被修改,包括添加元素,任何方法都不可以
2740
#tuple1[3] = 4 # 'tuple' object does not support item assignment
@@ -94,9 +107,12 @@ def count(day):
94107
# 考试题2:一次性输入任意不少于5个数字(数字之间以逗号为界)保存在list中,
95108
# 对输入的数字进行排序,不准使用内置函数,封装成函数
96109
def sort_number():
97-
input_str = input()
110+
input_str = input("请输入不少于5个数字(格式:5,3,1,2)")
98111
number_list = input_str.split(",")
99112
# print(number_list)
113+
if len(number_list) < 5:
114+
print("输入数字不能少于5个")
115+
return
100116
number_list = [ int(index) for index in number_list]
101117
print("before sort:", number_list)
102118
# 冒泡排序
@@ -129,7 +145,7 @@ def slice_list():
129145
while True:
130146
if index == len(input_list):
131147
break
132-
while input_list[index] == input_list[index+1]:
148+
while (index != len(input_list) -2) and input_list[index] == input_list[index+1]:
133149
temp_list.append(input_list[index])
134150
index += 1
135151
temp_list.append(index)
@@ -141,7 +157,25 @@ def slice_list():
141157

142158
# 考试题4:写一个程序,可以任意输入文件名,打开该文件,如果文件不存在则创建该文件。
143159
# 然后输入内容,可重复输入追加到文件中。
144-
160+
def write_file():
161+
file_name = input("请输入文件名:")
162+
# 打开文件
163+
try:
164+
# "E:\workspace_python\README.md"
165+
f = open(file_name, 'a+')
166+
while True:
167+
input_str = input()
168+
if input_str == '#':
169+
print(f.read())
170+
break
171+
else:
172+
f.write(input_str)
173+
except FileNotFoundError:
174+
print("没有这个文件!")
175+
finally:
176+
f.close()
177+
178+
write_file()
145179
# 考试题5:计算年龄
146180
#f(1) = 10 f(2) = f(1) + 2 f(3) = f(2) + 2
147181
def count_age(number):
@@ -206,25 +240,60 @@ class Class(object):
206240
def __init__(self, person_number):
207241
Class.__person_number = person_number
208242

209-
def increase_person_number(self):
210-
pass
243+
def increase(self):
244+
Class.__person_number += 1
211245

212246
def get_peson_number(self):
247+
print("班级总人数为:%d" % Class.__person_number)
213248
return Class.__person_number
214-
249+
#class1 = Class(10)
250+
#print(class1.get_peson_number())
251+
#class1.increase()
252+
#print(class1.get_peson_number())
215253
class Student(Class):
216254

217-
def __init__(self, name, age, score):
255+
def __init__(self, name, age, score, myClass):
218256
self.name = name
219257
self.age = age
220258
self.score = score
259+
myClass.increase()
221260

222261
def get_name(self):
262+
print("大家好我叫%s" % self.name)
223263
return self.name
224264

225265
def get_age(self):
226-
return self.age
266+
print("姓名:%s年龄:%d" % (self.name, self.age))
267+
return self.name,self.age
227268

228269
def get_score(self, class_name):
229270
if class_name in self.score:
230-
return "%d:%d" % (class_name, self.score[class_name])
271+
print("这是我的%s:%d" % (class_name, self.score[class_name]))
272+
return class_name, self.score[class_name]
273+
274+
def get_best_score(self):
275+
max_score = 0
276+
course = ""
277+
for k,v in self.score.items():
278+
if v > max_score:
279+
max_score = v
280+
course = k
281+
print("我最好的成绩是%s:%d" % (course, max_score))
282+
return max_score, course
283+
284+
class1 = Class(0)
285+
joe = Student("joe", 20, {"语文":69, "数学": 70, "英语": 100}, class1)
286+
joe.get_name()
287+
joe.get_age()
288+
joe.get_score("语文")
289+
joe.get_best_score()
290+
print('-' * 20)
291+
susan = Student("susan", 20, {"语文": 99, "数学": 100, "英语":79}, class1)
292+
susan.get_name()
293+
susan.get_age()
294+
susan.get_score("语文")
295+
susan.get_best_score()
296+
print('-' * 20)
297+
class1.get_peson_number()
298+
299+
12

0 commit comments

Comments
 (0)