条件:

if 条件:

    语句块

elif:

    语句块

else:

    语句块

elif 表示 else if

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

  1. >>> if 1 < x < 2:
  2. print('True')
  3. True

and 表示且

  1. >>> if x > 1 and x < 2:
  2. print('True')
  3. True

or 表示 或

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

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

a  if  b  else  c

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

while 循环

while 条件:

语句块

不需要括号哦!

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

经常用 :

  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. 0
  4. 1
  5. 4
  6. 9
  7. 16
  8. 25
  9. 36
  10. 49
  11. 64
  12. 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. I didn't fint it

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

  1. >>> [x*x for x in range(1,5)]
  2. [1, 4, 9, 16]
  3. >>> [x**2 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. >>>

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的更多相关文章

  1. Python之条件 循环和其他语句 2014-4-6

    #条件 循环和其他语句 23:30pm-1:431.print和import的更多信息 使用逗号将多个表达式输出 >>> print 'age:',42 age: 42 >&g ...

  2. 一步一步学python(五) -条件 循环和其他语句

    1.print 使用逗号输出 - 打印多个表达式也是可行的,但要用逗号隔开 >>> print 'chentongxin',23 SyntaxError: invalid synta ...

  3. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 7. 条件循环

    条件循环能够让程序在条件成立时(即为真时)重复执行循环体中的语句.如果条件一直成立(即永远不会为假),则循环会一直进行下去,不会停止.如果初始时,条件不成立,则循环 1 次也不会执行.Python 中 ...

  4. Python 3语法小记(六)条件、循环和assert、pass、del

    条件: if 条件:     语句块 elif:     语句块 else:     语句块 elif 表示 else if 这居然是合法的!!!1 < x < 2!!! >> ...

  5. python学习笔记2_条件循环和其他语句

    一.条件循环和其他语句 1.print和import的更多信息. 1.1.使用逗号输出  //print() 打印多个表达式是可行的,用逗号隔开.       在脚本中,两个print语句想在一行输出 ...

  6. Python基础教程之第5章 条件, 循环和其它语句

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环 ...

  7. python变量、条件循环语句

    1. 变量名 - 字母  - 数字  - 下划线  #数字不能开头:不能是关键字:最好不好和python内置的函数等重复 2. 条件语句 缩进用4个空格(Tab键)注意缩进如果是空格键和Tab键混用, ...

  8. Python的条件判断与循环

    1.if语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下 if condition: blockelif condition: block...else: ...

  9. Python条件循环判断

    1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: 1 2 3 4 5 6 7 8 9 age_of_cc = 27   age = int( ...

随机推荐

  1. 张艾迪(创始人):Hello.世界...

    The World No.1 Girl :Eidyzhang The World No.1 Internet Girl :Eidyzhang AOOOiA.global Founder :Eidyzh ...

  2. Python基本时间转换

    时间转换 python中处理时间的时候,最常用的就是字符形式与时间戳之间的转换. 把最基本的转换在这里记下来 string -> timestamp import time import dat ...

  3. 跑马灯标记marquee

    常见属性: direction:滚动方向(left默)/right/up/down; behavior:滚动方式(scroll默)/slide/alternate来回弹动: width.height. ...

  4. Xcode5 取消ARC

    终于开心的装上Xcode5,主管马上发布新的任务,开始新的项目,各种开心,终于可以换个界面看看了. 可是谁知第一步创建项目就开始悲剧了,居然没有地方可以选择非ARC了,真是肿么个情况呀,查了一下,万能 ...

  5. Tri-Training: Exploiting Unlabeled Data Using Three Classifiers

    Abstract – In many practical data mining applications such as web page classification, unlabeled tra ...

  6. qt搭建环境

    1 用viewteam实现远程控制电脑.可以在家里继续操作办公电脑了. http://blog.csdn.net/sch0120/article/details/38324599 2qt环境安装.今天 ...

  7. 二分查找C++

    #include <iostream> using namespace std; //二分查找:每次都从中间位置寻找,如果找到了就返回,如果没找到, //则分两种情况: //(1)中间元素 ...

  8. android基础(四)service

    Service的两种启动方式:startService()与bindService()   statService:生命周期:[onCreate()-  >onStartCommand()-&g ...

  9. Libgdx 开发指南(1.1) 应用框架——生命周期

    生命周期 Libgdx应用有一个定义好的生命周期,控制着整个应用的状态,例如creation, pausing, resuming, disposing ApplicationListener 开发者 ...

  10. BZOJ 1833 count 数字计数

    sb数位dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...