Python 的条件与循环语句,,Python 条件语


Python 条件语句:
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功
1、if语句用于控制程序的执行:
if 条件:
代码块
else:
代码块

例:if 基本用法
#!/usr/bin/python
# -*- coding: UTF-8 -*-

flag = False
name = ‘luren‘
if name == ‘python‘: # 判断变量否为‘python‘
flag = True # 条件成立时设置标志为真
print ‘welcome boss‘ # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
输出结果为:
>>> luren# 输出结果


例:elif用法

#!/usr/bin/python
# -*- coding: UTF-8 -*-

num = 5
if num == 3: # 判断num的值
print ‘boss‘
elif num == 2:
print ‘user‘
elif num == 1:
print ‘worker‘
elif num < 0: # 值小于零时输出
print ‘error‘
else:
print ‘roadman‘ # 条件均不成立时输出
输出结果为:
>>> roadman# 输出结果


2、多个条件的判断:
if 条件:
代码块
elif 条件: (else if缩写elif)
代码块
else:
代码块

例:
num = 9
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print ‘hello‘
>>> hello# 输出结果

num = 10
if num < 0 or num > 10: # 判断值是否在小于0或大于10
print ‘hello‘
else:
print ‘undefine‘
>>> undefine# 输出结果

num = 8
# 判断值是否在0~5或者10~15之间
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
print ‘hello‘
else:
print ‘undefine‘
>>> undefine# 输出结果
3、条件
True False
1 > 2 n1 > n2 n1 == n2
name == "alex" or name == "eric"
name != "alex"
name == "alex" and pwd == "123"
Python循环语句:

1,循环语句
while 循环在给定的判断条件为 true 时执行循环体,否则退出循环体。
for 循环重复执行语句
嵌套循环你可以在while循环体中嵌套for循环

2,循环控制语句:

break 语句在语句块执行过程中终止循环,并且跳出整个循环
continue 语句在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
pass 语句pass是空语句,是为了保持程序结构的完整性。

Python While循环语句
基本形式:
while 判断条件:
执行语句……
判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
例:
#!/usr/bin/python

count = 0
while (count < 9):
print ‘The count is:‘, count
count = count + 1

print "Good bye!"

以上代码执行输出结果:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
while 语句两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
# continue 和 break 用法

i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10

i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break
无限循环
如果条件判断语句永远为 true,循环将会无限的执行下去
例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

var = 1
while var == 1 : # 该条件永远为true,循环将无限执行下去
num = raw_input("Enter a number :")
print "You entered: ", num

print "Good bye!"

以上实例输出结果:
Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
File "test.py", line 5, in <module>
num = raw_input("Enter a number :")
KeyboardInterrupt
循环使用 else 语句

在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
#!/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"
以上实例输出结果为:
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

Python 的条件与循环语句

相关内容

    暂无相关文章

评论关闭