Python学习笔记(五)条件,循环和其他语句(上)

来源:互联网 发布:原生js click事件 编辑:程序博客网 时间:2024/05/22 13:25

5.1 print 和import的更多信息

   5.1.1 使用逗号输出

>>> print 1,2,31 2 3

  5.2 赋值魔法

   5.2.1 序列解包

     多个赋值可以同时进行

   

>>> x,y,z=1,2,3>>> x,y,z(1, 2, 3)
      用来交换两个变量也可以

    

>>> x,y=y,x>>> print x,y2 1
     事实上,这里所做的事情叫作序列解包:将多个值的序列解开,然后放到变量序列中.

   

   当函数或者方法返回元组时,这个方法格外有用

   

>>> s={'name':'l','age':25}>>> s.popitem();('age', 25)>>> key,value=s.popitem()>>> key'name'
5.2.2 链式赋值

   

>>> x=y=2>>> x2>>> y2
5.2.3 增量赋值

>>> x=1>>> x+=11>>> x12>>> x='a'>>> x*=2>>> x'aa'

5.3 语句块: 缩排的乐趣

    语句块是条件为真时执行或执行多次的一组语句,下面将具体说明.


5.4 条件和条件语句

   5.4.1 这就是布尔变量的作用

   下面的值作为布尔表达式的时候,会被解释器看做假(false)

   False   None  0  "" () [] {}

  5.4.2 条件执行和if语句

   

name='gumby'>>> if name.endswith('gumby'):print 'hello,mr.gumby'hello,mr.gumby
5.4.3 else语句

        与 if 搭配使用 . 与C,java 等语言一致.

5.4.4

      elif子句

    它是  else if 的简写 ,用法也与c ,java 中else if 一致

5.4.5

5.4.6

5.4.7 断言

   if 语句有个非常有用的"近亲" 断言

  它的工作方式可以用下面伪代码表示

  if  not condition :

   crash program

   它存在的意义是,与其让程序再晚些时候崩溃, 不如让程序在错误出现时就崩溃.使用关键字assert

>>> age = 10>>> assert 0<age<10Traceback (most recent call last):  File "<pyshell#8>", line 1, in <module>    assert 0<age<10AssertionError>>> age=-1>>> assert 0 <age <100,' you r wrong'Traceback (most recent call last):  File "<pyshell#10>", line 1, in <module>    assert 0 <age <100,' you r wrong'AssertionError:  you r wrong

5.5 循环

   5.5.1 while循环

  

x=1;>>> while x<=10:print xx+=112345678910
5.5.2 for循环

 

>>> numbers= [0,1,2,3,4,5,6]>>> for number in numbers:print number0123456

因为迭代某一范围的数字是很常见的,所以有个内建范围函数供使用

 for number in range(0,10):print number0123456789


5.5.3 循环遍历字典元素

 d={'x':1 ,'y':2}>>> for key in d:print key,'corresponds to',d[key]y corresponds to 2x corresponds to 1

5.5.4 一些迭代工具

    1  并行迭代

       程序可以同时迭代两个序列,不如下面两个列表

    

>>> names['a', 'b', 'c', 'd']>>> ages[1, 2, 3, 4]>>> for i in range(len(names)):print names[i],'is ',ages[i],'years old'a is  1 years oldb is  2 years oldc is  3 years oldd is  4 years old
    内建zip函数可以用来并行机迭代,可以把连个序列"压缩"在一起,然后返回一个元组的列表:

   

>>> zip(names,ages)[('a', 1), ('b', 2), ('c', 3), ('d', 4)]>>> for name,age in zip(names,ages):print name,'+',agea + 1b + 2c + 3d + 4
2 编号迭代

  有时候想要迭代序列中的对象,同时获取对象的索引 . 例如,在一个字符串列表中替换所有包含'xxx'的子字符串.

 普通方法:

   index = 0;

  for  string in strings:

           if 'xxx' in string :

           strings[index]='[censored]'

 index+=1


另一种方法是使用内建的enumerate函数:

     for index,string in enumerate(strings):

               if 'xxx' in string:

               strings[index]='[censored]'

 这个方法可以在提供索引的地方迭代索引-值树

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

      

1 0
原创粉丝点击