码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3 条件、循环和assert、pass、del

时间:2016-11-07 01:28:35      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:most   语句   eth   plain   boa   assertion   erro   条件   tab   

条件:

if 条件:
    语句块
elif:
    语句块
else:
    语句块


elif 表示 else if


这居然是合法的!!!1 < x < 2!!!

  1. >>> if 1 < x < 2:  
  2.     print(‘True‘)  
  3.   
  4.       
  5. True  


and 表示且

  1. >>> if x > and x < 2:  
  2.     print(‘True‘)  
  3.   
  4.       
  5. True  


or 表示 或

  1. >>> x  
  2. 2  
  3. >>> if x == or x == 3:  
  4.     print(x)  
  5.   
  6.       
  7. 2  

 

如果 b 为真则返回a,否则返回 c

a  if  b  else  c

  1. >>> ‘True‘ if 1 < x <else ‘False‘  
  2. ‘True‘  

 

while 循环

while 条件:

   语句块


不需要括号哦!

  1. >>> x  
  2. 1.2  
  3. >>> while x < 2:  
  4.     print(x)  
  5.     x += 0.2  
  6.   
  7.       
  8. 1.2  
  9. 1.4  
  10. 1.5999999999999999  
  11. 1.7999999999999998  
  12. 1.9999999999999998  
  13. >>>  


经常用 :

  1. while True:  
  2.     ....  
  3.     if ... :  
  4.         break  
  5.     ....  


for 循环

for something in XXXX:

    语句块


即表示对XXXX中的每一个元素,执行某些语句块,XXXX可以是列表,字典,元组,迭代器等等。

  1. >>> for x in range(0,10):  
  2.     print(x*x)  
  3.   
  4.       
  5. 0  
  6. 1  
  7. 4  
  8. 9  
  9. 16  
  10. 25  
  11. 36  
  12. 49  
  13. 64  
  14. 81  

这是 for..else...语句
仅在没有 break 的情况下执行,或者说,只要你没有 break,它就会执行


  1. >>> for n in range(99,81,-1):  
  2.     root = sqrt(n)  
  3.     if root == int(root):  
  4.         print (n)  
  5.         break  
  6. else:  
  7.     print ("I didn‘t fint it")  
  8.   
  9.       
  10. I didn‘t fint it  


但你应该尽可能使用列表推导式,因为它更方便,清晰

  1. >>> [x*x for x in range(1,5)]  
  2. [1, 4, 9, 16]  
  3. >>> [x**for x in range(1,10) if x % 2 ==0]  
  4. [4, 16, 36, 64]  
  5. >>> [(x,y) for x in range(1,3) for y in range(4,6)]  
  6. [(1, 4), (1, 5), (2, 4), (2, 5)]  


断言 assert
后面语句为真,否则出现 AssertionError

用来检查一个条件,如果它为真,就不做任何事。如果它为假,则会抛出AssertError并且包含错误信息。

例如:

py> x = 23
py> assert x > 0"x is not zero or negative"
py> assert x%2 == 0"x is not an even number"
Traceback (most recent call last):
File "", line 1in
AssertionError: x is not an even number
#常用在代码开头的注释
assert target in (x, y, z)
if target == x:
    run_x_code()
elif target == y:
    run_y_code()
else:
    assert target == z
    run_z_code()

 

 

pass

pass 表示这里什么都没有,不执行任何操作

如果你的程序还有未完成的函数和类等,你可以先添加一些注释,然后代码部分仅仅写一个 pass,这样程序可以运行不会报错,而后期你可以继续完善你的程序

  1. >>> class Nothing:  
  2.     pass  
  3.   
  4. >>>   


del
del 删除的只是引用和名称,并不删除值,也就是说,Python 会自动管理内存,负责内存的回收,这也是 Python 运行效率较低的一个原因吧

    1. >>> x = [1,2,3]  
    2. >>> y = x    #x 和 y指向同一个列表  
    3. >>> del x  
    4. >>> x  
    5. Traceback (most recent call last):  
    6.   File "<pyshell#41>", line 1, in <module>  
    7.     x  
    8. NameError: name ‘x‘ is not defined  
    9. >>> y  
    10. [1, 2, 3] 

Python 3 条件、循环和assert、pass、del

标签:most   语句   eth   plain   boa   assertion   erro   条件   tab   

原文地址:http://www.cnblogs.com/harvey888/p/6036901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!