前面我们已经把所有的Python数据结构说完了,从现在起就开始讲程序的执行逻辑了,因为程序不可能每个执行语句都要顺序执行,正是有了这些条件或循环的执行逻辑,才体现出了算法,之前看过书编程书的人都知道,程序是什么?其实程序就是数据+算法,我们了解了数据,再加上了解程序的执行逻辑,就能写出复杂的程序来,这些执行逻辑每个语言都有,就是语法稍有不同而已,所以我们来看看在Python语言里怎么实现条件和循环。
1、条件语句:If/else
1 2 |
if 条件: 执行语句 |
这是最简单的用法,只有条件为真时才执行后面的语句。
1 2 3 4 |
if 条件: 执行语句1 else: 执行语句2 |
这是加了else的表达方式,如果条件为真执行语句1,否则就执行语句2,注意在条件和else后面都有一个冒号(:),这是Python的语法要求,因为python下没有了大括号,完全是用缩进方式来区分代码块,所以回车换行语句前面要有空格(一般是4个空格),另外如果条件后只有一个单一的语句可以写成用一行来表示:
1 |
if 条件: 执行语句 |
注意条件这里可以是单个表达式也可以是多个表达式,如果是多个表达式就需要用and,or和not连接起来,分别表示与或非关系,例如:
1 2 3 4 5 6 |
>>> a = 1 >>> b = 2 >>> if a == 1 and b == 2: ... print a+b ... 3 |
条件语句的多重语句判断elif,它检查多个表达式是否为真,如果为真就执行对应的代码块,例如:
1 2 3 4 5 6 7 8 |
if 条件1: 语句1 elif 条件2: 语句2 elif 条件3: 语句3 else: 其它语句 |
2、循环语句: while, for
while语句功能跟shell里的while基本一样,当条件为真时会一直执行相应的代码块,直到条件为假时结束,例如:
1 2 3 4 5 6 7 8 |
#!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" |
执行结果:
1 2 3 4 5 6 |
0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5 |
这个语句跟我们之前学的基本一样,不过多讲了,大家可以自己写几个列子测试下,唯一需要注意的地方是使用while循环时主要循环的结束,避免写成死循环。
下面介绍下我们使用最多的for循环,如果接触过shell里的for循环,再看python的for循环就很容易上手,因为都属于是枚举型循环,使用for循环非常容易遍历一个序列对象,例如遍历列表,字典,元组等。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/python fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print 'Current fruit :', fruits[index] print "Good bye!" ##运行结果## Current fruit : banana Current fruit : apple Current fruit : mango Good bye! |
这种方式是通过列表的索引来遍历列表,range函数可以生成自然数序列,运行结果:
1 |
如果需要把索引也打印出来可以用enumerate函数,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/python fruits = ['banana', 'apple', 'mango'] for index,ef in enumerate(fruits): print '%d %s' % (index+1, ef) print "Good bye!" ##运行结果## 1 banana 2 apple 3 mango Good bye! |
3、break,continue语句
break语句是结束当前循环去执行后面的语句,break可以立刻从while和for循环中退出来,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#!/usr/bin/python for letter in 'Python': if letter == 'h': break print 'Current Letter :', letter #################################################### var = 10 while var > 0: print 'Current variable value :', var var = var -1 if var == 5: break print "Good bye!" ##执行结果## Current Letter : P Current Letter : y Current Letter : t Current variable value : 10 Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6 Good bye! |
continue语句是当遇到continue语句时程序终止当前的循环,不在执行后续的语句,然后回到循环的顶端进行判断,如果条件为真或有元素可迭代,然后进行下一次的循环,例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/usr/bin/python for letter in 'Python': if letter == 'h': continue print 'Current Letter :', letter ############################################### var = 10 while var > 0: var = var -1 if var == 5: continue print 'Current variable value :', var print "Good bye!" ##执行结果## Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6 Current variable value : 4 Current variable value : 3 Current variable value : 2 Current variable value : 1 Current variable value : 0 Good bye! |
条件和循环语句就介绍到这里,下篇我们介绍下Python的函数,喜欢的小伙伴请帮忙转发下。