Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
可以通过下图来简单了解条件语句的执行过程:
if 判断条件:
执行语句……
else:
执行语句……
其中”判断条件”成立时(非零),则执行后面的语句,而执行内容可以多行,以”四个空格“来区分表示同一范围。
else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例1:if 基本用法
flag = False
name = 'luren'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print 'welcome boss' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
输出结果为:
luren # 输出结果
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值时,可以使用以下形式:
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# @Time : 12/25/2017 2:31 PM
# @Author : Zhdya
# @Email : [email protected]
# @File : test_1225.py
# @Software: PyCharm
score = int(raw_input("pls input a number: ")) ##把输入的数字转换成int型
if score >= 90 and score <= 100:
print "congurations!! your score level is A"
elif score >= 80:
print "congurations!! your score level is B"
elif score >= 70:
print "congurations!! your score level is C"
else:
print "Sorry!! you didn't pass the exam!"
由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果判断需要多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于>(大于)、<(小于)等判断符号,即大于和小于在没有括号的情况下会比与或要优先判断。
你也可以在同一行的位置上使用if条件判断语句,如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
var = 100
if ( var == 100 ) : print "变量 var 的值为100"
print "Good bye!"
以上代码执行输出结果如下:
Good bye!
# -*- coding: utf-8 -*-
# @Time : 2017/12/25 21:48
# @Author : Zhdya
# @Email : [email protected]
# @File : test_1225.py
# @Software: PyCharm
x = int(raw_input("pls input a number: "))
y = int(raw_input("pls input a number: "))
z = int(raw_input("pls input a number: "))
aa = [x,y,z]
list(aa)
aa.sort()
print aa 第一种
------------------------------------------
aa = [x,y,z]
list(aa)
for i in range(len(aa)):
for j in range(i+1):
if aa[i] < aa[j]:
# 实现连个变量的互换
aa[i],aa[j] = aa[j],aa[i]
print aa 第二种
-------------------------------------------------
aa = [x,y,z]
list(aa)
if x > y:
temp = x
x = y
y = temp
if y > z:
temp = y
y = z
z = temp
if x > y:
temp = x
x = y
y = temp
elif y > z:
temp = z
z = y
y = temp
if x > y:
temp = x
x = y
y = temp
print x,y,z 第三种
# 2.-------------------------------------------------
# 企业发放的奖金根据利润提成。
# 利润(I)低于或等于10万元时,奖金可提10%;
# 利润高 于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提 成7.5%;
# 20万到40万之间时,高于20万元的部分,可提成5%;
# 40万到60万之间时高于 40万元的部分,可提成3%;
# 60万到100万之间时,高于60万元的部分,可提成1.5%;
# 高于 100万元时,超过100万元的部分按1%提成;
# 从键盘输入当月利润I,求应发放奖金总数?
i = int(raw_input("pls input the profit: "))
if i <= 100000:
print "the bonus is: %s" % (i * 0.1)
elif i >= 10000 and i<= 200000:
print "the bonus is: %s" % (100000 * 0.1 + (i - 100000) * 0.075)
elif i >= 200000 and i<= 400000:
print "the bonus is: %s" % (100000 * 0.1 + 100000 * 0.075 + (i - 200000) * 0.05)
elif i >= 400000 and i<= 600000:
print "the bonus is: %s" % (100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (i - 400000) * 0.03)
elif i >= 600000 and i<= 1000000:
print "the bonus is: %s" % (100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.015 + (i - 600000) * 0.015)
else :
print "the bonus is: %s" % (100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.015 + 400000 *0.015 + (i - 1000000) * 0.01)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有