首页 / PYTHON / Python学习笔记四:过程控制
Python学习笔记四:过程控制
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python学习笔记四:过程控制,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2987字,纯文字阅读大概需要5分钟。
内容图文
条件语句:
#以缩进来区分表示同一范围。
1 # coding=utf8 2 # 例1:if 基本用法 3 4 flag = False 5 name = ‘luren‘ 6if name == ‘python‘: # 判断变量否为‘python‘ 7 flag = True # 条件成立时设置标志为真 8print‘welcome boss‘# 并输出欢迎信息 9else: 10print name # 条件不成立时输出变量名称
多重判断:
1 # coding=utf8 2 # 例2:elif用法 3 4 num = 5 5if num == 3: # 判断num的值 6print‘boss‘ 7elif num == 2: 8print‘user‘ 9elif num == 1: 10print‘worker‘11elif num < 0: # 值小于零时输出12print‘error‘13else: 14print‘roadman‘# 条件均不成立时输出
python 并不支持 switch 语句
1 # coding=utf8 2 # 例3:if语句多个条件 3 4 num = 9 5if num >= 0 and num <= 10: # 判断值是否在0~10之间 6print‘hello‘ 7 >>> hello # 输出结果 8 9 num = 10 10if num < 0 or num > 10: # 判断值是否在小于0或大于1011print‘hello‘12else: 13print‘undefine‘14 >>> undefine # 输出结果1516 num = 8 17# 判断值是否在0~5或者10~15之间18if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): 19print‘hello‘20else: 21print‘undefine‘22 >>> undefine # 输出结果
同一行,与IDL类似
1 # !/usr/bin/python 2 3 var = 100 45if ( var == 100 ) : print"Value of expression is 100"67print"Good bye!"
循环语句:
while
1 # !/usr/bin/python 2 3 count = 0 4while (count < 9): 5print‘The count is:‘, count 6 count = count + 1 78print"Good bye!"
while... else
1 # !/usr/bin/python 2 3 count = 0 4while (count < 9): 5print‘The count is:‘, count 6 count = count + 1 78print"Good bye!"
1 # !/usr/bin/python 2 3 flag = 1 45while (flag): print‘Given flag is really true!‘67print"Good bye!"
while循环嵌套
1 # coding=utf-8 2 # !/usr/bin/python 3 4 i = 2 5while(i < 100): 6 j = 2 7while(j <= (i/j)): 8ifnot(i%j): break 9 j = j + 1 10if (j > i/j) : print i, " 是素数"11 i = i + 1 1213print"Good bye!"
for
遍历任何序列的项目,列表或字符串
for i in sequence
statements(s)
1 # !/usr/bin/python 2 3 for letter in‘Python‘: # First Example 4print‘Current Letter :‘, letter 5 6 fruits = [‘banana‘, ‘apple‘, ‘mango‘] 7for fruit in fruits: # Second Example 8print‘Current fruit :‘, fruit 910print"Good bye!"
>
Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n Current fruit : banana Current fruit : apple Current fruit : mango Good bye!
索引
1 # !/usr/bin/python 2 3 fruits = [‘banana‘, ‘apple‘, ‘mango‘] 4for index in range(len(fruits)): 5print‘Current fruit :‘, fruits[index] 67print"Good bye!"
内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。
for.... else...(for循环嵌套)
1 # !/usr/bin/python 2 3 for num in range(10,20): #to iterate between 10 to 20 4for i in range(2,num): #to iterate on the factors of the number 5if num%i == 0: #to determine the first factor 6 j=num/i #to calculate the second factor 7print‘%d equals %d * %d‘ % (num,i,j) 8break#to move to the next number, the #first FOR 9else: # else part of the loop10print num, ‘is a prime number‘
break跳出整个循环 continue跳出本次循环 pass空语句,是为了保持程序结构的完整性。
1 # !/usr/bin/python 2 3 for letter in‘Python‘: 4if letter == ‘h‘: 5pass6print‘This is pass block‘7print‘Current Letter :‘, letter 89print"Good bye!"
原文:http://www.cnblogs.com/haizhupan/p/4198454.html
内容总结
以上是互联网集市为您收集整理的Python学习笔记四:过程控制全部内容,希望文章能够帮你解决Python学习笔记四:过程控制所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。