详解Python中的循环语句的用法
|
一、简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性。须重要理解,if、while、for以及与它们相搭配的 else、 elif、break、continue和pass语句。 Python中的if子句由三部分组成:关键字本身、用于判断结果真假的条件表达式以及当表达式为真或者非零时执行的代码块。if 语句的语法如下: if expression: expr_true_suite if 语句的expr_true_suite代码块只有在条件表达式的结果的布尔值为真时才执行,否则将继续执行紧跟在该代码块后面的语句。 if expression: expr_true_suite else: expr_false_suite 在C语言中,不会在条件语句范围外发现else语句, 但Python不同,可以在while和for循环中使用else语句,在循环中使用时,else子句只在循环完成后执行,也就是说break语句也会跳过else块。 在CODE上查看代码片派生到我的代码片 #!/usr/bin/env python def showMaxFactor(num): count = num / 2 while count > 1: if (num % count == 0): print 'largest factor of %d is %d' % (num,count) break count = count - 1 else: print eachNum,'is prime' for eachNum in range(10,21): showMaxFactor(eachNum) 在CODE上查看代码片派生到我的代码片 largest factor of 10 is 5 11 is prime largest factor of 12 is 6 13 is prime largest factor of 14 is 7 largest factor of 15 is 5 largest factor of 16 is 8 17 is prime largest factor of 18 is 9 19 is prime largest factor of 20 is 10 3、elif (即else-if )语句 if expression1: expr1_true_suite elif expression2: expr2_true_suite ... elif expressionN: exprN_true_suite else: none_of_the_above_suite 在将来的某天,Python可能会支持 switch /case语句,但是完全可以用其他的Python结构来模拟它。在Python中,大量的if-elif 语句并不难阅读。 if user.cmd == 'create': action = "create item" elif user.cmd == 'delete': action = 'delete item' elif user.cmd == 'update': action = 'update item' else: action = 'invalid choice... try again!' 上面的语句还可以用序列和成员关系操作符来简化它: 在CODE上查看代码片派生到我的代码片
if user.cmd in ('create','delete','update'):
action = '%s item' % user.cmd
else:
action = 'invalid choice... try again!'
还可以用Python字典给出更加优雅的解决方案,使用映射对象(比如字典)的一个最大好处就是它的搜索操作比类似语句或是 for 循环这样的序列查询要快很多。 在CODE上查看代码片派生到我的代码片
msgs = {'create': 'create item','delete': 'delete item','update': 'update item'
}
default = 'invalid choice... try again!'
action = msgs.get(user.cmd,default)
4、条件表达式(即"三元操作符") 在CODE上查看代码片派生到我的代码片 >>> x,y = 4,3 >>> smaller = x if x < y else y >>> smaller 3 5、while语句 while expression: suite_to_repeat while循环的suite_to_repeat子句会一直循环执行,直到expression值为布尔假。 count = 0 while (count < 9): print 'the index is:',count count += 1 代码块里包含了print和自增语句,它们被重复执行,直到count不再小于9。索引count在每次迭代时被打印出来然后自增 1。 while True: handle,indata = wait_for_client_connect() outdata = process_request(indata) ack_result_to_client(handle,outdata) “无限”循环永远不会结束,但它不一定是坏事,许多通讯服务器的客户端/服务器系统就是通过它来工作的。 (1)一般语法 >>> nameList = ['Walter',"Nicole",'Steven','Henry'] >>> for eachName in nameList: ... print eachName,"Lim" ... Walter Lim Nicole Lim Steven Lim Henry Lim 迭代一个列表.。每次迭代,eacgName变量都被设置为列表中特定某个元素。 >>> nameList = ['Cathy',"Terry",'Joe','Heather','Lucy'] >>> for nameIndex in range(len(nameList)): ... print "Liu,",nameList[nameIndex] ... Liu,Cathy Liu,Terry Liu,Joe Liu,Heather Liu,Lucy
没有迭代元素,而是通过列表的索引迭代。但通过直接迭代序列要比索引迭代快。 >>> nameList = ['Donn','Shirley','Ben','Janice','David','Yen','Wendy'] >>> for i,eachLee in enumerate(nameList): ... print "%d %s Lee" % (i+1,eachLee) ... 1 Donn Lee 2 Shirley Lee 3 Ben Lee 4 Janice Lee 5 David Lee 6 Yen Lee 7 Wendy Lee (3)用于迭代器类型 >>> range(3,7) [3,4,5,6] >>> for eachVal in range(2,19,3): ... print "value is:",eachVal ... value is: 2 value is: 5 value is: 8 value is: 11 value is: 14 value is: 17
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
