编程语言
首页 > 编程语言> > python第十天

python第十天

作者:互联网

.定义一个生成器函数,生成1-10 使用next(generator)方法获取1-10 使用for循环获取2.# 自己定义一个MyIterator, 实现range的功能 # # range(start, stop, step) => MyIterator # # range(10) # start=0, stop=10, step=1 # # rang(10) # range(start=0, stop, step=1) =>start=10 # # range(0,10) # # range(0,10,2) # # MyIterator(10) # # MyIterator(0,10) # # MyIterator(0,10,2)3. re中函数的使用(自己写用例来使用): match fullmatch search findall finditer split sub subn complie

range(start, stop, step)range(10) => stoprange(1, 10) => start, stoprange(1, 10, 2) => start, stop, step"""class MyRange: def init(self, start=None, stop=None, step=1): #传递一个参数 if start is not None and stop is None: self.stop = start self.start = 0 #传递两个参数, 三个参数 if start is not None and stop is not None: self.start = start self.stop = stop self.step = step def iter(self): return self def next(self): #什么时候取完了,需要一个终止的条件 #需要有一个记录位置的变量, 如果这个位置超过 stop结束 if self.start < self.stop: data = self.start self.start += self.step return data else: raise StopIterationfor i in MyRange(10): print(i)print(80 * “")for i in MyRange(1, 10): print(i)print(80 * "”)for i in MyRange(1, 10, 2): print(i)# 传一个参数 -》 start -> stop# 传递两个参数 -》 start, stop# 传递三个参数 -> start, stop ,step# step为负值# for i in range(1, 11):# print(i)def get_num(): for num in range(1, 11): yield numgen = get_num()print(type(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))# print(next(gen))

标签:10,第十天,python,self,stop,next,start,print
来源: https://blog.csdn.net/duoxiangl/article/details/121395652