1. 循环 if, while, for
break : 结束整个循环
continue :跳出当前这次循环,但不结束整个循环
else :结束整个循环后才执行,不能与break合用,但可以与continue合用。

 

2. bool 惰性求值,从左到右判断,只要出现一个false就返回。

 

3. 打开文件

 1 # 常用方法
 2 # 打开文件test.txt
 3 file = open('test.txt', 'r')
 4 # 读取文件
 5 file.read()
 6 # 关闭文件
 7 file.close()
 8 
 9 # with 方法(系统自动会关闭文件)
10 with open('test.txt', 'r') as f:
11  file = f.read()
12 
13 '''
14 with 工作机制分析
15 打开文件时,调用对象 test 的 __enter__方法
16 关闭文件时,调用对象 test 的 __exit__方法
17 分析如下
18 '''
19 class test(object):
20  def __init__(self, args):
21   self.args  = args
22  def __enter__(self):
23   print('I am come in now')
24   return self.args
25  def __exit__(self, type, value, traceback):
26   print('I am come out now')
27 with test('hello men') as f:
28  # 返回 __enter__ 方法 return 的值
29  print(f)
View Code

相关文章: