• 《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)


    《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)

    print 'AB', 123 ==> AB 123 # 插入了一个空格
    print 'AB', 'CD' ==> AB CD # 插入了一个空格
    print 1,2,3 ==> 1 2 3
    print (1,2,3) ==> (1, 2, 3)
    #在脚本中以下ABCD连在一起输出
    print 'AB',
    print 'CD'

    import somemodule #导入模块
    from somemodule import somefunction #导入函数
    from somemodule import function1,function2... #导入函数
    from somemodule import *
    module1.open() #使用模块中函数
    module2.open()
    import math as foobar #为模块提供别名
    foobar.sqrt(4)
    from math import sqrt as foobar #为函数提供别名
    foobar(4)

    序列解包
    x,y,z=1,2,3; print x,y,z ==> 1 2 3
    x,y,z=1,2,3; x,y=y,z; print x,y ==> 2 3
    x=1,2,3; a,b,c=x; print a,b,c ==> 1 2 3
    d={'key1':'value1','key2':'value2'}; key,value=d.popitem(); print key,value ==> key2 value2

    链式赋值
    x=y=z={'key1':'value1'}; x is y ==> True
    x=y=z={'key1':'value1'}; x==y  ==> True

    增量赋值
    x+=1; x*=1;

    可以用tab或空格分层缩进代码;推荐用四个空格来缩进。用Tab缩进更方便,比空格更快。

    : 表示语句块的开始

    布尔值:True, False, bool()
    这些被看为False: False, None, 0, "", (), [], {}
    True==1  ==> True
    False==0  ==> True
    True+False+4   ==> 5

    bool('ABC') ==> True
    bool('') ==> False
    bool(3) ==> True
    bool(0) ==> False

    if expression:
        block1
        
    if expression:
        block1
    else:
        block2
        
    if expression1:
        block1
    elif expression2:
        block2
    else:
        block3

    比较运算符
    x==y 相等运算符
    x!=y 不相等运算符
    x is y 同一性运算符
    x is not y
    x in y 成员资格运算符
    x not in y

    0<age<100 # python可以这样用

    'ab'=='ab' ==> True
    'ab'=='cd' ==> False
    12==34 ==> False

    x=y=[1,2]; x==y ==> True
    x=y=[1,2]; x is y ==> True
    x=y=[1,2]; z=[1,2]; x is z ==> False
    x=y=[1,2]; z=[1,2]; x==z ==> True

    'ABC'<'ABC' ==> False
    [1,2]<[2,1] ==> True

    布尔运算符:
    and, or, not

    断言
    age=-1; assert 0<age<100, 'NOTE'
    Traceback (most recent call last):
      File "<pyshell#21>", line 1, in <module>
        age=-1; assert 0<age<100, 'NOTE'
    AssertionError: NOTE

    while expression:
      block

    for item in itemset:
      block

    range函数包含下限,不包含上限;
    range(0,3) ==> [0, 1, 2]
    range(3) ==> [0, 1, 2]

    遍历字典
    d={'key1':'value1','key2':'value2'};
    for key in d:
        print key,d[key]

    for key,value in d.items():
        print key,value

    并行迭代
    a=[1,2,3]; b=['a','b','c']; zip(a,b) ==> [(1, 'a'), (2, 'b'), (3, 'c')]

    编号迭代
    for index, string in enumerate(strings):
        if 'xxx' in string:
           strings[index]='NewValue'

    反转和排序迭代
    sorted([3,1,2]) ==> [1, 2, 3]
    x=[1,2,3]; list(reversed(x)) ==> [3, 2, 1] # reversed返回可迭代对象

    break结束循环
    continue进行下一次循环

    while True:
       block1
       if expression:
          break
       block2

    for item in items:
       block1
       if expression:
          break
       block2
    else:
       block3  # 当for执行完毕,并且没有执行过其中的break,此时执行block3

    列表推导式
    [x*x for x in range(3)] ==> [0, 1, 4]
    [x*x for x in range(3) if x%2==0] ==> [0, 4]
    [(x,y) for x in range(3) for y in range(3)] ==> [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

    pass # 占位符,类似于空语句
    del # 删除对象
    exec # 执行一个字符串
    exec "print 'ABC'" ==> ABC
    from math import sqrt; scope={}; exec 'sqrt=1' in scope; sqrt(4); scope['sqrt'];

    eval 求值,类似于 exec
    eval(raw_input(""))
    1+1
    2

    scope={}; scope['x']=2; scope['y']=3; eval('x*y',scope) ==> 6

  • 相关阅读:
    视图创建
    根据表格作业题
    表格 作业题练习
    创建表格 练习题
    聚合函数、数学函数、日期时间函数
    接口自动化框架
    python+request+Excel 几十份接口测试用例一起自动化测试的接口测试框架
    python3 函数
    pip源头
    爬虫
  • 原文地址:https://www.cnblogs.com/emanlee/p/4028097.html
Copyright © 2020-2023  润新知