我是靠谱客的博主 超级鸭子,这篇文章主要介绍python学习中级,纯代码学习版,现在分享给大家,希望可以做个参考。

参考自



http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431928972981094a382e5584413fa040b46d46cce48e000


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#map(fun,list) print (list(map(str,list(range(10))))) print (map(str,range(10))) #将字符串数字转化为int from functools import reduce def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] return reduce(fn, list(map(char2num, s))) print (str2int('123')) #map(fun,l1) 对list中所有数执行函数fun def change(s): return s[0].upper()+s[1:].lower() l=['lias','DDVd','FAs'] l2=map(change,l) print (list(l2)) #reduce(fun,l2) 对list中所有数累计执行fun,fun(x,y) def prod(l): def pro(x,y): return x*y return reduce(pro,l) print (prod([1,2,3])) #累乘 6 #filter(fun,list) fun函数返回true则保留,否则舍弃 def is_odd(n): return n%2==0 print (list(filter(is_odd,[1,2,3,4,5,6]))) #2,4,6 print (sorted(['A','c','B','d'],key=str.lower)) print (sorted(['A','c','B','d'],key=str.lower,reverse=True)) L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] def cmp(x): return x[0] print (sorted(L,key=cmp)) #匿名函数 lambda x :x*x == def x: return x*x print (list(map(lambda x:x*x,[1,2,3]))) #类,,在函数或变量前加__就是定义为内部变量 class Stu(object): def __init__(self,name,score): self.name=name #self.__name=name 内部变量外部不能通过stu.__name访问 self.score=score def print_sco(self): print ('%s: %d'% (self.name,self.score)) def get_grade(self): if (self.score>=90): return 'a' else: return 'not a' stu = Stu('baiy',90) stu.print_sco() stu.__name='xxx' #可以通过此方式为类添加成员变量,但是不是内部的 print (stu.__name) class Animal(object): def run(self): print ('ani is run') class Dog(Animal): pass class Cat(Animal): def run(self): print ('cat is run') dog=Dog() dog.run() #ani is run cat=Cat() cat.run() #cat is run 覆盖 type(123)==type(456) #True type(123)==int #True type('abc')==type('123') #True type('abc')==str #True type('abc')==type(123) #False dir('ABC') #获取对象的所有属性和方法 len('ABC') == 'ABC'.__len__() #所有想调用len(),只需要在类中实现__len__()函数 #类属性和实例属性 class Student(object): name = 'Student' s = Student() # 创建实例s print(s.name) # 打印name属性,因为实例并没有name属性,所以会继续查找class的name属性 #Student print(Student.name) # 打印类的name属性 Student s.name = 'Michael' # 给实例绑定name属性 print(s.name) # 由于实例属性优先级比类属性高,因此,它会屏蔽掉类的name属性 #Michael print(Student.name) # 但是类属性并未消失,用Student.name仍然可以访问 #Student del s.name # 如果删除实例的name属性 print(s.name) # 再次调用s.name,由于实例的name属性没有找到,类的name属性就显示出来了 #Student #给实例绑定一个方法 只对此实例有效 s = Student() def set_age(self,age): self.age=age from types import MethodType s.set_age=MethodType(set_age,s) s.set_age(25) print (s.age) #给类绑定一个方法,对所有实例有效 def set_sex(self,sex): self.sex=sex Student.set_sex=set_sex s.set_sex('man') print(s.sex) #__slots__现在成员变量 class People(object): __slots__=('name','age') peop=People() #peop.x=100 错误 #property用法 class Student(object): @property def birth(self): return self._birth @birth.setter def birth(self, value): self._birth = value @property def age(self): return 2015 - self._birth #多重继承 class Runnable(object): pass class Dog(Animal,Runnable): pass f=open('text.txt','r') ss=f.readline() s=f.readlines() print(ss) print (s) print (s[3]=='n') f.close() f = open('test.txt', 'w') f.write('Hello, world!') f.close() #stringio from io import StringIO f = StringIO() f.write('hello') f.write(' ') f.write('world!') print(f.getvalue()) #hello world!

最后

以上就是超级鸭子最近收集整理的关于python学习中级,纯代码学习版的全部内容,更多相关python学习中级内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部