条件和条件语句


  1. 下面的值在作为布尔表达式的时候,会被解释器看作假(False):
    False  None    0    ""    ()    []    {}
  2. 条件执行和if语句
    name = raw_input('What is your name?\n')
    if name.endswith('Gumby'):
        print 'Hello, Gumby'
    else:
        print 'I donot know you!'
  3. elif 字句
    num = input("PLS input a num\n")
    if num > 0:
        print "The num is positive!"
    elif num < 0:
        print "The num is negetive!"
    else:
        print "The num is zero"

    结果:

    PLS input a num
    0
    The num is zero

更复杂的条件


  1. 比较预算符
    == ; < ; > ; >= ; <= ; != ; is ; is not ; in ; not in
  2. 相等运算符
    >>> 'foo' == 'foo'
    True
    >>> 'foo' == 'fo'
    False
  3. is:同一性运算符
    >>> x = y = [1,2,3]
    >>> z = [1,2,3]
    >>> x == y
    True
    >>> x == z
    True
    >>> x is y
    True
    >>> x is z
    False
    >>> id(x)
    19018656
    >>> id(y)
    19018656
    >>> id(z)
    11149144

    同一性可以理解为内存地址相同的数据。

  4. in:成员资格运算符
    >>> name = ['a','b','c']
    >>> 'a' in name
    True
  5. 字符串和序列比较
    字符串可以按照字母顺序排列进行比较。
    >>> 'beat'>'alpha'
    True

    程序会遍历比较

    >>> 'Forst'.lower() == 'F'.lower
    False
    >>> 'Forst'.lower() == 'Forst'.lower()
    True
  6. 布尔运算符
    略过
  7. 断言
    如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert 语句就有用了,它可以在程序中置入检查点,条件后可以添加字符串,来解释断言:
    >>> age = -1
    >>> assert 0<age<100, 'the age must be crazy'
    
    Traceback (most recent call last):
      File "<pyshell#7>", line 1, in <module>
        assert 0<age<100, 'the age must be crazy'
    AssertionError: the age must be crazy

循环


  1. while
    它可以用来在任何条件为真的情况下重复执行一个代码块

    name = ''
    while not name:
        name = raw_input("input your name:\n")
    print "hello ,%s!" %name

    运行结果:

    input your name:
    world
    hello ,world!
  2. for循环
    >>> for number in range(101):
        print number
  3. 迭代工具
    ①并行迭代
    >>> names = ['anne','beth','george']
    >>> ages = [1,11,111]
    >>> zip(names,ages)
    [('anne', 1), ('beth', 11), ('george', 111)]
    >>> for name, age in zip(names,ages):
        print name, 'is',age
    
    anne is 1
    beth is 11
    george is 111

    ② 编号迭代

    enumerate函数

    ③ 翻转和排序迭代

    >>> sorted('hello,world!')
    ['!', ',', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
    >>> list(reversed('hello,world!'))
    ['!', 'd', 'l', 'r', 'o', 'w', ',', 'o', 'l', 'l', 'e', 'h']
  4. 跳出循环
    ① break
    结束(跳出)循环可以使用break语句
    >>> from math import sqrt
    >>> for n in range(99,0,-1):
        root = sqrt(n)
        if root == int(root):
            print n
            break
    
    81

    ② continue

    ③ while True/break 习语

    while True:
        word = raw_input("PLS input a word:")
        if not word:break
        print 'the word is:%s'%word 

《Python基础教程(第二版)》学习笔记 -> 第五章 条件、循环 和 其他语句的更多相关文章

  1. &lt;&lt;Python基础教程&gt;&gt;学习笔记 | 第10章 | 充电时刻

    第10章 | 充电时刻 本章主要介绍模块及其工作机制 ------ 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 一个简 ...

  2. &lt;&lt;Python基础教程&gt;&gt;学习笔记 | 第04章 | 字典

    第04章:字典 当索引不好用时 Python唯一的内建的映射类型,无序,但都存储在一个特定的键中.键能够使字符.数字.或者是元祖. ------ 字典使用: 表征游戏棋盘的状态,每一个键都是由坐标值组 ...

  3. &lt;&lt;Python基础教程&gt;&gt;学习笔记 | 第12章 | 图形用户界面

    Python支持的工具包非常多.但没有一个被觉得标准的工具包.用户选择的自由度大些.本章主要介绍最成熟的跨平台工具包wxPython.官方文档: http://wxpython.org/ ------ ...

  4. &lt;&lt;Python基础教程&gt;&gt;学习笔记 | 第11章 | 文件和素材

    打开文件 open(name[mode[,buffing]) name: 是强制选项,模式和缓冲是可选的 #假设文件不在.会报以下错误: >>> f = open(r'D:\text ...

  5. Jquery基础教程第二版学习记录

    本文仅为个人jquery基础的学习,简单的记录以备忘. 在线手册:http://www.php100.com/manual/jquery/第一章:jquery入门基础jquery知识:jquery能做 ...

  6. 第二章、元组和列表(python基础教程第二版 )

    最基本的数据结构是序列,序列中每个元素被分配一个序号-元素的位置,也称索引.第一个索引为0,最后一个元素索引为-1. python中包含6种内建的序列:元组.列表.字符串.unicode字符串.buf ...

  7. python基础教程第二版 第一章

    1.模块导入python以增强其功能的扩展:三种方式实现 (1). >>> Import math >>> math.floor(32.9) 32.0 #按照 模块 ...

  8. &lt;&lt;Python基础课程&gt;&gt;学习笔记 | 文章13章 | 数据库支持

    备注:本章介绍了比较简单,只是比较使用样品,主要假设是把握连接,利用数据库.和SQLite做演示样本 ------ Python数据库API 为了解决Python中各种数据库模块间的兼容问题,如今已经 ...

  9. python cookbook第三版学习笔记十五:property和描述

    8.5 私有属性: 在python中,如果想将私有数据封装到类的实例上,有两种方法:1 单下划线.2 双下划线 1 单下划线一般认为是内部实现,但是如果想从外部访问的话也是可以的 2 双下划线是则无法 ...

随机推荐

  1. 元素属性和js数组

    arrObj.push(数组元素) --增加arrObj.splice(index,howmany)--删除  一般howmany为1,  index,开始截取掉的位置,arrObj[index].P ...

  2. C# Windows - RadioButton&CheckBox

    RadioButton和CheckBox控件与Button控件有相同的基类,但它们的外观和用法大不相同. RadioButton显示为一个标签,左边是一个圆点,该点可以是选中或未选中.用在给用户提供两 ...

  3. centos apache 隐藏和伪装 版本信息

    1.隐藏Apache版本信息 测试默认 apache 的状态信息[root@1314it conf]# curl -Is localhostHTTP/1.1 200 OKDate: Tue, 16 N ...

  4. 闭包小demo

    var a = (function(){ var c= 0; return function(){ return ++c; } }()); var g = a(); console.log(g); v ...

  5. java对象数组

    问题描述:     java 对象数组的使用 问题解决: 数组元素可以是任何类型(只要所有元素具有相同的类型) 数组元素可以是基本数据类型 数组元素也可以是类对象,称这样的数组为对象数组.在这种情况下 ...

  6. Exploring the 7 Different Types of Data Stories

    Exploring the 7 Different Types of Data Stories What makes a story truly data-driven? For one, the n ...

  7. POJ3273Monthly Expense(二分)

    http://poj.org/problem?id=3273 题意: 农夫约翰给出了n天的每天花费 ,让你将这n天分成m组,每组中存在的天数必须是连续的,然后让每组里花费的总和尽量的小,最后将花费最大 ...

  8. Java Web开发 之JavaBean整理

    JavaBean是一种Java组件技术,就其本质就是一个类,具有如下特点:1:实现可序列化2:有一个public的无参的构造方法3:所有实例变量都是private的4:为每一个属性提供getter和s ...

  9. AC题目简解-数据结构

    A - Japan  POJ 3067 要两条路有交叉,(x1,y1)(x2,y2)那么需要满足:(x1-x2)*(y1-y2)<0判断出这是求逆序的问题 树状数组求逆序,先通过自定义的比较器实 ...

  10. 92. Reverse Linked List II

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...