对象的布尔值
#测试对象的布尔值
print('------------------以下对象的布尔值为False------------------------')
print(bool(False)) #False
print(bool(0)) #False
print(bool(0.0)) #False
print(bool(None)) #False
print(bool('')) #False
print(bool("")) #False
print(bool([])) #空列表
print(bool(list())) #空列表
print(bool(())) #空元组
print(bool(tuple())) #空元组
print(bool({})) #空字典
print(bool(dict())) #空字典
print(bool(set())) #空集合
print('------------------------其他对象的布尔值均为true--------------------------')
分支结构 单分支结构
#单分支解构·
money=1000
s=int(input('请输入取款金额'))#取款金额
#判断余额是否充足
if money>=s:
money=money-s
print('取款成功,余额为:', money)
多分支结构
print('------------------------多支结构,多选一执行-------------------------')
#题目
#从键盘录入一个整数 成绩
#90-100 A
#80-89 B
#70-79 C
#60-69 D
#0-59 E
#小于0或者大于100 为非法数据(不是成绩的有效范围)
score=int(input('请输入一个成绩:'))
#判断
if score>=90 and score<=100:
print('A')
elif score>=80 and score<=89:
print('B')
elif score>=70 and score<=79:
print('C')
elif score>=60 and score<=69:
print('D')
elif score>=0 and score<=59:
print('E')
else:
print('对不起,成绩有误,不在成绩的有效范围内')
#判断
if 90<=score<=100:
print('A')
elif 80<=score<=89:
print('B')
elif 70<=score<=79:
print('C')
使用这样的形式会更加简便
---------------------------------------------------------------------------------------------------------------------------------
双分支结构
双分支结构 if else 二选一执行
#从键盘录入一个整数,编写程序让计算机判断是奇数还是偶数
num=int(input('请输入一个整数'))
#条件判断
if num%2==0:
print(num, '是偶数')
else:
print(num, '是奇数')
--------------------------------------------------------------------------------------------------------------------------------
嵌套if 的使用
answer=input('您是会员吗?y/n')
money=float(input('请输入您的购物金额:'))
#外层判断是否是会员
if answer =='y' : #会员
if money>=200:
print('打八折,付款金额为:', money*0.8)
elif money>=100:
print('打九折,付款金额为:', money*0.9)
else:
print('不打折,付款金额为:', money)
else:
if money>=200:
print('达九点五折,付款金额为:', money*0.95)
else:
print('不打折,付款金额为:', money)
-------------------------------------------------------------------------------------------------------------------------------
条件表达式
简化之后
#从键盘录入两个整数,比较两个整数的大小
num_a=int(input('请输入第一个整数:'))
num_b=int(input('请输入第二个整数:'))
#比较大小
#第一个代码是比较复杂的
#if num_a>=num_b:
# print(num_a, '大于等于', num_b)
#else:
# print(num_a, '小于等于', num_b)
print('使用条件表达式进入比较')
print((num_a, '大于等于', num_b) if num_a>=num_b else (num_a, '小于', num_b))
------------------------------------------------------------------------------------------------------------------------------
pass语句
#pass 语句,什么都不是只是一个占位符,用到需要写语句的地方
answer=input('您是会员吗?y/n')
money=float(input('请输入您的购物金额:'))
if answer =='y' : #会员
pass
else:
pass
#在这样的情况下不会报错可以先写好框架