第12章 if测试和语法规则
python中没有switch/case 用字典索引 if/elif/else
文档字符串,显示但不运行
严格缩进,同一块中的语句采取相同的缩进
语句以反斜杠(\)结尾,就可以跨行
or:停在第一个为真的对象上 and:停在第一个为假的对象上
2 or 3>>>2 3 and []>>>[]
三元表达式 A = Y if X else Z <==> [Z, Y][bool(x)](这个不好,不会短路运算)
第13章 while和for循环
while <test>:
<statements1>
else:
<statements2>
pass:无运算,占位语句 while true:pass
...也可以代替空 还可以x=...
break, continue, else
<test> 不能是表达式 但可以通过下面方法实现:
while True:
x=next()
if x:
...
for循环
for <target> in <object>:
<statements1>
else:
<statements2>
自动解包:
for both in [(1,2),(3,4)]:
a,b = both
for比while运行的更快
range(-2,2)>>>[-2,-1,0,1] 经常与for配合使用
用range遍历字符串的优势是它不会复制串,也不会创建列表
zip 并行遍历多个列表 以较短的列表截断
s1 = 'abc' s2 = 'd' list(zip(s1,s2))>>>[(a,d)]
zip构造字典 d=dict(zip(keys, values))
产生偏移和元素 lis[enumerate('abc')]>>>[(0, 'a'), (1, 'b'), (2, 'c')]
第15章--文档
#注释 dir列出各种属性
文档字符串:文件开头,类和函数的开头(仅开头的字符串),其他忽略
自动保存在**.__doc__ 函数是在 文件.函数/类名.__doc__中
import 不用输入路径
python中没有switch/case 用字典索引 if/elif/else
文档字符串,显示但不运行
严格缩进,同一块中的语句采取相同的缩进
语句以反斜杠(\)结尾,就可以跨行
or:停在第一个为真的对象上 and:停在第一个为假的对象上
2 or 3>>>2 3 and []>>>[]
三元表达式 A = Y if X else Z <==> [Z, Y][bool(x)](这个不好,不会短路运算)
第13章 while和for循环
while <test>:
<statements1>
else:
<statements2>
pass:无运算,占位语句 while true:pass
...也可以代替空 还可以x=...
break, continue, else
<test> 不能是表达式 但可以通过下面方法实现:
while True:
x=next()
if x:
...
for循环
for <target> in <object>:
<statements1>
else:
<statements2>
自动解包:
for both in [(1,2),(3,4)]:
a,b = both
for比while运行的更快
range(-2,2)>>>[-2,-1,0,1] 经常与for配合使用
用range遍历字符串的优势是它不会复制串,也不会创建列表
zip 并行遍历多个列表 以较短的列表截断
s1 = 'abc' s2 = 'd' list(zip(s1,s2))>>>[(a,d)]
zip构造字典 d=dict(zip(keys, values))
产生偏移和元素 lis[enumerate('abc')]>>>[(0, 'a'), (1, 'b'), (2, 'c')]
第15章--文档
#注释 dir列出各种属性
文档字符串:文件开头,类和函数的开头(仅开头的字符串),其他忽略
自动保存在**.__doc__ 函数是在 文件.函数/类名.__doc__中
import 不用输入路径