python基础之条件语句
2024-09-07 14:32:08
检查相等和不等
多个检查条件
age1 = 22
age2 = 19
s1 = age1 > 21 and age2 > 19
print(s1)
s2 = age1 > 21 or age2 > 19
print(s2)
检查是否包含在列表内
requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' in requested_toppings
print(s1)
s2 = 'pepperoni' in requested_toppings
print(s2)
检查不包含
requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' not in requested_toppings
print(s1)
s2 = 'pepperoni' not in requested_toppings
print(s2)
if语句
age = 19
if age >= 18:
print('You are old enough to vote')
if age >= 20:
print('is no print')
if-elif-else语句
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print('Your admission cost is $' + str(price) + '.')
检查特殊元素
requested_toppings= ['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print('Sorry,we are out of green peppers right now')
else:
print('Adding' + requested_topping + '.')
requested_toppings= []
if requested_toppings:
for requested_topping in requested_toppings:
print('Adding ' + requested_topping + '.')
else:
print('Are you sure you want a plain pizza?')
available_toppings = ['mushrooms','olives','green peppers','pepperoni']
requested_toppings= ['mushrooms','french fries','green peppers']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print('Adding ' + requested_topping + '.')
else:
print("Sorry,we don\'t have " + requested_topping + '.')
最新文章
- EF 资料
- python抓取某学院视频
- 【BZOJ-4281】Związek Harcerstwa Bajtockiego 树上倍增LCA
- 【原】iOS学习40网络之数据安全
- 使用adb shell dumpsys检测Android的Activity任务栈
- javascript学习-原生javascript的小特效(改变透明度效果)
- in 与 = 的区别
- MVC4 Razor视图下使用iframe加载RDLC报表
- linux 定时执行shell脚本
- MySQL5.5.28启动错误 The server quit without updating PID file
- SDWebImage源码解读之SDWebImageManager
- 如何修改Tomcat默认端口?
- centos7 python3 pip
- Oracle下查看索引的语句
- 网球pt站 T3nnis 情况说明
- Android应用开发资源
- Ext Js 6+ 如何引入dashboard模版
- 路由交换02-----ARP协议
- Linux内核分析第四周学习总结
- oracle-sql分析练习