python学习手册笔记--第10章--语句简介--第11章--复制_表达式_打印

来源:互联网 发布:js获取元素name属性值 编辑:程序博客网 时间:2024/06/05 02:36

第10章--语句简介

pass        空占位符
yield        生成器函数    ??
nonlocal    命名空间(3.0)    ??
try/except    捕捉异常    ??
raise        触发异常    ??
assert        调试检查    assert x>y,'x too small'    
with/as    环境管理器(2.6)    ??    with open('data') as myfile:process(myfile)
冒号,缩进语法,对齐--所见即所得
一行多条语句,用;隔开        x=1;y=2;print(x+y)
任何在(), [], {}中的内容都可以跨行
if(a==b  and
   c==d  and):
简单语句可以直接跟在:后面    if x > y:print(x)
input('please enter your name:')    接受输入函数
交互小例子:
while True:
    i = input('please enter x and n, we will calculate power(x, n)\n')
    if(i == 'stop'):break
    l = i.split()
    try:
        print(int(l[0]) ** int(l[1]))
    except:
        print('bad!'*6)
print('bye')

第11章--复制_表达式_打印

name,age='cai',42    <==>    [name,age]=['cai', 42]
a,b,c='cai'    >>>    a='c',b='a',c='i'
a,*b='cai'    >>>    a='c', b=['a', 'i']    (3.0)
等号左边,不能有两个带*的元素
带*的元素可以在中间,会自动匹配        a,*b,c,d='qwert'    b=['w','e']
tips:
增强赋值,对于列表和字典是在原处修改
l=m=[1,2]    l +=[3,4]    l,m>>>([1,2,3,4],[1,2,3,4])
l=m=[1,2]     l = l + [3,4]    l,m>>>([1,2,3,4],[1,2])
以保留字命名的文件,无法导入
3.0标准打印print
print([object,……][, sep=' '][, end='\n'][, file=sys.stdout])
流重定向    sys.stdout=open('log.txt','a')    用print写入文件log.txt
log=open('log.txt','a')    print(data, file=log)


原创粉丝点击