Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 205.md #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions 205.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,27 +202,28 @@

#网友frankwang分享一段关于素数的代码,供各位参考:

def find_primes(n):
primesList = []
for x in range(2, n+1):
isPrime = True
for y in range(2, int(x**0.5) + 1): #x**0.5 相当于math.sqrt(x)
if x % y == 0:
isPrime = False
break
if isPrime:
primesList.append(x)

print(primesList)
if __name__ == "__main__":
max = int(input('Find primes up to: '))
find_primes(max)
def find_primes(n):
primesList = []
for x in range(2, n+1):
isPrime = True
for y in range(2, int(x**0.5) + 1): #x**0.5 相当于math.sqrt(x)
if x % y == 0:
isPrime = False
break
if isPrime:
primesList.append(x)
print(primesList)

if __name__ == "__main__":
max = int(input('Find primes up to: '))
find_primes(max)

代码保存后运行:
# 打印结果如下:
Find primes up to: 100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Find primes up to: 100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]


##编写函数的注意事项

Expand Down
2 changes: 1 addition & 1 deletion 214.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
def __iter__(self):
return self

def next(self):
def next(self): #python3: def __next__(self):
fib = self.a
if fib > self.max:
raise StopIteration
Expand Down
6 changes: 3 additions & 3 deletions 235.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ nested的汉语意思是“嵌套的,内装的”,从字面上读者也可

contextlib.contextmanager是一个装饰器,它作用于生成器函数(也就是带有yield的函数),一单生成器函数被装饰以后,就返回一个上下文管理器,即contextlib.contextmanager因为装饰了一个生成器函数而产生了`__enter__()`和`__exit__()`方法。例如:

特别要提醒,被装饰的生成器函数只能产生一个值,否则就会抛出RuntimeError异常;如果有as子句,则所产生的值,会通过as子句赋给某个变量,就如同前面那样,例如下面的示例(本示例来自:http://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/index.html)。
特别要提醒,被装饰的生成器函数只能产生一个值,否则就会抛出RuntimeError异常;如果有as子句,则所产生的值,会通过as子句赋给某个变量,就如同前面那样,例如下面的示例(本示例来自:http://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/index.html )。

#!/usr/bin/env python
# coding=utf-8
Expand All @@ -274,7 +274,7 @@ contextlib.contextmanager是一个装饰器,它作用于生成器函数(也
the word is: contextmanager demo.
after yield.

为了好玩,再借用网上的一个示例,理解这个装饰器的作用(下面代码来自:http://preshing.com/20110920/the-python-with-statement-by-example/),代码中用到了`cairo`模块,该模块的安装方法是:
为了好玩,再借用网上的一个示例,理解这个装饰器的作用(下面代码来自:http://preshing.com/20110920/the-python-with-statement-by-example/ ),代码中用到了`cairo`模块,该模块的安装方法是:

sudo apt-get install libcairo2-dev

Expand Down Expand Up @@ -302,7 +302,7 @@ contextlib.contextmanager是一个装饰器,它作用于生成器函数(也
cr.line_to(0, 0)
cr.stroke()
cr.scale(0.72, 0.72)
if angle > 0.72:
if angle > 0.12:
for a in [-angle, angle]:
with saved(cr):
cr.rotate(a)
Expand Down