python学习小计

2018-03-28 02:18:28   python_learning

  python小计  

  1. python 的三元运算符
    1. //条件为真时才为X否则为y
    2. small = x if x>y else y
  2. 断言(有时我们需要强制为真时才让程序进行,可以采用断言的方式去执行)
    1. #不会报错
    2. assert 3<4
    3. #抛出错误,程序终止
    4. assert 3>4
    5. Traceback (most recent call last):
    6. File "<stdin>", line 1, in <module>
    7. AssertionError
  3. while循环体
    1. while condition
    2. 循环体
  4. for循环体
    1. teacher = "johnshuxian"
    2. for each in teacher:
    3. print(each,end='')
  5. range(start,stop,step)循环
    1. #打印0到4不包含5
    2. for i in range(5):
    3. print(i)
    4. #打印5,7,9
    5. for i in range(5,10,2)
    6. print(i)